项目结构
hello-spring-boot-starter├── highness-hello-spring-boot-starter 主要模块├── highness-hello-spring-boot-starter-autoconfigure 自动配置模块└── highness-hello-spring-boot-starter-demo 测试模块
项目依赖
<!-- hello-spring-boot-starter --><groupId>top.parak</groupId><artifactId>hello-spring-boot-starter</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><modules><module>highness-hello-spring-boot-starter</module><module>highness-hello-spring-boot-starter-autoconfigure</module><module>highness-hello-spring-boot-starter-demo</module></modules><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><spring.boot.version>2.2.2.RELEASE</spring.boot.version></properties><!-- highness-hello-spring-boot-starter --><parent><artifactId>hello-spring-boot-starter</artifactId><groupId>top.parak</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>highness-hello-spring-boot-starter</artifactId><dependencies><dependency><artifactId>highness-hello-spring-boot-starter-autoconfigure</artifactId><groupId>top.parak</groupId><version>${project.version}</version></dependency></dependencies><!-- highness-hello-spring-boot-autoconfigure --><parent><artifactId>hello-spring-boot-starter</artifactId><groupId>top.parak</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>highness-hello-spring-boot-starter-autoconfigure</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>${spring.boot.version}</version></dependency></dependencies><!-- highness-hello-spring-boot-demo --><parent><artifactId>hello-spring-boot-starter</artifactId><groupId>top.parak</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>highness-hello-spring-boot-starter-demo</artifactId><dependencies><dependency><groupId>top.parak</groupId><artifactId>highness-hello-spring-boot-starter-autoconfigure</artifactId><version>${project.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>${spring.boot.version}</version></dependency></dependencies>
核心编码
highness-hello-spring-boot-starter为空模块,highness-hello-spring-boot-starter-autoconfigure模块如下:
top└─parak└─hello├─auto──HelloServiceConfigure├─bean──HelloProperties└─service──HelloService
HelloService
import org.springframework.beans.factory.annotation.Autowired;import top.parak.hello.bean.HelloProperties;/*** @author KHighness* @since 2021-09-01*/public class HelloService {@Autowiredprivate HelloProperties helloProperties;public String sayHello(String username) {return helloProperties.getPrefix() + ' ' +username + helloProperties.getSuffix();}}
HelloProperties
import org.springframework.boot.context.properties.ConfigurationProperties;/*** @author KHighness* @since 2021-09-01*/@ConfigurationProperties("parak.hello")public class HelloProperties {private String prefix;private String suffix;public String getPrefix() {return prefix;}public void setPrefix(String prefix) {this.prefix = prefix;}public String getSuffix() {return suffix;}public void setSuffix(String suffix) {this.suffix = suffix;}}
HelloServiceConfigure
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import top.parak.hello.bean.HelloProperties;import top.parak.hello.service.HelloService;/*** @author KHighness* @since 2021-09-01*/@Configuration@ConditionalOnMissingBean(HelloService.class)@EnableConfigurationProperties(HelloProperties.class)public class HelloServiceConfigure {@Beanpublic HelloService helloService() {return new HelloService();}}
另外,需要在resources目录下新建META-INF文件夹,新建spring.factories文件:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\top.parak.hello.auto.HelloServiceConfigure
测试代码
highness-spring-boot-starter-demo模块如下:
top└─parak└─hello├─controller──HelloController└─KhighnessApplication
在resources下面新建application.properties文件:
server.port=3333parak.hello.prefix=Helloparak.hello.suffix=!
HelloController
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import top.parak.hello.service.HelloService;/*** @author KHighness* @since 2021-09-01*/@RestControllerpublic class HelloController {@Autowiredprivate HelloService helloService;@GetMapping("/hello")public String sayHello(@RequestParam("name") String name) {return helloService.sayHello(name);}}
KhighnessApplication
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author KHighness* @since 2021-09-01*/@SpringBootApplicationpublic class KHighnessApplication {public static void main(String[] args) {SpringApplication.run(KHighnessApplication.class, args);}}
测试结果

