[SpringBoot] An understanding of the principle of automatic configuration and a detailed summary of automatic configuration classes and configuration information classes

1. Annotation

(1) @EnableAutoConfiguration

		@EnableAutoConfiguration 注解:启用自动化配置功能

(2) @AutoConfigurationPackage

     @AutoConfigurationPackage   添加该注解的类所在的package 作为 自动配置package 进行管理

(3) @Import

@Import 注解:相对于@Bean 注解,使用@Import 注解可以更便捷的将一个类加入 IOC 容器。

(4) @EnableConfigurationPropertie

@EnableConfigurationPropertie 注解 将配置信息类加入IOC容器中 通常的作用使标注了@ConfigurationProperties的配置信息类生效

(5) @ConfigurationProperties

@ConfigurationProperties注解 将配置文件中的信息与配置类中对应的属性进行绑定

(6) @Conditiona

@Conditional 条件注解(注意先后顺序!!)一个类在满足特定条件时才加入 IOC 容器:

insert image description here

2. Related classes

2.1 XxxxAutoConfiguration

XxxxAutoConfiguration类的含义是:自动配置类,目的是给容器中添加组件

2.2 XxxxProperties

XxxxProperties类的含义是:封装配置文件中相关属性

2.3 Example

insert image description here
insert image description here

3. Automatic configuration process

1.Spring Boot启动的时候会通过@EnableAutoConfiguration注解找到META-INF/spring.factories配置文件

2.找到配置文件中key为EnableAutoConfiguration的value,这些值都是自动配置类的全限定类名,然后对其进行加载,中间会经过排除(使用者排除掉的自动配置类)和过滤(不满足条件注解所要求的自动配置类)

3.这些自动配置类都是以xxxAutoConfiguration结尾来命名的,它实际上就是一个JavaConfig形式的Spring容器配置类,它能通过@EnableConfigurationPropertie注解将以Properties结尾命名的配置类中加入容器中,取得在全局配置文件中配置的属性,而XxxxProperties类是通过@ConfigurationProperties注解与全局配置文件中对应的属性进行绑定的。

4. Exclude auto-configuration classes at startup

Method 1: exclude or excludeName of @SpringBootApplication

exclude specifies auto-configuration classes to exclude

excludeName specifies the fully qualified class name of the auto-configuration class to exclude

Method 2: Set spring.autoconfigure.exclude in yml to exclude


5. Summary diagram

insert image description here

If there are any mistakes, please point them out and correct them in time

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324135086&siteId=291194637
Recommended