Two proxy methods for AOP in spring (Java Dynamic agent and Cglib proxy)

Source: Internet
Author: User

The first agent is the dynamic proxy mode of Java has been analyzed, here no longer introduced, now we first understand the next gclib agent is what? How is it implemented? What is the difference between a Java dynamic agent?

Cglib (code Generation Library) is a powerful, high-performance, high-quality code generation Class library. It can extend Java classes and implement Java interfaces at run time.
The Cglib encapsulates ASM, which dynamically generates a new class at run time.
Cglib for proxies in AOP,JDK must be interface-based, cglib without this limitation.

Principle difference:

The Java Dynamic Agent uses the reflection mechanism to generate an anonymous class that implements the proxy interface, calling Invokehandler to process before invoking the concrete method. The Cglib dynamic agent is the use of ASM Open Source package, the class file of proxy object classes loaded in, by modifying its bytecode generation subclass to deal with.

1. If the target object implements an interface, AOP is implemented by default with the JDK's dynamic proxy
2. If the target object implements an interface, you can force the use of Cglib to implement AOP
3, if the target object does not implement the interface, must adopt the Cglib library, spring will automatically switch between the JDK dynamic agent and Cglib


How do I enforce AOP using cglib?
* Add Cglib Library, Spring_home/cglib/*.jar
* Add <aop:aspectj-autoproxy proxy-target-class= "true" to the spring configuration file/>

What is the difference between JDK dynamic agent and cglib bytecode generation?
* JDK dynamic agents can only generate proxies for classes that implement interfaces, not classes
* Cglib is the implementation of the proxy for the class, mainly to generate a subclass of the specified class, overriding the methods
Because it is inherited, it is best not to declare the class or method as final

The Java Dynamic agent and the Cglib agent are no different in other respects, only

Differences that are configured in the Spring configuration file:

<bean id= "#" class= "Org.springframework.ProxyFactoryBean" >

<property name= "Proxytargetclass" >

<value>true</value>

</property>

</bean>

***************************************************

<bean id= "#" class= "Org.springframework.ProxyFactoryBean" >

<property name= "Proxyinterfaces" >

<value>com.gc.impl.TimeBookInterface</value>

</property>

</bean>

Two proxy methods for AOP in spring (Java Dynamic agent and Cglib proxy)

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.