A, spring aop introduction
Second, what is spring aop and achieve
Oriented Programming spring AOP, and object-oriented programming is a supplement for each module crosscutting concerns distributed processing system, such as transaction management, logging, caching. It is the use of dynamic proxies, generates a temporary target for the AOP method in memory, this object contains all the methods of the target object, do the enhancement processing at a specific cut-off point, and the original callback method.
Spring AOP dynamic proxy There are two main ways to achieve, JDK dynamic proxy and cglib dynamic proxy. JDK dynamic proxy receives reflected by the proxy class, but the proxy class must implement the interface, and the core is InvocationHandler Proxy class. cglib dynamic proxy class that implements the interface is generally no class, cglib is a code generation library, you can dynamically generate a subclass of a class at runtime, so, CGLIB by inheritance way to do dynamic proxy, so if a class is marked as final, it can not be used for dynamic CGLIB agent.
realization of spring aop
Which way using Spring AOP proxy implementation logic in the realization of org.springframework.aop.framework.DefaultAopProxyFactory.
package org.springframework.aop.framework; import java.io.Serializable; import java.lang.reflect.Proxy; import org.springframework.aop.SpringProxy; @SuppressWarnings("serial") public class DefaultAopProxyFactory implements AopProxyFactory, Serializable { @Override public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException { if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) { Class<?> targetClass =config.getTargetClass (); IF (targetClass == null ) { the throw new new AopConfigException ( "CAN Not the TargetSource the Determine target class:" + . "A target or the Either interface IS AN required for Proxy Creation" ); } // If the judgment is the interface or proxied class, use JDK dynamic proxy IF (targetClass.isInterface () || Proxy.isProxyClass (targetClass)) { return new new JdkDynamicAopProxy (config); } // else with cglib dynamic proxies (not implement interface class) return new new ObjenesisCglibAopProxy (config); } the else{ // default jdk dynamic proxy return new new JdkDynamicAopProxy (config); } } / ** * {determining provided @link AdvisedSupport whether only} * specified { @link org.springframework.aop.SpringProxy Interface} * (or the agency did not specify the interface). * / Private Boolean hasNoUserSuppliedProxyInterfaces (AdvisedSupport config) { Class <?> [] = IFCS config.getProxiedInterfaces (); return . (Ifcs.length == 0 || (. 1 && == ifcs.length SpringProxy class .isAssignableFrom (IFCS [ 0 ]))); } }
2, Spring AOP class structure in FIG.
The process is a core InvocationHandler interface invoke () method
Three, aop term