Spring系列.AOP原理简析 (2)

下面是创建动态代理类的关键代码。

@Override public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) { if (bean != null) { Object cacheKey = getCacheKey(bean.getClass(), beanName); if (this.earlyProxyReferences.remove(cacheKey) != bean) { //这边是创建代码类的关键代码 return wrapIfNecessary(bean, beanName, cacheKey); } } return bean; } protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) { if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) { return bean; } if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) { return bean; } if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) { this.advisedBeans.put(cacheKey, Boolean.FALSE); return bean; } // Create proxy if we have advice. //获取当前Bean配置的advice,这步是关键 Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null); if (specificInterceptors != DO_NOT_PROXY) { this.advisedBeans.put(cacheKey, Boolean.TRUE); //创建代理 Object proxy = createProxy( bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean)); this.proxyTypes.put(cacheKey, proxy.getClass()); return proxy; } this.advisedBeans.put(cacheKey, Boolean.FALSE); return bean; }

层层dedug进去我们能看到下面这段代码,我们口中常说的JDK动态代理和Cglib动态代理就是在这边生成的。

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) { throw new AopConfigException("TargetSource cannot determine target class: " + "Either an interface or a target is required for proxy creation."); } if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) { //生成JDK动态代理 return new JdkDynamicAopProxy(config); } //生成Cglib动态代理 return new ObjenesisCglibAopProxy(config); } else { return new JdkDynamicAopProxy(config); } } /** * Determine whether the supplied {@link AdvisedSupport} has only the * {@link org.springframework.aop.SpringProxy} interface specified * (or no proxy interfaces specified at all). */ private boolean hasNoUserSuppliedProxyInterfaces(AdvisedSupport config) { Class<?>[] ifcs = config.getProxiedInterfaces(); return (ifcs.length == 0 || (ifcs.length == 1 && SpringProxy.class.isAssignableFrom(ifcs[0]))); }

到此,我们已经简单分析了Spring动态代理类的生成流程。

PS:关于InstantiationAwareBeanPostProcessor接口和BeanPostProcessor接口大家可以自行了解下,这两个接口是Spring中非常重要的接口。看懂了这两个接口,Spring很多“神秘”的功能你就能理解了。

简单总结

通过上面分析,其实我们发现如果不去看AOP动态代理类生成的细节的话,整个Spring AOP的流程还是挺简单的:

@EnableAspectJAutoProxy注解通过AopConfigUtils这个工具类注册AnnotationAwareAspectJAutoProxyCreator这个类,这个类实现了InstantiationAwareBeanPostProcessor接口,所以会在Bean实例化前后对Bean做一系列额外的操作;

AnnotationAwareAspectJAutoProxyCreator的postProcessAfterInitialization中会找出所有和当前Bean相关的Advice,如果找到就创建相应的动态代理类,如果找不到就不生成,返回原始类。

所以整个大流程就这么简单。

一些重要类:

@EnableAspectJAutoProxy;

AspectJAutoProxyRegistrar:注册AnnotationAwareAspectJAutoProxyCreator

AnnotationAwareAspectJAutoProxyCreator:AOP动态代理自动生成的处理类,其他类似的类有AspectJAwareAdvisorAutoProxyCreator和InfrastructureAdvisorAutoProxyCreator等;

AopConfigUtils:AOP配置工具类

ProxyFactory:代理工厂

AopProxy接口:常见实现类ObjenesisCglibAopProxy、JdkDynamicAopProxy

参考

https://blog.csdn.net/zhoushimiao1990/article/details/89853368

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpxxfd.html