Results 1 to 4 of 4

Thread: When aspect weaving is on it causes a returned object to become null? off no issue

  1. #1
    Join Date
    Sep 2007
    Posts
    138

    Default When aspect weaving is on it causes a returned object to become null? off no issue

    I'm new to creating my own Aspects and the aspect @Around is working in the sense that I see the output before and after the method executes.

    The issue however is that when I have aspectJ enabled my return value from the service becomes null??? I remove the use of aspectj and it returns fine.

    "results" here below is null when the aspects are enabled and not null when off...

    Code:
    @Test
    public void getStuff() {
    	List<CharacteristicValueDef> results = service.getStuff();
    	logger.debug("results  {}", results);
    	Assert.assertTrue(results.size() > 0);
    }
    If I log what service.getStuff() returns the list IS there... BUT after the method (getStuff()) exists aspectj must be setting it to null somehow since the logger above shows results as NULL.

    Below is my aspect set up:

    Code:
    @Component
    @Aspect
    public class TimeMethodAspect {
    	private final static Logger logger = LoggerFactory.getLogger(TimeMethodAspect.class);
    
    	@Pointcut("within(com.foo.services.product..*)")
    	public void adviceMethods() {
    	}
    
    	@Around("adviceMethods()")
    	public void execute(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    		logger.debug("entering {}",proceedingJoinPoint.getSignature());
    		StopWatch stopWatch = new StopWatch();
    		stopWatch.start();
    		proceedingJoinPoint.proceed();
    		stopWatch.stop();
    		logger.debug("Elapsed time calling {}: {}", proceedingJoinPoint.getSignature(), stopWatch);
    	}
    }
    The above does wrap my getStuff() call as it should, but any idea why my "results" returned from getStuff becomes null in my test case? (I'm using @RunWith(SpringJUnit4ClassRunner.class) and @ContextConfiguration(locations=.... )

    Thanks for any help

  2. #2
    Join Date
    May 2011
    Location
    New Delhi, India
    Posts
    157

    Default

    Your method is not returning anything. In case you want to use aspects for performance monitoring you should follow approach provided in the attached file.RepositoryPerformanceMonitor.zip

  3. #3
    Join Date
    Sep 2007
    Posts
    138

    Default

    The method having the aspect applied is Service.getStuff. The above method is just the unit test I'm using to see the aspect being applied before and after service.getStuff() is called:

    Code:
    //aspect starts stopwatch
    List<CharacteristicValueDef> results = service.getStuff();
    //aspect ends stopwatch
    So the method service.getStuff *IS* returning something, it's returning results. The issue is that for some reason "results" when it's used in the unit test within the log statement it's null.. yet logging what service.geStuff() returns does show as it's exiting it is returning a hydrated list of results. If I remove having service.getStuff() from having the aspect applied.. I do see the results as not null - that's why I'm confused what's going on.

    Looking at the RepositoryPerformanceMonitor you sent now.

  4. #4
    Join Date
    Sep 2007
    Posts
    138

    Default

    Thanks rishishehrawat... after modifying my Aspect to the following based on your class you provided things are working.

    Code:
    @Component
    @Aspect
    public class TimeMethodAspect {
    	private final static Logger logger = LoggerFactory.getLogger(TimeMethodAspect.class);
    
    	@Around("within(com.ncs.ncsapp.services.service.product..*)")
    	public Object execute(ProceedingJoinPoint outMethod) throws Throwable {
    		logger.debug("entering {}",outMethod.getSignature());
    		StopWatch stopWatch = new StopWatch();
    		stopWatch.start();
    		try {
    			return outMethod.proceed();
    		} finally {
    			stopWatch.stop();
    			logger.debug("Elapsed time calling {}: {}", outMethod.getSignature(), stopWatch);
    		}
    	}
    }

    What's confusing though is none of the examples online seem to show things that way. For example, I followed the pattern at the end of this document http://www.mkyong.com/spring3/spring...ation-example/ (where you see Around being used.)

    I'm sure glad you posted your example. Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •