Spring---AOP
总结
基于代理模式。
Spring AOP支持两种类型的代理机制:JDK动态代理和CGLIB代理。这两种代理机制在不同情况下会被使用,具体取决于目标对象的类型。
1. JDK动态代理
- 适用情况:当目标对象实现了至少一个接口时,Spring会默认使用JDK动态代理。
- 实现原理:
- JDK动态代理是Java标准库提供的功能,位于
java.lang.reflect
包中。 - 允许 在运行时 创建 一个实现了一组给定接口的新类。这个新类可以用来作为代理,拦截对这些接口方法的调用,并可以在实际调用前后执行额外的逻辑。
- Spring会为 每个目标对象 创建 一个实现了相同接口的代理类。这个 代理类内部 持有一个目标对象的引用,并且重写了接口中的方法。
- 当调用代理对象的方法时,实际执行的是代理类中的方法。在这个方法中,Spring可以插入切面逻辑(如前置通知、后置通知等),然后再调用目标对象的实际方法。
- JDK动态代理是Java标准库提供的功能,位于
示例
2. CGLIB代理
- 适用情况:当目标对象没有实现任何接口时,Spring会使用CGLIB代理。
- 实现原理:
- CGLIB是一个开源库,它可以在运行时动态生成一个子类来扩展目标类。
- Spring会为每个目标对象创建一个继承自该目标类的子类。这个子类重写了父类的方法。
- 当调用代理对象的方法时,实际执行的是子类中的方法。同样,在这个方法中,Spring可以插入切面逻辑,然后再调用父类的实际方法。
/** * 【Spring-AOP实现】 * org.springframework.aop.framework.AopProxy * Delegate interface for a configured AOP proxy, allowing for the creation of actual proxy objects. * Out-of-the-box implementations are available for JDK dynamic proxies and for CGLIB proxies, as applied by DefaultAopProxyFactory. * * public interface AopProxy { * Object getProxy(); * Object getProxy(@Nullable ClassLoader classLoader); * } * * * org.springframework.aop.framework.JdkDynamicAopProxy * JDK-based AopProxy implementation for the Spring AOP framework, based on JDK java.lang.reflect.Proxy dynamic proxies. 基于JDK动态代理实现; * Objects of this type should be obtained through proxy factories, configured by an AdvisedSupport class. 该类型的代理对象 应该由工厂提供,工厂由AdvisedSupport提供支持; * * final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializable { * * private final AdvisedSupport advised; * * public Object getProxy() { * return getProxy(ClassUtils.getDefaultClassLoader()); * } * public Object getProxy(@Nullable ClassLoader classLoader) { * ... * return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this); * } * * public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {} * } * * * org.springframework.aop.framework.CglibAopProxy * CGLIB-based AopProxy implementation for the Spring AOP framework. 基于CGLB的AOP代理实现; * Objects of this type should be obtained through proxy factories, configured by an AdvisedSupport object. 该类型的代理对象 应该由工厂提供,工厂由AdvisedSupport提供支持; * DefaultAopProxyFactory will automatically create CGLIB-based proxies if necessary, for example in case of proxying a target class. 如果需要创建代理对象,DefaultAopProxyFactory将会创建CGLB代理对象; * * class CglibAopProxy implements AopProxy, Serializable { * * protected final AdvisedSupport advised; * * public Object getProxy() { * return getProxy(null); * } * public Object getProxy(@Nullable ClassLoader classLoader) { * * } * * private static class DynamicAdvisedInterceptor implements MethodInterceptor, Serializable { * public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {} * } * ... * } * * * * org.springframework.aop.framework.AopProxyFactory * Interface to be implemented by factories that are able to create AOP proxies based on {@link AdvisedSupport} configuration objects. 被代理工厂类实现 能够 基于AdvisedSupport 创建AOP代理类; * * public interface AopProxyFactory { * AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException; * } * * org.springframework.aop.framework.DefaultAopProxyFactory * Default {@link AopProxyFactory} implementation, creating either a CGLIB proxy or a JDK dynamic proxy. 默认的AopProxyFactory实现; * * public class DefaultAopProxyFactory implements AopProxyFactory, Serializable { * public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException { * if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) { * Class<?> targetClass = config.getTargetClass(); * if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) { * return new JdkDynamicAopProxy(config); * } * return new ObjenesisCglibAopProxy(config); * } * else{ * return new JdkDynamicAopProxy(config); * } * } * } * * org.springframework.aop.framework.ProxyCreatorSupport * Base class for proxy factories. 代理工厂的基类; * Provides convenient access to a configurable AopProxyFactory. 提供便捷的方式访问AopProxyFactory; * * public class ProxyCreatorSupport extends AdvisedSupport { * * private AopProxyFactory aopProxyFactory; * * public AopProxyFactory getAopProxyFactory() { * return this.aopProxyFactory; * } * * protected final synchronized AopProxy createAopProxy() { * ... * return getAopProxyFactory().createAopProxy(this); * } * } * * org.springframework.aop.framework.ProxyFactory * public class ProxyFactory extends ProxyCreatorSupport { * * public Object getProxy(@Nullable ClassLoader classLoader) { * return createAopProxy().getProxy(classLoader); ---createAopProxy() :使用DefaultAopProxyFactory创建对应的AopProxy; * ProxyFactory.getProxy * -> ProxyCreatorSupport.createAopProxy * -> ProxyCreatorSupport.getAopProxyFactory * -> AopProxyFactory.createAopProxy * -> DefaultAopProxyFactory.createAopProxy * ---getProxy(classLoader) : 不同的AopProxy创建不同的AOP代理对象; * * } * } * * * 实现思路:BeanPostProcessor#postProcessAfterInitialization * * org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean * * -> * * org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsAfterInitialization * * -> * * 不同的BeanPostProcessor有不同的实现 * eg: * BeanPostProcessor : org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator * @Aspect * org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#postProcessAfterInitialization{ * * ... * return wrapIfNecessary(bean, beanName, cacheKey); * ... * * } * * protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) { * ... * if (specificInterceptors != DO_NOT_PROXY) { * ... * Object proxy = createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean)); * ... * } * ... * } * * protected Object createProxy(Class<?> beanClass, @Nullable String beanName, @Nullable Object[] specificInterceptors, TargetSource targetSource) { * ... * ProxyFactory proxyFactory = new ProxyFactory(); * ... * proxyFactory.addAdvisors(advisors); * ... * return proxyFactory.getProxy(getProxyClassLoader()); * } * * 事务 * org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#postProcessAfterInitialization * * * BeanPostProcessor : org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor * @Async * org.springframework.aop.framework.AbstractAdvisingBeanPostProcessor#postProcessAfterInitialization{ * ... * if (isEligible(bean, beanName)) { * ProxyFactory proxyFactory = prepareProxyFactory(bean, beanName); * ... * proxyFactory.addAdvisor(this.advisor); * ... * return proxyFactory.getProxy(getProxyClassLoader()); * } * } * * */