Back Forum Reply New

Applying advice to methods marked with annotation

So I picked up a few of Denis' examples today and started playing with changing pointcuts, trying to use all the designators available. Loads of fun but I'm stuck on applying advice to methods marked with a particular annotation.

The annotation :

Code:
package com.spring.aop;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {   String value() default quot;defquot;;
}
The aspect :

Code:
package com.spring.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org..stereotype.Component;

@Aspect
@Component
public class TestAspect {
   @Before(quot;@target(com.spring.aop.TestAnnotation)quot;)   public void advice() {       System.out.println(quot;xxxxxxxxx: TestAspect.advice()quot;);   }
}
The service :

Code:
package com.spring.aop;

import org..stereotype.Component;

@Component
public class AopService {

@TestAnnotation
public void service(Object o) {
System.out.println(quot;xxxxxxxxxx: AopService.service()quot;);
}
}
The configuration:

Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;beans xmlns=quot;schema/beansquot;
xmlns:xsi=quot;2001/XMLSchema-instancequot; xmlns:aop=quot;schema/aopquot;
xmlns:tx=quot;schema/txquot; xmlns:context=quot;schema/contextquot;
xmlns:util=quot;schema/utilquot;
xsi:schemaLocation=quot; schema/beans schema/beans/spring-beans-2.5.xsd schema/tx schema/tx/spring-tx-2.5.xsd schema/aop schema/aop/spring-aop-2.5.xsd schema/context schema/context/spring-context-2.5.xsd schema/util schema/util/spring-util-2.5.xsdquot;
default-lazy-init=quot;truequot;gt;
lt;context:component-scan base-package=quot;com.spring.aopquot; /gt;
lt;aop:aspectj-autoproxy proxy-target-class=quot;truequot; /gt;
lt;bean name=quot;aopServicequot; id=quot;aopServicequot; class=quot;com.spring.aop.AopServicequot; /gt;
lt;/beansgt;
And finally, SpringStart.java:

Code:
package com.spring.aop;

import org..context.support.FileSystemXmlApplicationContext;

public class SpringStart {   public static void main(String[] args) throws Exception {       FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(quot;spring-config.xmlquot;);
       AopService aopService = (AopService)context.getBean(quot;aopServicequot;);       aopService.service(new Object());   }
}
It is my understanding that the advice should be applied to the service() method of AopService, however it is not. Where did I make a mistake?

Your pointcut is incorrect (it expects the class to be annotated with TestAnnotation). Change it to the following:Code:
execution(@com.spring.aop.TestAnnotation * *(..))
-Ramnivas

Thanks I see it now. I misunderstood the @target designator completely.
¥
Back Forum Reply New