使用SpringBoot自定義starter詳解
工程由xxx-sprig-boot-starter和xxx-sprig-boot-starter-configure兩個模塊組成;
xxx-sprig-boot-starter模塊
只用來做依賴導(dǎo)入 依賴于 xxx-sprig-boot-starter-configure模塊,沒有實際代碼<?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.ander</groupId> <artifactId>ander-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> <!--依賴ander-spring-boot-starter-configure工程--> <dependencies><dependency> <groupId>com.ander</groupId> <artifactId>ander-spring-boot-starter-configure</artifactId> <version>0.0.1-SNAPSHOT</version></dependency> </dependencies></project>
xxx-sprig-boot-starter-configure模塊
專門自動配置模塊 依賴于spring-boot-starter-web<?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.3.10.RELEASE</version><relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.ander</groupId> <artifactId>ander-spring-boot-starter-configure</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ander-spring-boot-starter-configure</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> </dependencies></project>
/** * Service層 * * @Author: Ander * @Date: 2021-05-04 */public class HelloService { private HelloServiceProperties helloServiceProperties; public String helloService(String name) {return helloServiceProperties.getPrefix() + ' '+ name + ' ' + helloServiceProperties.getSuffix(); } public HelloServiceProperties getHelloServiceProperties() {return helloServiceProperties; } public void setHelloServiceProperties(HelloServiceProperties helloServiceProperties) {this.helloServiceProperties = helloServiceProperties; }}2.2 屬性配置類編碼
/** * 屬性配置類 * * @Author: Ander * @Date: 2021-05-04 */@ConfigurationProperties(prefix = 'com.ander')public class HelloServiceProperties { private String prefix = 'hi'; private String suffix = 'hello world'; 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; }}2.3 starter自動配置類編碼
@EnableConfigurationProperties({HelloServiceProperties.class})作用:讓xxxProperties生效加入到容器中
/** * 自定義starter自動配置類 * * @Author: Ander * @Date: 2021-05-04 */@Configuration@ConditionalOnWebApplication // 指定web應(yīng)用才生效@EnableConfigurationProperties({HelloServiceProperties.class})public class HelloServiceAutoConfigure { @Autowired private HelloServiceProperties helloServiceProperties; @Bean public HelloService helloService() {HelloService helloService = new HelloService();helloService.setHelloServiceProperties(helloServiceProperties);return helloService; }}2.4 添加自動配置類到META-INF路徑下
注意先安裝xxx-spring-boot-starter-configure,再安裝xxx-spring-boot-starter
/** * starter測試控制類 * * @Author: Ander * @Date: 2021-05-05 */@RestControllerpublic class StarterTestController { @Autowired private HelloService helloService; @GetMapping('hello') public String hello(String name) {return helloService.helloService(name); }}3.2 編寫配置文件
server.port=8888com.ander.prefix=HIcom.ander.suffix=HELLO WORLD
四、測試結(jié)果4.1 使用starter默認(rèn)配置到此這篇關(guān)于使用Spring Boot自定義starter詳解的文章就介紹到這了,更多相關(guān)Spring Boot自定義starter內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. React+umi+typeScript創(chuàng)建項目的過程2. .Net core 的熱插拔機制的深入探索及卸載問題求救指南3. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁4. SharePoint Server 2019新特性介紹5. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過程解析6. html清除浮動的6種方法示例7. 讀大數(shù)據(jù)量的XML文件的讀取問題8. XML入門的常見問題(二)9. ASP中常用的22個FSO文件操作函數(shù)整理10. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp
