前言:
本章节使用springboot3.0进行代码演示,与Spring Boot 2.7及之前的版本中,这些候选自动配置类通常通过spring.factories文件存放;而在Spring Boot 3.0及之后的版本中,则通过META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件存放。
上一篇自动配置基础知识:https://blog.csdn.net/Qzhangww/article/details/143760961
本次使用springboot版本:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.1.5</version>
<relativePath/>
</parent>
一:创建自动配置模块
qzh-common-test模块,组件化。
1.依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
2.定义属性配置
读取application.yml配置
@Data
@ConfigurationProperties(prefix = "test")
public class TestProperties {
private String enabled;
private String name;
private String desc;
}
3.定义测试接口
提供出去给别的模块调用
public class TestService {
private TestProperties testProperties;
public TestService(TestProperties testProperties) {
this.testProperties = testProperties;
System.out.println("公共模块自动配置:" + testProperties);
}
public String read() {
return testProperties.getName() + "=====" + testProperties.getDesc();
}
}
4.定义自动配置类
@AutoConfiguration
@EnableConfigurationProperties(value = TestProperties.class)
public class TestConfig {
//当test.enabled=true,才创建这个bean
@Bean
@ConditionalOnProperty(value = {"test.enabled"}, havingValue = "true")
public TestService testService(TestProperties testProperties) {
return new TestService(testProperties);
}
}
5.创建自动配置候选文件
在resource目录创建META-INF目录,在META-INF目录创建spring目录,在spring目录创建文件org.springframework.boot.autoconfigure.AutoConfiguration.imports
将自动配置类纳入:
二:新建测试模块
1.依赖
引入自动配置qzh-common-test模块。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.1.5</version>
<relativePath/>
</parent>
<packaging>jar</packaging>
<groupId>com.qzh.test</groupId>
<artifactId>qzh-test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven-jar-plugin.version>3.2.2</maven-jar-plugin.version>
<maven-war-plugin.version>3.2.2</maven-war-plugin.version>
</properties>
<dependencies>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--qzh-common-test模块-->
<dependency>
<groupId>com.qzh</groupId>
<artifactId>qzh-common-test</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.1.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 关闭过滤 -->
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<!-- 引入所有 匹配文件进行过滤 -->
<includes>
<include>application*</include>
</includes>
<!-- 启用过滤 即该资源中的变量将会被过滤器中的值替换 -->
<filtering>true</filtering>
</resource>
</resources>
</build>
2.测试
a.属性条件注解
- 当test.enabled=true场景
可以看到,满足条件之后,spring自动创建这个bean。
- 当test.enabled=false场景
可以看到,不满足条件之后,spring不会自动创建这个bean。
b.类条件注解
这里注释掉了属性条件注解,使用@ConditionalOnWebApplication:当前项目是web项目才开启配置
总结:
在实际项目中,自动配置会使用的非常广泛,一般项目会自定义基础框架,框架会涉及多种不同功能的基础模块,需要使用的时候引入模块即可,开发者只需关注应用的核心业务逻辑。