Spring AOP 基于注解源码整理

导入配置类

  1. EnableAspectJAutoProxy 注解导入 AspectJAutoProxyRegistrar

  2. ImportBeanDefinitionRegistrar#registerBeanDefinitions向容器中加入AnnotationAwareAspectJAutoProxyCreator

  3. AnnotationAwareAspectJAutoProxyCreator#initBeanFactory初始化ReflectiveAspectJAdvisorFactory

    text 复制代码
    protected void initBeanFactory(ConfigurableListableBeanFactory beanFactory) {
        super.initBeanFactory(beanFactory);
        if (this.aspectJAdvisorFactory == null) {
            this.aspectJAdvisorFactory = new ReflectiveAspectJAdvisorFactory(beanFactory);
        }
        this.aspectJAdvisorsBuilder =
                new BeanFactoryAspectJAdvisorsBuilderAdapter(beanFactory, this.aspectJAdvisorFactory);
    }

解析出所有的Advisor对象

  1. AbstractAutoProxyCreator#postProcessBeforeInstantiation

    text 复制代码
     public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
         Object cacheKey = getCacheKey(beanClass, beanName);
         if (!StringUtils.hasLength(beanName) || !this.targetSourcedBeans.contains(beanName)) {
             if (this.advisedBeans.containsKey(cacheKey)) {
                 return null;
             }
             if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
                 this.advisedBeans.put(cacheKey, Boolean.FALSE);
                 return null;
             }
         }
         return null ;
     }
  2. AspectJAwareAdvisorAutoProxyCreator#shouldSkip

  3. AnnotationAwareAspectJAutoProxyCreator#findCandidateAdvisors

  4. BeanFactoryAspectJAdvisorsBuilder#buildAspectJAdvisors

  5. 获取Spring容器中被Aspect注解的bean, 通过ReflectiveAspectJAdvisorFactory#getAdvisors获取Advisor

    text 复制代码
    MetadataAwareAspectInstanceFactory factory =
        new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName);
    List<Advisor> classAdvisors = this.advisorFactory.getAdvisors(factory);
  6. ReflectiveAspectJAdvisorFactory#getAdvisors(factory)

    text 复制代码
    public List<Advisor> getAdvisors(MetadataAwareAspectInstanceFactory aspectInstanceFactory) {
        Class<?> aspectClass = aspectInstanceFactory.getAspectMetadata().getAspectClass();
        String aspectName = aspectInstanceFactory.getAspectMetadata().getAspectName();
        MetadataAwareAspectInstanceFactory lazySingletonAspectInstanceFactory =
                 new LazySingletonAspectInstanceFactoryDecorator(aspectInstanceFactory);
        List<Advisor> advisors = new ArrayList<>();
        for (Method method : getAdvisorMethods(aspectClass)) {
             Advisor advisor = getAdvisor(method, lazySingletonAspectInstanceFactory, 0, aspectName);
             if (advisor != null) {
                 advisors.add(advisor);
             }
        }
    }

生产代理对象

  1. AbstractAutoProxyCreator#postProcessAfterInitialization

    text 复制代码
     public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {
         Object cacheKey = getCacheKey(bean.getClass(), beanName);
         if (this.earlyProxyReferences.remove(cacheKey) != bean) {
             return wrapIfNecessary(bean, beanName, cacheKey);
         }
         return bean;
     }
  2. AbstractAutoProxyCreator#wrapIfNecessary

    text 复制代码
    protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
         // Create proxy if we have advice.
         Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
         this.advisedBeans.put(cacheKey, Boolean.TRUE);
         Object proxy = createProxy(
             bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
         this.proxyTypes.put(cacheKey, proxy.getClass());
         return proxy;
    }
获取符合条件的Advisor集合
  1. AbstractAdvisorAutoProxyCreator#getAdvicesAndAdvisorsForBean

    text 复制代码
    protected Object[] getAdvicesAndAdvisorsForBean(
             Class<?> beanClass, String beanName, @Nullable TargetSource targetSource) {
        List<Advisor> advisors = findEligibleAdvisors(beanClass, beanName);
        if (advisors.isEmpty()) {
            return DO_NOT_PROXY;
        }
        return advisors.toArray();
    }
  2. AbstractAdvisorAutoProxyCreator#findEligibleAdvisors

    text 复制代码
    protected List<Advisor> findEligibleAdvisors(Class<?> beanClass, String beanName) {
         List<Advisor> candidateAdvisors = findCandidateAdvisors();
         List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName);
         extendAdvisors(eligibleAdvisors);
         if (!eligibleAdvisors.isEmpty()) {
             eligibleAdvisors = sortAdvisors(eligibleAdvisors);
         }
         return eligibleAdvisors;
    }
  3. AbstractAdvisorAutoProxyCreator#findAdvisorsThatCanApply

    text 复制代码
    protected List<Advisor> findAdvisorsThatCanApply(
             List<Advisor> candidateAdvisors, Class<?> beanClass, String beanName) {
        ProxyCreationContext.setCurrentProxiedBeanName(beanName);
        try {
            return AopUtils.findAdvisorsThatCanApply(candidateAdvisors, beanClass);
        }finally {
            ProxyCreationContext.setCurrentProxiedBeanName(null);
        }
    }
创建代理对象
  1. AbstractAdvisorAutoProxyCreator#createProxy
相关推荐
心灵宝贝19 分钟前
IDEA 安装 SpotBugs 插件超简单教程
java·macos·intellij-idea
幼稚诠释青春23 分钟前
Java学习笔记(对象)
java·开发语言
你怎么知道我是队长30 分钟前
Go语言标识符
后端·golang
小羊学伽瓦1 小时前
【Java基础】——JVM
java·jvm
老任与码1 小时前
Spring AI(2)—— 发送消息的API
java·人工智能·spring ai
*.✧屠苏隐遥(ノ◕ヮ◕)ノ*.✧1 小时前
MyBatis快速入门——实操
java·spring boot·spring·intellij-idea·mybatis·intellij idea
csdn_freak_dd1 小时前
查看单元测试覆盖率
java·单元测试
爱吃烤鸡翅的酸菜鱼1 小时前
【SpringMVC】详解cookie,session及实战
java·http·java-ee·intellij-idea
Wyc724091 小时前
JDBC:java与数据库连接,Maven,MyBatis
java·开发语言·数据库
老任与码2 小时前
Spring AI(3)——Chat Memory
java·人工智能·spring ai