springboot starter自定义实现公共模块
生活随笔
收集整理的這篇文章主要介紹了
springboot starter自定义实现公共模块
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
SpringBoot中的starter是一種非常重要的機制,能夠拋棄以前繁雜的配置,將其統一集成進starter,應用者只需要在maven中引入starter依賴,SpringBoot就能自動掃描到要加載的信息并啟動相應的默認配置。
我們將可獨立于業務代碼之外的功配置模塊封裝成一個個starter,復用的時候只需要將其在pom中引用依賴即可,SpringBoot為我們完成自動裝配。
比如登錄模塊,基于aop的日志模塊等。
SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的。官方建議自定義的starter使用xxx-spring-boot-starter命名規則。以區分SpringBoot生態提供的starter。
以下是定義一個starter的步驟:
1. 建立一個父項目
<?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. 新建一個maven module項目作為自定義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"><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;/*** 描述:配置信息 實體***/ @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. 定義服務類
package com.demo.service;import com.demo.DemoProperties; import org.springframework.beans.factory.annotation.Autowired;/*** author:HUAWEI*/ public class DemoService {@Autowiredprivate 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 {@Autowiredprivate DemoProperties demoProperties;@Bean(name = "demo")public DemoService demoService(){return new DemoService(demoProperties.getParams1(), demoProperties.getParams2());} }6. 重要的一步
resource目錄下添加META-INF目錄,在其下面建立文件spring.factories,內容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.demo.DemoStarterAutoConfig經過以上步驟,starter就完成了。
以下是建立測試項目的步驟。
7. 建立maven module測試項目
<?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: 8010demo:enable: trueparams1: 參數1params2: 參數2minLength: 49. 添加測試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();elsereturn "無效數據";} }10. 打開瀏覽器,執行測試
自此完成了starter的基本測試。
總結
以上是生活随笔為你收集整理的springboot starter自定义实现公共模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电子邮件群发成就千里马和伯乐“良缘”
- 下一篇: 数据链路层之以太网协议