Search code examples
javaspringaoppointcut

Add aspect to implementation class not interface


We have a class which implements several interfaces. We would like to add some pointcut for the entire class - not for a particular interface of it.

How it can be done with Spring AOP? Is it possible to apply an aspect to non-interface class?


Solution

  • Yes, you have to use CGLIB to proxy classes. See section 7.1.3 of the spring docs

    Spring AOP can also use CGLIB proxies. This is necessary to proxy classes, rather than interfaces. CGLIB is used by default if a business object does not implement an interface. As it is good practice to program to interfaces rather than classes, business classes normally will implement one or more business interfaces.

    The magic is to define proxy-target-class via the following...

    <aop:config proxy-target-class="true">
        <!-- other beans defined here... -->
    </aop:config>