4

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

Wilhelm Kleu
  • 10,821
  • 4
  • 36
  • 48

2 Answers2

2

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.

Wilhelm Kleu
  • 10,821
  • 4
  • 36
  • 48
0

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

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

See this link for possible solution

Community
  • 1
  • 1
Aravind A
  • 9,507
  • 4
  • 36
  • 45
  • I have tried specifying parameter name in the pointcut (`@Pointcut("@annotation(wsTransaction)")` and specifying the value and argNames) with no luck. Also according to http://static.springsource.org/spring/docs/3.0.3.RELEASE/spring-framework-reference/html/aop.html I should be able to use it like I did. – Wilhelm Kleu Dec 08 '11 at 11:17
  • Pointcuts like that provided above worked fine for me in Spring as well with annotated methods. – Joseph Lust Mar 12 '13 at 13:43