一、先准备好项目
-
wx-spring-boot-starter-config
: 处理自动配置的项目 -
wx-spring-boot-starter
:空项目,引入wx-spring-boot-starter-config
-
test-my-starter
:测试starter的项目,是一个web项目,引入wx-spring-boot-starter
二、准备 wx-spring-boot-starter-config
项目
1、准备pom
文件,引入spring boot ,lombok
是为了减少getter
和setter
代码
<?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>
<groupId>com.wx.starter</groupId>
<artifactId>wx-spring-boot-starter-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>wx-spring-boot-starter-config</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
</project>
2、准备配置属性类,绑定前缀是my.config
的配置
@Data
@ConfigurationProperties(prefix = "my.config")
public class MyProperties {
private String name;
private Integer age;
}
3、准备测试类,提供一个方法,供其它项目测试获取配置
@Setter
public class MyService {
private MyProperties myProperties;
public String findMyProperties() {
return myProperties.getName() + "_" + myProperties.getAge();
}
}
4、准备自动配置类,并将MyService
注入容器,将MyProperties
置入MyService
@Configuration
@ConditionalOnWebApplication //只有web项目才会生效
@EnableConfigurationProperties(MyProperties.class)
public class MyConfig {
@Autowired
private MyProperties myProperties;
@Bean
public MyService myService() {
MyService myService = new MyService();
myService.setMyProperties(myProperties);
return myService;
}
}
5、准备spring.factories
文件,配置EnableAutoConfiguration
使spring boot可以识别到自动配置类
在resources
下面建一个META-INF目录,在下面建一个spring.factories
文件,配置如下代码:指向自己的自动配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.wx.starter.config.MyConfig
三、准备 wx-spring-boot-starter
项目
1、pom
文件引入 wx-spring-boot-starter-config
项目
<dependencies>
<dependency>
<groupId>com.wx.starter</groupId>
<artifactId>wx-spring-boot-starter-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
2、使用maven的install
将 wx-spring-boot-starter-config
安装到maven仓库中
3、使用maven的install
将wx-spring-boot-starter
安装到maven仓库中,顺序不能反
四、准备test-my-starter
项目
1、准备pom
文件,需要准备一个web项目,因为上面自动配置类起作用的条件配置了@ConditionalOnWebApplication
,并引入自定义的wx-spring-boot-starter
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.wx.starter</groupId>
<artifactId>wx-spring-boot-starter-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
3、在application.properties
文件中配置属性
my.config.name=aa
my.config.age=10
4、使用JUnit测试获取配置属性代码
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyServiceTest {
@Autowired
private MyService myService;
@Test
public void testHello() {
System.out.println(myService.findMyProperties());
}
}
控制台输出: