"Go" Spring's AOP dynamic agent--jdk agent and Cglib agent

Source: Internet
Author: User
Tags aop

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.