0
点赞
收藏
分享

微信扫一扫

Spring AOP核心源码解读

上一篇 <<<Spring AOP通知责任链模式图解
下一篇 >>>Spring事务常识汇总


回顾bean容器的初始化过程

核心源码

1、启动@EnableAspectJAutoProxy时,会往IOC容器中注入AnnotationAwareAspectJAutoProxyCreator.class

@EnableAspectJAutoProxy
    @Import({AspectJAutoProxyRegistrar.class})
        AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);
            registerOrEscalateApcAsRequired(AnnotationAwareAspectJAutoProxyCreator.class, registry, source);
            registry.registerBeanDefinition("org.springframework.aop.config.internalAutoProxyCreator", beanDefinition);
beanId:org.springframework.aop.config.internalAutoProxyCreator
class:AnnotationAwareAspectJAutoProxyCreator.class

2、AnnotationAwareAspectJAutoProxyCreator.class类图 最终属于BeanPostProcessor的子类

>>>>AspectJAwareAdvisorAutoProxyCreator
>>>>>>>>AbstractAdvisorAutoProxyCreator
>>>>>>>>>>>>AbstractAutoProxyCreator extends ProxyProcessorSupport implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware

SmartInstantiationAwareBeanPostProcessor.class
>>>>InstantiationAwareBeanPostProcessor extends BeanPostProcessor

3、BeanPostProcessor 拥有前置和后置通知,可看到子类AbstractAutoProxyCreator,可以看到AOP是后置代理起到了作用

public Object postProcessBeforeInitialization(Object bean, String beanName) {
    return bean;
}

public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) throws BeansException {
        if (bean != null) {
            Object cacheKey = this.getCacheKey(bean.getClass(), beanName);
            if (!this.earlyProxyReferences.contains(cacheKey)) {
                return this.wrapIfNecessary(bean, beanName, cacheKey);
            }
        }

        return bean;
    }

4、this.wrapIfNecessary(bean, beanName, cacheKey);

//判断是否需要增加
protected boolean isInfrastructureClass(Class<?> beanClass) {
        boolean retVal = Advice.class.isAssignableFrom(beanClass) || Pointcut.class.isAssignableFrom(beanClass) || Advisor.class.isAssignableFrom(beanClass) || AopInfrastructureBean.class.isAssignableFrom(beanClass);
        if (retVal && this.logger.isTraceEnabled()) {
            this.logger.trace("Did not attempt to auto-proxy infrastructure class [" + beanClass.getName() + "]");
        }

        return retVal;
    }
Object[] specificInterceptors = this.getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, (TargetSource)null);
//需要增强的话,则创建JDK动态代理或cglib动态代理
    Object proxy = this.createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
        proxyFactory.getProxy(this.getProxyClassLoader());
            this.createAopProxy().getProxy(classLoader);
            //如果请求是接口,则调用JdkDynamicAopProxy,否则调用ObjenesisCglibAopProxy
            public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
            if (!config.isOptimize() && !config.isProxyTargetClass() && !this.hasNoUserSuppliedProxyInterfaces(config)) {
                return new JdkDynamicAopProxy(config);
            } else {
                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.");
                } else {
                    return (AopProxy)(!targetClass.isInterface() && !Proxy.isProxyClass(targetClass) ? new ObjenesisCglibAopProxy(config) : new JdkDynamicAopProxy(config));
                }
            }
        }

推荐阅读:
<<<Spring IOC的初始化原理
<<<SpringBean的生命周期流程图
<<<SpringBean单例情况下解决循环依赖的原理
<<<Spring AOP的底层原理
<<<Spring AOP通知责任链模式图解
<<<Spring事务常识汇总
<<<Spring声明事务原理及核心源码分析
<<<手动事务和自定义注解事务代码参考
<<<Spring常用注解汇总

举报

相关推荐

0 条评论