Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 497
jdevelop Avatar asked Aug 14 '26 12:08

jdevelop


1 Answers

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>
like image 182
Andrew White Avatar answered Aug 17 '26 02:08

Andrew White



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!