4

I'm trying to get AOP to work on a JAX-RS service. It is working on Spring-injected objects, but not my JAX-RS services. My assumption is that it's not working because Jersey creates my JAX-RS objects, rather than Spring. How can I get AOP to work on my JAX-RS services?

Here's how I declare my aspects:

@Pointcut("execution (* com.ancestry.academy.api.user.UserService.*(..))")
public void apiMethods() {}

@Around("com.ancestry.academy.api.ApiAdvisor.apiMethods()")
public Object aroundApi(ProceedingJoinPoint pjp) throws Throwable
{
    System.out.println("User Service API called");

    return pjp.proceed();
}

@Pointcut("execution (* com.ancestry.academy.services.user.UserManagerImpl.*(..))")
public void serviceMethods() {}

@Around("com.ancestry.academy.api.ApiAdvisor.serviceMethods()")
public Object aroundService(ProceedingJoinPoint pjp) throws Throwable
{
    System.out.println("User Maanageer API called");

    return pjp.proceed();
}

The first aspect is on my JAX-RS service, and is the one that doesn't work. The second aspect is on a spring-injected bean that provides business logic, and it does work.

There's nothing particularly special about UserService, except that it extends SpringBeanAutowiringSupport.

I have <aop:aspectj-autoproxy/> in my spring XML file.

Robert Wille
  • 189
  • 1
  • 12
  • 1
    Spring only applies AOP to beans it knows and controls. The JAX-RS classes/beans aren't under control of spring and as such will not work with Spring AOP. Either make those JAX-RS classes part of spring or use loadtime- or compile time weaving of your aspects. – M. Deinum Feb 24 '14 at 14:33
  • Yes, I figured I needed to get these classes until spring control, but have been unsuccessful. I've tagged them with @Component and put them in my spring.xml, but spring creates an instance (presummably with an AOP proxy), and then jersey creates its own instances. So, how do I get jersey to use spring to obtain its service classes? The situation is different for another project, which uses jax-ws instead of jax-rs. It's service class do use spring AOP successfully. I don't understand why there would be a difference. – Robert Wille Feb 24 '14 at 17:00
  • I am looking for a solution to a similar question. You can check [here my question/analisis](http://stackoverflow.com/questions/21271485/why-my-aspect-is-not-detected-for-jersey-controller-using-custom-annotation). I've tried many things without luck but it can give you another idea or another way to test your app. Hope to help – Federico Piazza Feb 27 '14 at 20:45
  • 1
    Switching from Spring AOP to AspectJ solved the problem. Thanks. – Robert Wille Mar 01 '14 at 17:36
  • @RobertWille Are you using load-time weaving or compile-time weaving for AspectJ? I am not able to configure this with LTW. It seems like Jersey still created instances itself and they are not "visible" to AspectJ. – Zakhar Mar 10 '14 at 16:53
  • It would be nice if your wrote a little answer to your question about how the problem was solved. Then accept your own answer to close it. It would make life easier for people like me scanning SO for unanswered questions in order to help people. :-) – kriegaex Oct 16 '14 at 13:23
  • @RobertWille, I am facing the same problem. I have an aspect to profile methods with specific annotation. It is working fine in spring container. I am using AspectJ CompileTimeWeaving with Spring. I even tried with Component annotation and specify rest resource in spring xml stil not working. Looks like jersey creating its on beans in its container. It would be really helpful if you could explain how you fix the similar issue on your project? Thanks in advance. – Joshan George Mar 04 '16 at 14:26

0 Answers0