Search code examples
javaspring-mvcaop

Converting AOP xml config to Java


I have the following xml based AOP code

    <aop:config>
        <aop:pointcut id="handlerMethodAop"
            expression="execution(@org.springframework.web.bind.annotation.RequestMapping * *(..))" />

        <aop:advisor pointcut-ref="handlerMethodAop"
            advice-ref="handlerMethodAdvice" />
        <aop:advisor pointcut-ref="handlerMethodAop"
            advice-ref="handlerMethodThrowsAdvice" />
        <aop:advisor pointcut-ref="handlerMethodAop"
            advice-ref="warningThrowsAdvice" />
    </aop:config>

The values in the advice-ref attributes are beans I have declared elsewhere.

How can I convert this to Java Config? 90% of my app is already using Java config so I don't need a lesson in the basics of how to do that, specifically just how to configure this AOP stuff.


Solution

    1. Use advice declarations in @Aspect components instead of advisors.
    2. Use declarative @Configuration elements such as @EnableAspectJAutoProxy instead of XML configuration.

    See Spring AOP manual.