1、使用IDEA创建一个maven工程
2、引入依赖后,最终pom.xml为:
<?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.beck</groupId>
<artifactId>beck-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.0.0.RELEASE</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
3、创建需要自动配置的类(StreamYearService.java)
package com.beck.service;
public class StreamYearService {
private String configName;
public StreamYearService(String name){
this.configName = name;
}
public void printStreamYear(){
System.out.println("streamyear-spring-boot-starter ::: " + configName);
}
}
4、创建读取配置的类(StreamYearServiceProperties.java)
package com.beck.service;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("streamyear.service")
public class StreamYearServiceProperties {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
5、创建自动配置的类(StreamYearServiceAutoConfigure .java)
package com.beck.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnClass(StreamYearService.class)
@EnableConfigurationProperties(StreamYearServiceProperties.class)
public class StreamYearServiceAutoConfigure {
@Autowired
private StreamYearServiceProperties streamYearServiceProperties;
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "streamyear.service",value = "enabled", havingValue = "true")
public StreamYearService streamYearService(){
return new StreamYearService(streamYearServiceProperties.getName());
}
}
6、在resources目录下创建目录META-INF,在META-INF里面创建spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.beck.service.StreamYearServiceAutoConfigure
8、测试:在其他项目中引入我们自己的starter
<dependency>
<groupId>com.beck</groupId>
<artifactId>beck-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
9、在测试的项目配置文件中加入
streamyear.service.enabled=true
streamyear.service.name= = 我哦哦哦哦!!!
10、在项目的controller代码中加入如下代码
@Autowired
private StreamYearService streamYearService;
@RequestMapping("starter")
public void starter(){
streamYearService.printStreamYear();
}
11、测试(请求:http://localhost:8088/api/student/starter)
12、最终执行结果
streamyear-spring-boot-starter ::: = 我哦哦哦哦!!!