When you use Spring AOP, you encounter the following error:
Exception in thread "main" Java.lang.ClassCastException:com.sun.proxy. $Proxy 0 cannot is cast to Com.spring.test.setter.Instrumentalist
At Com.spring.test.setter.test.main (test.java:12)
Reading the wrong message shows that the class generated by the dynamic agent cannot be converted to our custom implementation class.
Workaround:
Add proxy-target-class= "true" to the aop:config tag.
"Explanatory Notes"
According to the blog saying: http://blog.csdn.net/z69183787/article/details/17161297
There are two ways to generate proxy classes: JDK and Cglib, one based on interfaces and one class-based.
If you add the above attribute, you use class-based cglib, instead, if there is no write or false, the proxy class is generated through the JDK's interface-based approach.
Of course, if it is not based on the interface itself, then it will automatically use the Cglib way, where it is very strange why there is no way to automatically go cglib.
The reason, but also to see the source of AOP, here first to do the next record.
Here is my own spring configuration file, for reference only
<aop:config proxy-target-class="true"> <aop:aspectref="Audience"> <aop:before pointcut="Execution (* com.spring.test.action1.Performer.perform (..))"Method="Takeseats"/> <aop:before pointcut="Execution (* com.spring.test.action1.Performer.perform (..))"Method="Turnoffcellphones"/> <aop:after-returning pointcut="Execution (* com.spring.test.action1.Performer.perform (..))"Method="Applaud"/> <aop:after-throwing pointcut="Execution (* com.spring.test.action1.Performer.perform (..))"Method="Demandrefund"/> </aop:aspect> </aop:config>
Spring AOP Error: Com.sun.proxy. $Proxyxxx cannot is cast to yyy