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)