springboot starter自定義實(shí)現(xiàn)公共模塊方式
SpringBoot中的starter是一種非常重要的機(jī)制,能夠拋棄以前繁雜的配置,將其統(tǒng)一集成進(jìn)starter,應(yīng)用者只需要在maven中引入starter依賴,SpringBoot就能自動(dòng)掃描到要加載的信息并啟動(dòng)相應(yīng)的默認(rèn)配置。
我們將可獨(dú)立于業(yè)務(wù)代碼之外的功配置模塊封裝成一個(gè)個(gè)starter,復(fù)用的時(shí)候只需要將其在pom中引用依賴即可,SpringBoot為我們完成自動(dòng)裝配。
比如登錄模塊,基于aop的日志模塊等。
SpringBoot提供的starter以spring-boot-starter-xxx
的方式命名的。官方建議自定義的starter使用xxx-spring-boot-starter
命名規(guī)則。以區(qū)分SpringBoot生態(tài)提供的starter。
以下是定義一個(gè)starter的步驟:
1. 建立一個(gè)父項(xiàng)目
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.9.RELEASE</version> </parent> <groupId>org.example</groupId> <artifactId>springboot-starter-demo</artifactId> <version>1.0-SNAPSHOT</version> <modules> <module>springboot-starter-config</module> <module>springboot-starter-application</module> </modules> <packaging>pom</packaging> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>
2. 新建一個(gè)maven module項(xiàng)目作為自定義starter項(xiàng)目
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"> <parent> <artifactId>springboot-starter-demo</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springboot-starter-config</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> </project>
3. 定義屬性類
package com.demo; import org.springframework.boot.context.properties.ConfigurationProperties; /** * 描述:配置信息 實(shí)體 * **/ @ConfigurationProperties(prefix = "demo") public class DemoProperties { private String params1; private String params2; private Integer minLength; public String getParams1() { return params1; } public void setParams1(String params1) { this.params1 = params1; } public String getParams2() { return params2; } public void setParams2(String params2) { this.params2 = params2; } public Integer getMinLength() { return minLength; } public void setMinLength(Integer minLength) { this.minLength = minLength; } }
4. 定義服務(wù)類
package com.demo.service; import com.demo.DemoProperties; import org.springframework.beans.factory.annotation.Autowired; /** * author:HUAWEI */ public class DemoService { @Autowired private DemoProperties demoProperties; public String params1; public String params2; public DemoService(String param1, String param2) { this.params1 = param1; this.params2 = param2; } public String paramsInfo() { return this.params1 + "! " + params2; } public boolean isValidLength(String param) { int length = param.length(); Integer minLength = demoProperties.getMinLength(); if (length < minLength.intValue()) { return false; } else { return true; } } }
5. 定義配置類
package com.demo; import com.demo.service.DemoService; import org.springframework.beans.factory.annotation.Autowired; 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 @EnableConfigurationProperties(DemoProperties.class) @ConditionalOnProperty( prefix = "demo", name = "enable", havingValue = "true" ) public class DemoStarterAutoConfig { @Autowired private DemoProperties demoProperties; @Bean(name = "demo") public DemoService demoService(){ return new DemoService(demoProperties.getParams1(), demoProperties.getParams2()); } }
6. 重要的一步
resource目錄下添加META-INF目錄,在其下面建立文件spring.factories,內(nèi)容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.demo.DemoStarterAutoConfig
經(jīng)過以上步驟,starter就完成了。
以下是建立測(cè)試項(xiàng)目的步驟。
7. 建立maven module測(cè)試項(xiàng)目
<?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"> <parent> <artifactId>springboot-starter-demo</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springboot-starter-application</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.example</groupId> <artifactId>springboot-starter-config</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
8. 添加配置文件application.yml
server: port: 8010 demo: enable: true params1: 參數(shù)1 params2: 參數(shù)2 minLength: 4
9. 添加測(cè)試controller
package com.demo.starter.controller; import com.demo.service.DemoService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController public class AppController { @Resource(name = "demo") private DemoService demoService; @GetMapping("/test") public String test(){ boolean valid = demoService.isValidLength("test"); if(valid) return demoService.paramsInfo(); else return "無效數(shù)據(jù)"; } }
10. 打開瀏覽器,執(zhí)行測(cè)試
自此完成了starter的基本測(cè)試。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
新手學(xué)習(xí)微服務(wù)SpringCloud項(xiàng)目架構(gòu)搭建方法
這篇文章主要介紹了新手學(xué)習(xí)微服務(wù)SpringCloud項(xiàng)目架構(gòu)搭建方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01Java生成日期時(shí)間存入Mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法
本文主要介紹了Java生成日期時(shí)間存入Mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Java線程編程中isAlive()和join()的使用詳解
這篇文章主要介紹了Java線程編程中isAlive()和join()的使用詳解,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09詳解Reactor如何優(yōu)雅Exception異常處理
初識(shí)響應(yīng)式編程的時(shí)候,除了從命令式的思維方式轉(zhuǎn)變?yōu)楹瘮?shù)式的編程方式外,其中有一個(gè)很大的不適應(yīng)的地方就是在面對(duì)異常時(shí)該怎么處理。本文將通過Project?Reactor的文檔以及源碼來深入解讀,在reactor中是如何優(yōu)雅地實(shí)現(xiàn)這異常處理三板斧,希望對(duì)大家有所幫助2023-02-02