1.新建两个项目 starter 和starter-configure (非spring -boot 官方的用xxx-spring-boot-starter)
2.auto-configure pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.kelly</groupId>
<artifactId>kelly-spring-boot-starter-autoconfigure</artifactId>
<version>1.0-SNAPSHOT</version>
<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>
</dependencies>
3. starter pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kelly</groupId>
<artifactId>kelly-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>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-web</artifactId>
</dependency>
<dependency>
<groupId>com.kelly</groupId>
<artifactId>kelly-spring-boot-starter-autoconfigure</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
4. starter 实现
1)配置文件
package com.kelly.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "hello")
public class HelloProperties {
private String welcomeTip;
private String openTip;
public String getWelcomeTip() {
return welcomeTip;
}
public void setWelcomeTip(String welcomeTip) {
this.welcomeTip = welcomeTip;
}
public String getOpenTip() {
return openTip;
}
public void setOpenTip(String openTip) {
this.openTip = openTip;
}
}
2)服务
package com.kelly.service;
import com.kelly.config.HelloProperties;
public class HelloService {
private HelloProperties helloProperties ;
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String hello(String name ){
return helloProperties.getOpenTip()+" "+name +" "+ helloProperties.getWelcomeTip();
}
}
3)autoconfigure配置
package com.kelly;
import com.kelly.config.HelloProperties;
import com.kelly.service.HelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceConfiguration {
@Resource
private HelloProperties helloProperties ;
@Bean
public HelloService helloService (){
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
5. install autoconfigure , install starter
6. 使用示例
1) 添加依赖
<dependency>
<groupId>com.kelly</groupId>
<artifactId>kelly-spring-boot-starter-autoconfigure</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
2)添加配置
hello.open-tip=Hello
hello.welcome-tip=Welcome to ShenZhen