SpringBoot2零基礎(chǔ)到精通之profile功能與自定義starter
1 profile功能
1.1 profile的生效規(guī)則
為了方便多環(huán)境適配,SpringBoot簡化了profile功能,具體的使用規(guī)則如下: ①在resources文件夾下可以一次創(chuàng)建多個application-xxx.yaml配置文件,分別對應著不同的生產(chǎn)、測試等環(huán)境,但是只有命名為application.yaml(或者后綴.properties的文件)文件會默認加載,所以說其他環(huán)境的配置文件中的配置信息都不會生效。

??②如果是想切換配置文件環(huán)境的話,就可以在默認配置文件中配置
spring:
profiles:
active: test

??③當不同配置文件的配置項產(chǎn)生沖突的時候,首先若是其他環(huán)境都沒有激活的話使用默認配置文件的配置,若是在默認配置文件中激活了其他環(huán)境的配置就按激活的配置

??④使用命令行運行jar包期間可以不用重新修改配置文件再次打包,可以通過命令行參數(shù)配置進行修改激活的環(huán)境。首先需要對項目進行打包并打開jar包的存儲位置

進入dos窗口輸入命令修改環(huán)境并運行jar包

java -jar test-profile-0.0.1-SNAPSHOT.jar --spring.profiles.active=test

⑤我們該可以使用@Profile(“xxx”)注解標注在類、方法或參數(shù)綁定上,表示在指定環(huán)境下才會執(zhí)行該類、方法或者進行配置文件與POJO類的綁定

1.2 外部配置源
??常用可以作為外部配置源的有:Java屬性文件、YAML文件、環(huán)境變量、命令行參數(shù)。其中配置文件的默認掃描位置也不只單單一個,以下五個位置都能被SpringBoot默認掃到,加載順序由高到低但是優(yōu)先級相反(也就是說配置項相同的時候后面的可以覆蓋前面的):(1) classpath 根路徑(2) classpath 根路徑下config目錄(3) 項目jar包同層級(4) 項目jar包同層級的config目錄(5) config目錄的直接子目錄
2 自定義starter
??SpringBoot的starter場景啟動器想必大家都不陌生,在SpringBoot開發(fā)的時候不管進行什么開發(fā)只要用到哪種技術(shù)第一都是引入它的starter場景啟動器,接下來讓我們根據(jù)SpringBoot中的源碼自定義一個場景啟動器。
第一步: 使用Spring Initializr創(chuàng)建一個SpringBoot項目作為autoconfiguration,構(gòu)建項目目錄如下:

封裝自定義starter業(yè)務的HelloService
/**
* @author : mereign
* @date : 2022/3/12 - 20:55
* @desc : service組件,內(nèi)部定義了方法
*/
public class HelloService {
@Autowired
HelloProperties helloProperties;
public String sayHello(String userName) {
return helloProperties.getPrefix() + ":" + userName + "》" + helloProperties.getSuffix();
}
}
封裝配置文件屬性的HelloProperties
/**
* @author : mereign
* @date : 2022/3/12 - 20:57
* @desc : 配置文件的屬性封裝,默認自動導入容器中
*/
@ConfigurationProperties("com.xiaochen")
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;
}
}
決定是否注冊組件的自動配置類HelloServiceAutoConfiguration
/**
* @author : mereign
* @date : 2022/3/12 - 21:04
* @desc : 一個自動配置類,決定是否向容器中注冊service組件,以及配置文件綁定
*/
// 表明這是一個配置類
@Configuration
// 配置文件綁定
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
// 如果容器中沒有這個組件就是用下面的方法進行容器的helloService組件注入,如果有的話就用容器中的
@ConditionalOnMissingBean(HelloService.class)
// 容器注入組件
@Bean
public HelloService helloService() {
HelloService helloService = new HelloService();
return helloService;
}
}
resources文件夾下創(chuàng)建MATE-INF目錄下spring.factories文件,這樣才能加載到指定的自動配置類
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.xiaochen.auto.HelloServiceAutoConfiguration
第二步: 創(chuàng)建一個maven項目作為自定義starter,只需要在它的pom文件中導入autoconfiguration的項目依賴
<dependencies>
<dependency>
<groupId>com.xiaochen</groupId>
<artifactId>test-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
第三步: 分別對兩個項目模塊在生命周期中選擇clean和install,將兩個模塊打成jar包
第四步: 創(chuàng)建測試項目,目錄結(jié)構(gòu)如下

pom文件中導入自定義的starter
<dependency>
<groupId>com.xiaochen</groupId>
<artifactId>test-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
創(chuàng)建一個測試使用的controller
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hel")
public String sayHello() {
return helloService.sayHello("張三");
}
}
配置測試項目的配置文件
com.xiaochen.prefix=jaka
com.xiaochen.suffix=hafd
啟動測試項目訪問controller的請求映射

到此這篇關(guān)于SpringBoot2零基礎(chǔ)到精通之profile功能與自定義starter的文章就介紹到這了,更多相關(guān)SpringBoot2 profile內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java異常鏈表throw結(jié)構(gòu)assert詳細解讀
這篇文章主要給大家介紹了關(guān)于Java中方法使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-08-08
Java中的break和continue關(guān)鍵字的使用方法總結(jié)
下面小編就為大家?guī)硪黄狫ava中的break和continue關(guān)鍵字的使用方法總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11

