Announcement Announcement Module
Collapse
No announcement yet.
Using MethodInterceptor classes with new AspectJ Annotations Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using MethodInterceptor classes with new AspectJ Annotations

    Hi all... I've always found the various MethodInterceptors that come with Spring quite useful (in org.springframework.aop.interceptor.*). I've begun to embrace the new Java Config style along with AspectJ Annotations but have not found an elegant way to configure the MethodInterceptors. Is there a way to use the @Aspect annotations to configure a MethodInterceptor in an @Around pointcut?? So far I've had to resort to using Java Config for everything else, but still have to keep around an XML config file to define an <aop:advisor>. I'd like to get rid of the xml file.

    Quick Example:
    @Configuration
    @ComponentScan
    @ImportResource("/path/to/aopconfig.xml")
    @EnableAspectJAutoProxy
    public class MyConfig {

    //define beans for DebugInterceptor, etc here.

    }

    @Aspect
    @Component
    public class MyAspect {
    @Pointcut("execution (* *..*Service.*(..))")
    public void tracePointcut() {}
    }

    contents of /path/to/aopconfig.xml:
    <beans>
    <aop:config>
    <aop:advisor advice-ref="debugInterceptor" pointcut="com.mycompany.MyAspect.tracePointcut()" order="1"/>
    <aop:advisor advice-ref="perfInterceptor" pointcut="com.mycompany.MyAspect.tracePointcut()" order="2"/>
    </aop:config>
    </beans>


    How can I get rid of aopconfig.xml and move the advisors into Java code????

    Thanks!
    Last edited by tjhaz; Jun 5th, 2013, 02:13 PM.

  • #2
    You can! Wrap the MethodInterceptor with a DefaultPointcutAdvisor bean, e.g. this is how it would look for your debugInterceptor

    Code:
    @Bean
    public Advisor debugAdvisor() {
       AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("com.mycompany.MyAspect.tracePointcut()");
        return new DefaultPointcutAdvisor(pointcut, new DebugInterceptor());
    }

    Comment

    Working...
    X