AopAutoConfiguration源码阅读

代码如下:

复制代码
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.springframework.boot.autoconfigure.aop;

import org.aspectj.weaver.Advice;
import org.springframework.aop.config.AopConfigUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration(
    proxyBeanMethods = false
)
@ConditionalOnProperty(
    prefix = "spring.aop",
    name = {"auto"},
    havingValue = "true",
    matchIfMissing = true
)
public class AopAutoConfiguration {
    public AopAutoConfiguration() {
    }

    @Configuration(
        proxyBeanMethods = false
    )
    @ConditionalOnMissingClass({"org.aspectj.weaver.Advice"})
    @ConditionalOnProperty(
        prefix = "spring.aop",
        name = {"proxy-target-class"},
        havingValue = "true",
        matchIfMissing = true
    )
    static class ClassProxyingConfiguration {
        ClassProxyingConfiguration(BeanFactory beanFactory) {
            if (beanFactory instanceof BeanDefinitionRegistry) {
                BeanDefinitionRegistry registry = (BeanDefinitionRegistry)beanFactory;
                AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry);
                AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
            }

        }
    }

    @Configuration(
        proxyBeanMethods = false
    )
    @ConditionalOnClass({Advice.class})
    static class AspectJAutoProxyingConfiguration {
        AspectJAutoProxyingConfiguration() {
        }

        @Configuration(
            proxyBeanMethods = false
        )
        @EnableAspectJAutoProxy(
            proxyTargetClass = true
        )
        @ConditionalOnProperty(
            prefix = "spring.aop",
            name = {"proxy-target-class"},
            havingValue = "true",
            matchIfMissing = true
        )
        static class CglibAutoProxyConfiguration {
            CglibAutoProxyConfiguration() {
            }
        }

        @Configuration(
            proxyBeanMethods = false
        )
        @EnableAspectJAutoProxy(
            proxyTargetClass = false
        )
        @ConditionalOnProperty(
            prefix = "spring.aop",
            name = {"proxy-target-class"},
            havingValue = "false",
            matchIfMissing = false
        )
        static class JdkDynamicAutoProxyConfiguration {
            JdkDynamicAutoProxyConfiguration() {
            }
        }
    }
}

可以看到里面类似if-else的语法判断,决定创建哪个bean。

第一个分支决定ClassProxyingConfiguration的创建(相当于if)

第二个分支决定AspectJAutoProxyingConfiguration的创建(相当于else)

由于Advice类存在,它是aop的核心类,springBoot已经整合,所以走了第二个分支,第一个分支的@ConditionalOnMissingClass({"org.aspectj.weaver.Advice"})条件不成立

上面的类存在,所以进入AspectJAutoProxyingConfiguration类,里面又有类似的条件判断

复制代码
 @Configuration(
        proxyBeanMethods = false
    )
    @ConditionalOnClass({Advice.class})
    static class AspectJAutoProxyingConfiguration {
        AspectJAutoProxyingConfiguration() {
        }

        @Configuration(
            proxyBeanMethods = false
        )
        @EnableAspectJAutoProxy(
            proxyTargetClass = true
        )
        @ConditionalOnProperty(
            prefix = "spring.aop",
            name = {"proxy-target-class"},
            havingValue = "true",
            matchIfMissing = true
        )
        static class CglibAutoProxyConfiguration {
            CglibAutoProxyConfiguration() {
            }
        }

        @Configuration(
            proxyBeanMethods = false
        )
        @EnableAspectJAutoProxy(
            proxyTargetClass = false
        )
        @ConditionalOnProperty(
            prefix = "spring.aop",
            name = {"proxy-target-class"},
            havingValue = "false",
            matchIfMissing = false
        )
        static class JdkDynamicAutoProxyConfiguration {
            JdkDynamicAutoProxyConfiguration() {
            }
        }
    }

对比两个配置类CglibAutoProxyConfiguration和JdkDynamicAutoProxyConfiguration上的条件。

1.CglibAutoProxyConfiguration类上的注解

1)proxy-target-class配置值为true

2)上面配置不存在

当上面两个一个成立便会执行。

3)@EnableAspectJAutoProxy( proxyTargetClass = true )注解

启用AspectJ的自动代理功能,允许使用AspectJ风格的切面编程。设置为true时,强制使用CGLIB代理而不是JDK动态代理,确保即使目标类没有实现接口也能正常创建代理对象,默认值为false,表示优先使用JDK动态代理

4)@Configuration( proxyBeanMethods = false )

这个属性控制Spring是否为@Configuration类创建CGLIB代理:

proxyBeanMethods = false:不创建代理,提升性能

2.JdkDynamicAutoProxyConfiguration类上的注解

1)proxy-target-class配置值为fasle,配置不存在则不匹配

上面条件成立才会走这个逻辑。

2) @EnableAspectJAutoProxy( proxyTargetClass = false )注解

proxyTargetClass 指定代理方式,false表示使用JDK动态代理(基于接口)

3)@Configuration( proxyBeanMethods = false )

不创建CGLIB代理,提升性能

由于配置缺少,最后选择了创建CglibAutoProxyConfiguration类的逻辑。其中CglibAutoProxyConfiguration类上的@EnableAspectJAutoProxy注解还会继续注入bean。

点开@EnableAspectJAutoProxy注解

复制代码
    在进入导入类AspectJAutoProxyRegistrar里面
复制代码
	/**
	 * Register, escalate, and configure the AspectJ auto proxy creator based on the value
	 * of the @{@link EnableAspectJAutoProxy#proxyTargetClass()} attribute on the importing
	 * {@code @Configuration} class.
	 * 
	 * @param importingClassMetadata 导入该配置类的元数据信息,包含注解配置信息
	 * @param registry Bean定义注册器,用于注册和管理Bean定义
	 */
	@Override
	public void registerBeanDefinitions(
			AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

		// 注册AspectJ注解自动代理创建器
		AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);

		// 获取@EnableAspectJAutoProxy注解的属性配置
		AnnotationAttributes enableAspectJAutoProxy =
				AnnotationConfigUtils.attributesFor(importingClassMetadata, EnableAspectJAutoProxy.class);
		if (enableAspectJAutoProxy != null) {
			// 根据proxyTargetClass属性决定是否使用CGLIB代理
			if (enableAspectJAutoProxy.getBoolean("proxyTargetClass")) {
				AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
			}
			// 根据exposeProxy属性决定是否暴露代理对象
			if (enableAspectJAutoProxy.getBoolean("exposeProxy")) {
				AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);
			}
		}
	}
复制代码
AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);

这段代码决定org.springframework.aop.config.internalAutoProxyCreator类的创建。

相关推荐
小羊没烦恼!2 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
俊昭喜喜里2 天前
java中的继承和多态的区别
java
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
譕痕2 天前
JSONObject与JSONArray封装数据格式区别
java·json
胡写代码2 天前
别再前后端各写一套表单校验了
java·后端
小鱼能吃糖2 天前
缺陷修复总览 · mall电商项目:5类缺陷,1个病根,4个业务域
java·电商
此时不提桶,更待何时2 天前
01-06-A-JVM排查实战详解
java·jvm
vipxieliang2 天前
ValidX 在 DDD 领域驱动设计中的实践
java·spring boot
ba_pi2 天前
mysql 查询所有表名并授权
java
ProcessOn官方账号2 天前
java函数式编程--入门基础
java·编程·函数式编程