Original address:
http://www.iteye.com/topic/323547
We all know that AOP uses the proxy mode, this article mainly introduces the two proxy mode how to set up and the difference, the original text of some of the contents of the reference after the addition of their own understanding and more in-depth elaboration:
first, the JDK agent and cglib agent of the underlying implementation differences
* JDK Agent can only
for classes that implement an interfacein a reflective way.The build agent, not the class, is also called the interface agent
* Cglib is
for ClassImplements the proxy, primarily for the specified classbyte-code conversion method(ASM framework) generates a
subclass , and override the methods in them.
"Note": There are two special cases, static and final methods:
|
Final Method |
Static Method |
JDK Agent |
The interface cannot use the final keyword, so it cannot be used. Error |
After the interface method uses static , the proxy object will not be able to access this method, so it cannot be used. Error |
Cglib Agent |
After the parent class method uses final , the subclass cannot override it and cannot intercept it. "No error, but no interception" |
After the parent class method uses static , the subclass cannot override it and cannot intercept it. "No error, but no interception" |
At the same time, when using the Cglib proxy, the target class must not be the final class (cannot be inherited), or error. The above can be seenwhen using proxies, try not to use the final and static keywords。
second, spring in two modes of deployment:
1. If the target object
implements an interface , AOP is implemented by default using the dynamic agent mechanism of the JDK, but it is possible to enforce AOP using cglib ;
2 . if the target object
does not implement an interface , it must use the Cglib build agent, and spring will automatically switch between Cglib and the JDK dynamic Agent .
3.
How do I force the use of cglib generation agents?
* Add Cglib Library, <spring_home>/lib/cglib/*.jar (in fact, SPRING's core package includes Cglib-nodep-2.2.jar, or build a project with MyEclipse will automatically introduce )
* In the Spring configuration file, add:
<aop:aspectj-autoproxy proxy-target-class= "true"/>
"Go" Spring's AOP dynamic agent--jdk agent and Cglib agent