Wilhelm Kleu
Wilhelm Kleu

Reputation: 11047

Spring AOP: Annotation pointcut not causing advice to execute

I am using Spring AOP (with AspectJ annotation style support) and want to execute code if a method is annotated with a specific annotation (WsTransaction).

Here is my aspect:

@Aspect
@Component
public class ExampleAspect {

    @Pointcut("execution(* example.*.ws.*.*(..))")
    public void isWebService() {}

    @Pointcut("@annotation(example.common.ws.WsTransaction)")
    public void isAnnotated() {}

    @Before("isWebService() && isAnnotated()")
    public void before() {
        System.out.println("before called");
    }
}

This is an example class where I expect it to run:

package example.common.ws;

@Endpoint
public class SomeEndpoint {

    @WsTransaction() // I want advice to execute if this annotation present
    @PayloadRoot(localPart = "SomeRequest", namespace = "http://example/common/ws/")
    public SomeResponse methodToBeCalled(SomeRequest request) {
            // Do stuff
            return someResponse;
    }
}

When I change @Before to only use isWebService() it is called but when I try it with isWebService() && isAnnotated() or just isAnnotated() nothing seems to happen.

I have <aop:aspectj-autoproxy/> in my Spring config.

The endpoint is created by Spring (using component-scan).

The annotation's retention policy is runtime.

Spring version is 3.0.3.RELEASE

I am not sure what is wrong or what I can try to debug.

Update: It seems Spring AOP doesn't pickup @Endpoint annotated classes

Update 2: AopUtils.isAopProxy(this) and AopUtils.isCglibProxy(this) are both false (Even when using <aop:aspectj-autoproxy proxy-target-class="true"/>)

Upvotes: 4

Views: 10320

Answers (2)

Wilhelm Kleu
Wilhelm Kleu

Reputation: 11047

Firstly I had to use <aop:aspectj-autoproxy proxy-target-class="true"/> to use class-based (CGLIB) proxies (instead of Java interface-based proxies).

Secondly (and this is where I got stuck) I had to specify the above in the contextConfigLocation of the servlet handling the SOAP requests (MessageDispatcherServlet) instead of the root application context.

Upvotes: 2

Aravind A
Aravind A

Reputation: 9697

I guess there may be some issue with the pointcut declaration.

  @Pointcut("@annotation(example.common.ws.WsTransaction)")

See this link for possible solution

Upvotes: 0

Related Questions