Spring AOP is implemented in proxy mode and currently supports two types of proxies: JDK dynamic agent, cglib Agent to create an AOP proxy, and Spring recommends the use of JDK dynamic proxies.
- JDK Dynamic Proxy: uses the java.lang.reflect.Proxy dynamic proxy implementation, which is to extract the interface of the target object and then create an AOP proxy on the interface.
- CGLIB Agent: Cglib agent can not only interface agent, also can carry out class agent, cglib agent need to pay attention to the following issues:
The final method cannot be notified because the final method cannot be overwritten (Cglib creates the proxy by generating subclasses).
Generates two constructor calls, the first of which is the constructor call of the target class, and the second is the constructor call of the cglib generated proxy class. If you need to Cglib proxy methods, make sure that two constructor calls do not affect the app.
Spring AOP defaults to using the JDK dynamic proxy first to proxy the target object, if the target object does not implement any interface will use the Cglib proxy, if you need to force the use of the Cglib proxy, use the following way to specify:
For schema-style configuration facets, use the following method to specify the use of the Cglib proxy:
<proxy-target-class= "true"> </aop:config >
If you use the @aspectj style, use the following method to specify the use of the Cglib proxy:
<proxy-target-class= "true"/>
Open Tao Spring3 (6.9)-AOP 6.9 proxy mechanism