当前位置: 首页 > news >正文

AOP配置类自动注入

        本文主要探究AopAutoConfiguration配置类里面的bean怎么被自动装配的。代码如下:

package com.example.springdemo.demos.a05;import com.example.springdemo.demos.a04.Bean1;
import com.example.springdemo.demos.a04.Bean2;
import com.example.springdemo.demos.a04.TestAutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.aop.AopAutoConfiguration;
import org.springframework.context.annotation.*;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.env.SimpleCommandLinePropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.type.AnnotationMetadata;import java.util.List;/*** @author zhou* @version 1.0* @description TODO* @date 2025/8/17 22:12*/
public class TestAopAuto {public static void main(String[] args) {GenericApplicationContext context = new GenericApplicationContext();/*StandardEnvironment env = new StandardEnvironment();env.getPropertySources().addLast(new SimpleCommandLinePropertySource("--spring.aop.auto=false"));context.setEnvironment(env);*/AnnotationConfigUtils.registerAnnotationConfigProcessors(context.getDefaultListableBeanFactory());context.registerBean(Config.class);context.refresh();for (String name : context.getBeanDefinitionNames()){System.out.println(name);}}@Configuration //本项目配置类@Import({MyImportSelector.class})static class Config{}static class MyImportSelector implements DeferredImportSelector {@Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {return new String[]{AopAutoConfiguration.class.getName()};}}
}

输出结果:

D:\Java2\jdk1.8.0_131\bin\java.exe "-javaagent:D:\idea2\IntelliJ IDEA 2022.3.2\lib\idea_rt.jar=53704:D:\idea2\IntelliJ IDEA 2022.3.2\bin" -Dfile.encoding=UTF-8 -classpath D:\Java2\jdk1.8.0_131\jre\lib\charsets.jar;D:\Java2\jdk1.8.0_131\jre\lib\deploy.jar;D:\Java2\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;D:\Java2\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;D:\Java2\jdk1.8.0_131\jre\lib\ext\dnsns.jar;D:\Java2\jdk1.8.0_131\jre\lib\ext\jaccess.jar;D:\Java2\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;D:\Java2\jdk1.8.0_131\jre\lib\ext\localedata.jar;D:\Java2\jdk1.8.0_131\jre\lib\ext\nashorn.jar;D:\Java2\jdk1.8.0_131\jre\lib\ext\sunec.jar;D:\Java2\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;D:\Java2\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;D:\Java2\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;D:\Java2\jdk1.8.0_131\jre\lib\ext\zipfs.jar;D:\Java2\jdk1.8.0_131\jre\lib\javaws.jar;D:\Java2\jdk1.8.0_131\jre\lib\jce.jar;D:\Java2\jdk1.8.0_131\jre\lib\jfr.jar;D:\Java2\jdk1.8.0_131\jre\lib\jfxswt.jar;D:\Java2\jdk1.8.0_131\jre\lib\jsse.jar;D:\Java2\jdk1.8.0_131\jre\lib\management-agent.jar;D:\Java2\jdk1.8.0_131\jre\lib\plugin.jar;D:\Java2\jdk1.8.0_131\jre\lib\resources.jar;D:\Java2\jdk1.8.0_131\jre\lib\rt.jar;D:\javacode\springDemo\target\classes;C:\Users\zhou\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.6.13\spring-boot-starter-web-2.6.13.jar;C:\Users\zhou\.m2\repository\org\springframework\boot\spring-boot-starter\2.6.13\spring-boot-starter-2.6.13.jar;C:\Users\zhou\.m2\repository\org\springframework\boot\spring-boot\2.6.13\spring-boot-2.6.13.jar;C:\Users\zhou\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.6.13\spring-boot-autoconfigure-2.6.13.jar;C:\Users\zhou\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.6.13\spring-boot-starter-logging-2.6.13.jar;C:\Users\zhou\.m2\repository\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;C:\Users\zhou\.m2\repository\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;C:\Users\zhou\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;C:\Users\zhou\.m2\repository\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;C:\Users\zhou\.m2\repository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;C:\Users\zhou\.m2\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\Users\zhou\.m2\repository\org\yaml\snakeyaml\1.29\snakeyaml-1.29.jar;C:\Users\zhou\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.6.13\spring-boot-starter-json-2.6.13.jar;C:\Users\zhou\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.13.4.2\jackson-databind-2.13.4.2.jar;C:\Users\zhou\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.13.4\jackson-annotations-2.13.4.jar;C:\Users\zhou\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.13.4\jackson-core-2.13.4.jar;C:\Users\zhou\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.4\jackson-datatype-jdk8-2.13.4.jar;C:\Users\zhou\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.4\jackson-datatype-jsr310-2.13.4.jar;C:\Users\zhou\.m2\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.4\jackson-module-parameter-names-2.13.4.jar;C:\Users\zhou\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\2.6.13\spring-boot-starter-tomcat-2.6.13.jar;C:\Users\zhou\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.68\tomcat-embed-core-9.0.68.jar;C:\Users\zhou\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.68\tomcat-embed-el-9.0.68.jar;C:\Users\zhou\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.68\tomcat-embed-websocket-9.0.68.jar;C:\Users\zhou\.m2\repository\org\springframework\spring-web\5.3.23\spring-web-5.3.23.jar;C:\Users\zhou\.m2\repository\org\springframework\spring-beans\5.3.23\spring-beans-5.3.23.jar;C:\Users\zhou\.m2\repository\org\springframework\spring-webmvc\5.3.23\spring-webmvc-5.3.23.jar;C:\Users\zhou\.m2\repository\org\springframework\spring-aop\5.3.23\spring-aop-5.3.23.jar;C:\Users\zhou\.m2\repository\org\springframework\spring-context\5.3.23\spring-context-5.3.23.jar;C:\Users\zhou\.m2\repository\org\springframework\spring-expression\5.3.23\spring-expression-5.3.23.jar;C:\Users\zhou\.m2\repository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;C:\Users\zhou\.m2\repository\org\springframework\spring-core\5.3.23\spring-core-5.3.23.jar;C:\Users\zhou\.m2\repository\org\springframework\spring-jcl\5.3.23\spring-jcl-5.3.23.jar com.example.springdemo.demos.a05.TestAopAuto
22:35:54.151 [main] DEBUG org.springframework.context.support.GenericApplicationContext - Refreshing org.springframework.context.support.GenericApplicationContext@4361bd48
22:35:54.191 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
22:35:54.438 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
22:35:54.440 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'forceAutoProxyCreatorToUseClassProxying'
22:35:54.442 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
22:35:54.447 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
22:35:54.448 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
22:35:54.452 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.aop.config.internalAutoProxyCreator'
22:35:54.521 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'com.example.springdemo.demos.a05.TestAopAuto$Config'
22:35:54.524 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.boot.autoconfigure.aop.AopAutoConfiguration$ClassProxyingConfiguration'
22:35:54.525 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.boot.autoconfigure.aop.AopAutoConfiguration'
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
com.example.springdemo.demos.a05.TestAopAuto$Config
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration$ClassProxyingConfiguration
forceAutoProxyCreatorToUseClassProxying
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration
org.springframework.aop.config.internalAutoProxyCreatorProcess finished with exit code 0

       可以看到,com.example.springdemo.demos.a05.TestAopAuto$Config之下的类都是通过aop自动配置类注入的bean。之上的是通过注入bean工厂后处理器自带的bean。

 进入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.config.BeanFactoryPostProcessor;
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.Bean;
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() {}@Beanstatic BeanFactoryPostProcessor forceAutoProxyCreatorToUseClassProxying() {return (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")static class JdkDynamicAutoProxyConfiguration {JdkDynamicAutoProxyConfiguration() {}}}
}

注意看上面的条件

@ConditionalOnProperty(prefix = "spring.aop",name = {"auto"},havingValue = "true",matchIfMissing = true
)

只要当下面条件之一成立便可加载AopAutoConfiguration里面的配置的bean

1)配置文件里面有spring.aop开头的属性,名字为auto,值为ture

2)上面配置缺失

现在我们手动在main方法里面把spring.aop.auto设置为false,便不会加载里面的bean

测试结果如下:

跟之前对比少了四个bean。

http://www.xdnf.cn/news/1316755.html

相关文章:

  • Git安装使用
  • Java增强for循环(小白友好版)
  • 整体设计 之“凝聚式中心点”原型 --整除:智能合约和DBMS的深层联合 之1
  • 【R语言】R语言矩阵运算:矩阵乘除法与逐元素乘除法计算对比
  • 7 索引的监控
  • 一文读懂[特殊字符] LlamaFactory 中 Loss 曲线图
  • JavaScript字符串详解
  • 图解希尔排序C语言实现
  • 力扣 hot100 Day76
  • Java 基础 -- Java 基础知识
  • C语言---第一个C语言程序
  • FreeRTOS源码分析八:timer管理(一)
  • 基于遗传编程的自动程序生成
  • Java语法进阶之常用类
  • SQL Server 2019安装教程(超详细图文)
  • PERCEIVER IO:一种用于结构化输入与输出的通用架构
  • iSCSI服务配置全指南(含服务器与客户端)
  • 快速掌握Hardhat与Solidity智能合约开发
  • SCAI采用公平发射机制成功登陆LetsBonk,60%代币供应量已锁仓
  • Houdini 粒子学习笔记
  • C# Newtonsoft.Json 反序列化子类数据丢失问题
  • 音频分类标注工具
  • 矿物分类案列 (一)六种方法对数据的填充
  • Java零基础笔记20(Java高级技术:单元测试、反射、注解、动态代理)
  • RAC环境redo在各节点本地导致数据库故障恢复---惜分飞
  • 勾股数-洛谷B3845 [GESP样题 二级]
  • 平行双目视觉-动手学计算机视觉18
  • Linux应用软件编程---多任务(线程)(线程创建、消亡、回收、属性、与进程的区别、线程间通信、函数指针)
  • (一)React企业级后台(Axios/localstorage封装/动态侧边栏)
  • Android 对话框 - 基础对话框补充(不同的上下文创建 AlertDialog、AlertDialog 的三个按钮)