项目结构
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.custom</groupId>
<artifactId>spring-boot-starter-custom</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<description>封装一个简单User组件</description>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.5.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.5.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.5.8</version>
</dependency>
</dependencies>
</project>
spring.factories 注意书写格式
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.custom.config.UserAutoConfiguration
UserService (最后注入成bean)
public class UserService {
private String name;
private String age;
private Long id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public void pint() {
System.out.println(String.format("获取的数据用户名:%s----密码:%s----id:%s", name, age, id));
}
}
UserProperties (用户配置)
@ConfigurationProperties(prefix = "song.user")
public class UserProperties {
private String start;
private String name;
private String age;
private Long id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
}
最关键的配置 自动配置类 一定要看注释
/**
* 功能描述: 自动配置类
*
* @author Songxianyang
* @date 2024-04-26 13:40
*/
@Configuration
// yaml档中song.user.start 有没有 如果没有则无法自动配置改类
@ConditionalOnProperty(prefix = "song.user", name = {"start"})
// 把yml配置的属性注入进来 然后使用它。启动自动配置属性
@EnableConfigurationProperties(UserProperties.class)
public class UserAutoConfiguration {
@Bean
public UserService userService(UserProperties userProperties) {
UserService userService = new UserService();
userService.setId(userProperties.getId());
userService.setName(userProperties.getName());
userService.setAge(userProperties.getAge());
return userService;
}
}
如何使用自己封装的starter
随便找一个spring boot 项目 然后再pom中引入自己的starter
这里以我自己kkxx-poi 项目为例子
再写一个web控制层调用一下
关键配置yml (application-local.yml)
测试 读取到具体的配置拉
知识点汇总
- 自动配置原理
- Spring Boot环境选择
- yml配置的类
- 条件注解
- 开启关闭组件逻辑