Results 1 to 4 of 4

Thread: Aspects not getting executed

  1. #1

    Default Aspects not getting executed

    Hi,
    I have question regarding aspects. I created an aspect with @AspectJ annotation. The aspect looks like this:

    Code:
    @Aspect
    @Component
    public class ExecutionTrackerAspect {
    
    	DateTime date;
    	Log log = LogFactory.getLog(getClass());
    	
    	@Before("execution(* doTest(..))")
    	public void executionStart() {
    		System.out.println("Message recieved and execution is starting: " + date.toString());
    	}
    	
    	@After("execution(* doTest(..))")
    	public void executionEnd() {
    		System.out.println("Message recieved and execution was done: " + date.toString());
    	}
    	
    	@Pointcut("execution(* doTest(..))")
    	public void testPointCut() {
    		System.out.println("FAXEN");
    	}
    	
    }
    The methods where it should be applied on is:
    Code:
    	@Override
    	public void onMessage(Message message, Channel channel) throws Exception {
    		Job job = convertMessage(message);
    		
    		
    		Thread.sleep(2000);
    		
    		System.out.println("received: " + job.getCommand());
    		doTest();
    		channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
    	}
    
    	public void doTest() {
    		System.out.println("I AM DOING TESTS");
    	}
    For my test the instantiation looks like this:

    Code:
    ApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
    With the ConsumerConfiguration.class
    Code:
    @Configuration
    @ImportResource("classpath:/de/sample/package/logging/aspects-config.xml")
    public class ConsumerConfiguration extends ServiceAMQPConfiguration {
    ...
    My aspect-config.xml looks like this:
    Code:
    	<context:component-scan base-package="de.sample.package.logging" />
    	<aop:aspectj-autoproxy />
    If I take a look at the stack-trace I can also see this:

    Code:
    2011-10-25 18:42:46,694 DEBUG [org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory] - <Found AspectJ method: public void de.sample.package.logging.ExecutionTrackerAspect.executionException(java.lang.Exception)>
    2011-10-25 18:42:46,882 DEBUG [org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory] - <Found AspectJ method: public void de.sample.package.logging.ExecutionTrackerAspect.executionStart()>
    2011-10-25 18:42:46,882 DEBUG [org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory] - <Found AspectJ method: public void de.sample.package.logging.ExecutionTrackerAspect.executionEnd()>
    The problem is, that the System.out.println("Message...."); never gets executed. Any idea?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Please use the forum search as this question has been answered numerous times before. Also read the reference guide especially the aop chapter and especially the part which explains proxies.

    In short, spring uses proxie based aop which means INTERNAL method calls will not get intercepted. Your call to your doTest method is an internal method call.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Hi Marten,
    thank you for your answer. I read the AOP chapter a numerous of times. To be specific, I read it 4 times now. Do I understand you right, that only methods called like this:

    Code:
    BeanFactory ctx = new ClassPathXmlApplicationContext("x/y/plain.xml"); 
    FooService foo = (FooService) ctx.getBean("fooService");
    foo.getFoo("Pengo", 12);
    get intercepted? And for interception of internal methods it is necessary to use AspectJ?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    That is what I explained. If you want more powerful AOP you will have to use either load time or compile time weaving.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

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