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可以插入切面逻辑,然后再调用父类的实际方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | /** * 【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()); * } * } * * */ |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)