當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
自定义SpringBoot Starter实现
生活随笔
收集整理的這篇文章主要介紹了
自定义SpringBoot Starter实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 自定義stater pom文件
- 配置文件類properties
- 使用配置類
- 創建AutoConfiguration
- 項目結構
自定義stater pom文件
引入自動配置類spring-boot-starter
<?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.sl.learn.starter</groupId><artifactId>spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version><properties><java.version>1.8</java.version></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.6.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>配置文件類properties
用來保存默認的配置
@ConfigurationProperties(prefix = "stater") public class StaterProperties {private String code;private String desc;public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc;} }使用配置類
public class StaterService {private StaterProperties staterProperties;public StaterProperties getStaterProperties() {return staterProperties;}public void setStaterProperties(StaterProperties staterProperties) {this.staterProperties = staterProperties;}public String say() {return staterProperties.getCode() + "-" + staterProperties.getDesc();} }創建AutoConfiguration
創建一個AutoConfiguration,引用定義好的配置信息;在AutoConfiguration中實現所有starter應該完成的操作,并且把這個類加入spring.factories配置文件中進行聲明。
@Configuration @EnableConfigurationProperties(StaterProperties.class) public class StaterAutoConfiguration {@Bean@ConditionalOnBeanpublic StaterService staterService() {return new StaterService();} } org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.sl.learn.config.StaterAutoConfiguration項目結構
打包項目安裝到Maven倉庫,之后在一個SpringBoot項目中引入該項目依賴,然后就可以使用該starter了。
總結
以上是生活随笔為你收集整理的自定义SpringBoot Starter实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Druid 连接池的实用 配置详解
- 下一篇: SpringCloud教程- 路由网关Z