How spring decides to use the JDK dynamic agent and cglib (GO)

Source: Internet
Author: User

Spring1.2: the transaction Agent factory [Transactionproxyfactorybean] or automatic proxy interceptor [Beannameautoproxycreator] the Proxytargetclass property, set to True, uses the cglib proxy, which defaults to false, using the JDK dynamic proxy. the following references Spring Framework reference 2.0.5: Spring2.0:

The Spring AOP section uses JDK dynamic proxies or cglib to create proxies for target objects. (It is recommended that you use the JDK dynamic agent as much as possible)

If the target object of the agent implements at least one interface, the JDK dynamic proxy is used. All interfaces implemented by this target type will be proxied. If the target object does not implement any interfaces, create a cglib proxy.

If you want to force the use of the Cglib proxy (for example, you want to proxy all the methods of the target object, not just the method that implements the self-interface). However, the following questions need to be considered:

The Final method cannot be notified (advise) because they cannot be overwritten.
You need to place the Cglib 22 distribution package under Classpath, which provides a dynamic proxy compared to the JDK itself
Forcing the use of the Cglib agent requires the |aop:config| The Proxy-target-class property is set to true:

|aop:config proxy-target-class= "true" |
...
|/aop:config|

When you need to use Cglib Proxy and @aspectj automatic proxy support, set the |aop:aspectj-autoproxy| as follows: The Proxy-target-class property:

|aop:aspectj-autoproxy proxy-target-class= "true"/|

and the actual use of the process will find the difference between the details, the devil is in the detail. JDK Dynamic Proxy: Its proxy object must be an implementation of an interface that accomplishes a proxy to the target object by creating an implementation class of an interface during runtime.
Cglib Proxy: The implementation principle is similar to the JDK dynamic proxy, except that the proxy object that it generates during runtime is a subclass of the target class extension. Cglib is an efficient code generation package that relies on ASM (open source Java bytecode editing class library) to operate bytecode implementations that are more robust than JDK.
What does spring rely on to determine which proxy strategy to use to generate an AOP agent? The following code is spring's judgment logic

//Org.springframework.aop.framework.DefaultAopProxyFactory
//Parameter Advisedsupport is a spring AOP configuration related class
PublicAopproxy createaopproxy (Advisedsupport advisedsupport)
Throwsaopconfigexception {
//Determine whether to use the JDK dynamic agent or the Cglib agent
If(Advisedsupport.isoptimize ()||Advisedsupport.isproxytargetclass ()
||Hasnousersuppliedproxyinterfaces (Advisedsupport)) {
If(!cglibavailable) {
ThrowNewAopconfigexception (
"Cannot proxy target class because CGLIB2 is not available."
+"Add CGLIB to the class path or specify proxy interfaces."               }   
             return  Cglibproxyfactory.createcglibproxy (advisedsupport);    
          } else {    
             return new jdkdynamicaopproxy (advisedsupport);    
          }   
     }  

advisedsupport.isoptimize () With Advisedsupport.isproxytargetclass () The default return is False, so by default the target object has no implementation interface that determines the policy that spring takes. Of course you can set advisedsupport.isoptimize () or Advisedsupport.isproxytargetclass () to return to True, This will choose to use the Cglib proxy regardless of whether the target object has implemented the interface.

So by default, if a target object implements the interface spring chooses the JDK dynamic agent policy to dynamically create an interface implementation class (Dynamic proxy Class) to proxy the target object, it is common to understand that this dynamic proxy class is another version of the target object, So the java.lang.ClassCastException will be thrown in between the two when casting. And so, by default, if the target object does not implement any interfaces, spring chooses the Cglib proxy, which generates a dynamic proxy object that is a subclass of the target class.

says by default, you can also manually configure some options to make spring adopt the Cglib proxy.  
Org.springframework.transaction.interceptor.TransactionProxyFactoryBean is org.springframework.aop.framework. Proxyconfig, so you can refer to some of the settings in Proxyconfig as shown below, and setting either optimize and Proxytargetclass to true will force spring to adopt the Cglib proxy.

If you need to use Cglib Proxy and @aspectj automatic proxy support, set the |aop:aspectj-autoproxy| as follows: The proxy-target-class property:
|aop:aspectj-autoproxy proxy-target-class= "true"/|

This use of Cglib proxy will not appear the aforementioned classcastexception problem, but also can improve performance, the key is whether the proxy object inherits the interface can be used uniformly.

How spring decides to use the JDK dynamic agent and cglib (GO)

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.