简体   繁体   中英

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"/> )

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.

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

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

See this link for possible solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM