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类的创建。

相关推荐
手握风云-8 分钟前
JavaEE 进阶第十三期:Spring Ioc & DI,从会用容器到成为容器(下)
java·spring·java-ee
组合缺一11 分钟前
论 AI Skills 分布式发展的必然性:从单体智能到“云端大脑”的跃迁
java·人工智能·分布式·llm·mcp·skills
砚边数影11 分钟前
决策树原理(一):信息增益与特征选择 —— Java 实现 ID3 算法
java·数据库·决策树·机器学习·kingbase·数据库平替用金仓·金仓数据库
让我上个超影吧15 分钟前
天机学堂——BitMap实现签到
java·数据库·spring boot·redis·spring cloud
迷路爸爸18016 分钟前
无sudo权限远程连接Ubuntu服务器安装TeX Live实操记录(适配VS Code+LaTeX Workshop,含路径选择与卸载方案)
java·服务器·ubuntu·latex
有梦想的攻城狮28 分钟前
maven中的os-maven-plugin插件的使用
java·maven·maven插件·os-maven-plugin·classifer
Carry灭霸35 分钟前
【BUG】Redisson Connection refused 127.0.0.1
java·redis
消失的旧时光-194339 分钟前
第九课实战版:异常与日志体系 —— 后端稳定性的第一道防线
java·后端
钦拆大仁41 分钟前
Java设计模式-状态模式
java·设计模式·状态模式
人道领域44 分钟前
javaWeb从入门到进阶(SpringBoot基础案例2)
java·开发语言·mybatis