赞
踩
spring boot 中可以方便的开发自定义 starter依赖,自定义 starter 命名规则一般为 xxx-spring-boot-stater。创建自定义 starter 的主要目的有两个:一是方便引入和管理 maven 依赖,二是向 spring IOC 中注册自定义 bean 对象,方便在项目中直接使用。
假设目前需要创建一个 custom-spring-boot-stater 依赖。
整体开发过程如下。
<properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring.boot.version>2.4.5</spring.boot.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>${spring.boot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>${spring.boot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>${spring.boot.version}</version> </dependency> </dependencies> </dependencyManagement>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <!--此依赖可以实现配置文件的自动提示功能--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency> </dependencies>
引入 spring-boot-configuration-processor 依赖后,可以实现 spring-configuration-metadata.json 自动生成。spring boot 会根据 json 文件中的内容进行提示。
如果我们想要自定义一些提示,并且限制用户输入某些属性的值,需要在开发 starter 的项目里面的 META-INF 文件夹下,创建additional-spring-configuration-metadata.json文件。
一般只需要引用 autoconfigure 项目
<dependencies>
<dependency>
<groupId>com.ming</groupId>
<artifactId>custom-spring-boot-autoconfigure</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
使用 @ConfigurationProperties 注解,声明属性参数,属性字段上的注释信息会被自动提示。
@ConfigurationProperties(prefix = CustomProperties.MING_PREFIX) public class CustomProperties { public static final String MING_PREFIX = "ming"; /** * 姓名 */ private String name; /** * 年龄 */ private Integer age; /** * 家庭地址 */ private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
使用 @Configuration 注解,声明该类是一个配置类
使用 @EnableConfigurationProperties ,将外部配置信息读取到配置类中
@Configuration @EnableConfigurationProperties(CustomProperties.class) public class CustomAutoConfigure { private CustomProperties customProperties; // 注意这里直接传进来了配置类对象参数,是由 IOC 容器创建并传入的,不能通过 @Autowired 获取 public CustomAutoConfigure(CustomProperties customProperties) { this.customProperties = customProperties; } @Bean public MyStudent myStudent() { MyStudent myStudent = new MyStudent(customProperties.getName(), customProperties.getAge(), customProperties.getAddress()); return myStudent; } }
bean 实体类为注入到 IOC 中的对象,使用外部配置信息进行初始化,然后在自动配置类中使用 @Bean 进行注入
public class MyStudent { private String name; private Integer age; private String address; public MyStudent() { } public MyStudent(String name, Integer age, String address) { this.name = name; this.age = age; this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
在 resource 目录下,创建 META-INF 目录,并创建 spring.factories 文件,文件中配置好启用自动配置的类路径
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ming.custom.spring.boot.autoconfigure.CustomAutoConfigure
starter 项目一般无需写代码
<dependency>
<groupId>com.ming</groupId>
<artifactId>custom-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
@RestController
public class MyStudentController {
@Autowired
private MyStudent myStudent;
@GetMapping("/show")
public String showStudent() {
return myStudent.getName() + myStudent.getAge() + myStudent.getAddress();
}
}
测试可以正常获取到 autoconfigure 中注入的 bean 对象。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。