基于springboot2.5.5自建启动器starter制品库
【README】
本文po出了自建springboot 啟動(dòng)器步驟;
【1】新建2個(gè)starter相關(guān)組件
根據(jù)? mybatis-spring-boot-starter,我們看到 自建starter需要兩個(gè)組件,分別是 xxx-spring-boot-starter, xxx-spring-boot-starter-autoconfigure ;
其中,xxx-spring-boot-starter負(fù)責(zé)引入 xxx-spring-boot-starter-autoconfigure 依賴; xxx-spring-boot-starter-autoconfigure 負(fù)責(zé)定義相關(guān)配置;
step1,新建空項(xiàng)目 ;
項(xiàng)目名稱為: springbt-08-starter-diy2;
step2,在空項(xiàng)目上新建2個(gè)組件,分別為
lisi-spring-boot-starter 和 lisi-spring-boot-starter-autoconfigure;
其中, lisi-spring-boot-starter為 單純的maven項(xiàng)目;
?lisi-spring-boot-starter-autoconfigure 通過 spring Initializer 工具創(chuàng)建;
step3, lisi-spring-boot-starter 引入 lisi-spring-boot-starter-autoconfigure 依賴;
lisi-spring-boot-starter組件的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.cmc.starter</groupId><artifactId>lisi-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version><dependencies><!--引入 lisi-spring-boot-starter-autoconfigure 依賴--><dependency><groupId>com.cmc.starter</groupId><artifactId>lisi-spring-boot-starter-autoconfigure</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies></project>并把測(cè)試目標(biāo)目錄刪除, lisi-spring-boot-starter目錄結(jié)構(gòu)如下:
?step4, lisi-spring-boot-starter-autoconfigure 組件的pom.xml 如下:
(刪除多余的插件, 測(cè)試庫(kù)依賴)
<?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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.cmc.starter</groupId><artifactId>lisi-spring-boot-starter-autoconfigure</artifactId><version>0.0.1-SNAPSHOT</version><name>lisi-spring-boot-starter-autoconfigure</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</artifactId></dependency></dependencies></project>【2】開發(fā) starter-autoconfigure 啟動(dòng)器配置組件
step1, 新建 HelloService,HelloProperties, HelloServiceAutoConfiguration ;
package com.cmc.starter.lisi;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = "cmc.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;} } package com.cmc.starter.lisi;public class HelloService {HelloProperties helloProperties;public String sayHelloCmc(String name) {return helloProperties.getPrefix() + "-" + name + "-" + helloProperties.getSuffix();}public HelloProperties getHelloProperties() {return helloProperties;}public void setHelloProperties(HelloProperties helloProperties) {this.helloProperties = helloProperties;} } package com.cmc.starter.lisi;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration @ConditionalOnWebApplication // web應(yīng)用才生效 @EnableConfigurationProperties(HelloProperties.class) // 使屬性文件生效 public class HelloServiceAutoConfiguration {@AutowiredHelloProperties helloProperties;@Beanpublic HelloService helloService() {HelloService helloService = new HelloService();helloService.setHelloProperties(helloProperties);return helloService;} }step2,在資源類路徑下新建 META-INF/spring.factories 屬性文件;添加 自動(dòng)配置類全限定類名;
# spring.factories# Auto Config org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.cmc.starter.lisi.HelloServiceAutoConfiguration參考的是 spring-boot-autoconfigure.jar 中的 META-INF/spring.factories的內(nèi)容;
目錄結(jié)構(gòu)如下:
step3,先把 lisi-spring-boot-starter-autoconfigure 制品庫(kù)安裝到本地倉(cāng)庫(kù)
step4,再把 lisi-spring-boot-starter 制品庫(kù)安裝到本地倉(cāng)庫(kù);
【3】自建starter使用
step1, 新建項(xiàng)目,只需要引入 lisi-spring-boot-starter依賴? 即可, 因?yàn)?lisi-spring-boot-starter? 引入了 lisi-spring-boot-starter-autoconfigure,傳遞依賴;
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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.cmc</groupId><artifactId>springboot-08-starter-test2</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-08-starter-test2</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><!--引入自定義的starter--><dependency><groupId>com.cmc.starter</groupId><artifactId>lisi-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version></dependency><!-- 測(cè)試庫(kù) --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- springboot web庫(kù) --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>step2, 新建測(cè)試項(xiàng)目;編寫 Controller,引入 HelloService
import com.cmc.starter.lisi.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class HelloController {@AutowiredHelloService helloService;@GetMapping("/hello")public String hello() {return helloService.sayHelloCmc("zhangsan");} }step3,HelloServiceAutoConfiguration有兩個(gè)屬性前綴后綴,需要配置;在 application.properties 中配置如下:
cmc.hello.prefix=chengdu cmc.hello.suffix=028step4,啟動(dòng)springboot項(xiàng)目,并訪問
localhost:8080/hello ;
總結(jié)
以上是生活随笔為你收集整理的基于springboot2.5.5自建启动器starter制品库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 别对我说永远永远永远是什么歌 别对我说永
- 下一篇: s13什么意思 关于s13