Announcement Announcement Module
No announcement yet.
Using MethodInterceptor classes with new AspectJ Annotations Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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:
    public class MyConfig {

    //define beans for DebugInterceptor, etc here.


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

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

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

    Last edited by tjhaz; Jun 5th, 2013, 01:13 PM.

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

    public Advisor debugAdvisor() {
       AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        return new DefaultPointcutAdvisor(pointcut, new DebugInterceptor());