Announcement Announcement Module
Collapse
No announcement yet.
Using AOP with JAX-RS / Jersey resources Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using AOP with JAX-RS / Jersey resources

    Hi,

    I have a JAX-RS resource that uses Jersey's own @Inject annotation to inject a transactional Spring service bean. Injection works, and the service works.

    I want to create an aspect for logging. I wrote one, and was able to get it to work if I make the pointcut refer to the transactional Spring service bean, but I can't get it to execute when the pointcut points to the JAX-RS resource.

    My XML config looks like this:

    Code:
    <bean id="restLogAspect" class="com.company.product.service.rest.resource.RESTServiceLogAspect"/>
    
    <aop:config>
        <aop:pointcut id="loggingPointcut" expression="within(com.company.product.service.rest.MyResource)" />
    
        <aop:aspect id="loggingAspect" ref="restLogAspect">
            <aop:around pointcut-ref="loggingPointcut" method="aroundInvoke"/>
        </aop:aspect>
    </aop:config>
    On the JAX-RS resource class I added a couple annotations to make sure the class is managed by the Spring container:

    @Component
    @Scope("singleton")

    My XML config also includes:

    Code:
    <context:annotation-config/>
    <context:component-scan base-package="com.company.product"/>
    I suspect the problem is one of two things:

    1) JAX-RS/Jersey is not getting the resource instance from Spring container
    2) My pointcut needs to change

    Any ideas?


    Thanks,
    Ryan

  • #2
    I am now looking into Jersey specific filters:

    https://jersey.dev.java.net/nonav/ap...e-summary.html

    Comment


    • #3
      I am also trying the same thing. The Aspect does not get called on the Jersey REST resource but gets called on the bean injected in the resource. Did you get it to work?

      Comment


      • #4
        Originally posted by nikhiljain View Post
        I am also trying the same thing. The Aspect does not get called on the Jersey REST resource but gets called on the bean injected in the resource. Did you get it to work?
        Wow this is an old thread.. I think I ended up using Jersey's built-in logging filter configured in web.xml since logging is the only reason I needed AOP.

        The only thing we used the Spring container for was dependency injection. Everything else was Java EE (JAX-RS, JAX-WS, JPA, JTA, etc.) Our app is now being rewritten from scratch in Java EE 7 and so we no longer have a need for Spring. We're switching to CDI which is already well integrated into the rest of Java EE.

        Ryan

        Comment

        Working...
        X