给提供的 Student/Klass/School 实现自动配置和 Starter。
一、自定义一个starter的实现方法
1.1 新建工程
xsj-spring-boot-starter
1.2 pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo</groupId>
<artifactId>xsj-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>xsj-spring-boot-starter</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
1.3 定义一个实体类映射配置信息
@ConfigurationProperties(prefix = "student"),将“ student”前缀的配置映射成一个实体
package com.demo.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "student")
@Data
public class StudentPropertise {
private int id;
private String name;
}
1.4 定义一个配置类
@EnableConfigurationProperties(StudentPropertise.class),告诉springboot,支持步骤3.4中的ConfigurationProperties实体类映射配置。
@ConditionalOnProperty(
prefix = "student",
name = "isopen",
havingValue = "true"
),控制@Configuration是否生效。
当配置前缀是student,含有isopen属性,且属性值为true时,@Configuration生效。
package com.demo.config;
import com.demo.properties.StudentPropertise;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(StudentPropertise.class)
@ConditionalOnProperty(
prefix = "student",
name = "isopen",
havingValue = "true"
)
public class StudentConfig {
}
1.5 指定自动装配的配置类
spring.factories内容:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.demo.config.StudentConfig
1.6 生成starter
执行
mvn clean install
二、验证
2.1 在测试工程中引入pom依赖
<dependency>
<groupId>com.demo</groupId>
<artifactId>xsj-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
2.2 application.yml中配置依赖
student:
id: 1
name: xsj
isopen: true
2.3 验证StudentPropertise类
package com.example.demo;
import com.demo.properties.StudentPropertise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
@Autowired
private StudentPropertise studentPropertise;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println(studentPropertise);
}
}
输出:
三、集合对象如何自动装配?
Klass是一个包含student集合的类。
3.1自定义实体映射配置信息
package com.demo.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
@Data
@ConfigurationProperties(prefix = "klass")
public class KlassProperties {
private List<StudentPropertise> students;
}
3.2 自定义配置类
ConditionalOnProperty注解非必须
package com.demo.config;
import com.demo.properties.KlassProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(KlassProperties.class)
public class KlassConfig {
}
3.3 spring.factories中指定自动装配的配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.demo.config.StudentConfig,\
com.demo.config.KlassConfig
3.4 如何配置
在测试工程的yml文件中增加以下配置,springboot会将这些配置自动装配成一个KlassProperties实体
klass:
students:
- id: 11
name: xu
- id: 12
name: sj
四、踩坑
4.1.自定义的starter工程中不可添加启动入口类,否则,install之后无法引入starter中的类。
4.2 自定的starter工程中把启动入口类去掉,还要把maven插件去掉,不然install时会保存,这个插件会自动去找启动入口类,如果找不到,install会失败。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
参考:
springboot~ConfigurationProperties注解配置集合对象
代码地址:
https://github.com/xushengjun/JAVA-01/tree/main/Week_05/src/day02/homework3