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

相关推荐
炸薯条!1 分钟前
二叉树的链式表示(2)
java·数据结构·算法
徐寿春13 分钟前
什么是数据倾斜
java·guava
李白的天不白21 分钟前
一个服务器可以搭建多个网站
java·tomcat
●VON21 分钟前
AtomGit Flutter鸿蒙客户端:共享组件
java·flutter·华为·harmonyos·鸿蒙
程序猿乐锅22 分钟前
【JAVASE | 第十七篇】Java 网络通信
java·开发语言
执于代码23 分钟前
Java交互打印的问题
java
我命由我1234535 分钟前
Windows 操作系统 - Windows 查看防火墙是否开启、Windows 查看防火墙放行端口
java·运维·开发语言·windows·java-ee·操作系统·运维开发
fly spider36 分钟前
Spring 原理总览:从启动到请求执行
java·数据库·spring
大大杰哥1 小时前
SSeEmitter的基本使用和介绍
java·sse·通信
闪电悠米1 小时前
黑马点评-Redis 消息队列-02_list_pubsub_limits
java·数据库·ide·redis·缓存·list·intellij-idea