Java Dynamic proxy and CGLib proxy for IT Ninja Turtles, and cglib for ninja turtles

Source: Internet
Author: User

Java Dynamic proxy and CGLib proxy for IT Ninja Turtles, and cglib for ninja turtles

  1. <Br> public class UserDAOImpl {
  2. <Br> public void save (){
  3. <Br> // TODO Auto-generated method stub
  4. <Br> System. out. println ("user saved ");
  5. <Br>}
  6. <Br>}
  7. <Br> // related configuration, with irrelevant content omitted
  8. <Br> <bean id = "userDAO" class = "UserDAOImpl">
  9. <Br> <bean id = "userDAOProxy" class = "org. springframework. transaction. interceptor. TransactionProxyFactoryBean">
  10. <Br> <property name = "target">
  11. <Br> <ref local = "userDAO"/>
  12. <Br> </property>
  13. <Br> </bean>

Test code

  1. ApplicationContext ctx =
  2. <Br> new FileSystemXmlApplicationContext ("applicationContext. xml ");
  3. <Br> UserDAOImpl userDAOImpl =
  4. <Br> (UserDAOImpl) ctx. getBean ("userDAOProxy ");
  5. <Br> userDAOImpl. save ();

In this case, the program can run normally, but if UserDAOImpl implements an interface, the rest will not change.

  1. Public class UserDAOImpl implements UserDAO {
  2. <Br>
  3. <Br> public void save (){
  4. <Br> // TODO Auto-generated method stub
  5. <Br> System. out. println ("user saved ");
  6. <Br>}
  7. <Br>
  8. <Br>}

In this case, the program will not run normally and will throw a java. lang. ClassCastException

To understand the cause of this situation, you need to understand the implementation principles of Spring AOP.
Spring Implementation of AOP relies on JDK dynamic proxy and CGLIB proxy.
Below is a brief introduction to JDK dynamic proxy and CGLIB proxy
JDK dynamic Proxy: the proxy object must be the implementation of an interface. It is the proxy of the target object by creating an interface implementation class during running.
CGLIB Proxy: The implementation principle is similar to JDK dynamic proxy, but the proxy object generated during running is a subclass of the target class extension. CGLIB is an efficient code generation package. At the underlying layer, CGLIB relies on ASM (open-source java bytecode editing class library) to operate on bytecode, which has better performance than JDK.
What does Spring rely on to determine which proxy policy is used to generate the AOP proxy? The following code is the judgment logic of Spring.

 

 

 

 

 

AdvisedSupport. isOptimize () and advisedSupport. isProxyTargetClass () returns false by default. Therefore, whether or not the target object implements an interface determines the policy adopted by Spring by default. Of course, you can set advisedSupport. isOptimize () or advisedSupport. isProxyTargetClass () returns true, so that CGLIB proxy is used no matter whether the target object implements the interface Spring. Therefore, by default, if a target object implements the Spring interface, the JDK dynamic proxy policy will be used to dynamically create an interface implementation class (Dynamic proxy class) to proxy the target object, this dynamic proxy class is another version of the target object. Therefore, j ava is thrown during forced conversion between the two. lang. classCastException. By default, if the target object does not implement any interfaces, Spring selects the CGLIB proxy, and the generated dynamic proxy object is a subclass of the target class.

 

 

By default, you can also manually configure some options to use CGLIB proxy for Spring.

Org. springframework. transaction. interceptor. transactionProxyFactoryBean is org. springframework. aop. framework. proxyConfig subclass, so you can refer to some settings in ProxyConfig as follows, set optimize and proxyTargetClass to true, you can force Spring to use CGLIB proxy.

  1. // Related configuration, omitted some irrelevant content
  2. <Br> <bean id = "userDAO" class = "UserDAOImpl">
  3. <Br> <bean id = "userDAOProxy" class = "org. springframework. transaction. interceptor. TransactionProxyFactoryBean">
  4. <Br> <property name = "target">
  5. <Br> <ref local = "userDAO"/>
  6. <Br> </property>
  7. <Br> <property name = "optimize">
  8. <Br> <value> true </value>
  9. <Br> </property>
  10. <Br> <property name = "proxyTargetClass">
  11. <Br> <value> true </value>
  12. <Br> </property>
  13. <Br> </bean>

 

 

The ClassCastException problem mentioned above will not occur when CGLIB proxy is used,

It can also improve the performance, but it also has its drawbacks. The original Spring doc explains the following optimization will usually mean that advice changes won't take effect after a proxy has been created. for this reason, optimization is disabled by default.

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.