Spring Profile與PropertyPlaceholderConfigurer項(xiàng)目多環(huán)境配置切換
最近考慮項(xiàng)目在不同環(huán)境下配置的切換,使用profile注解搭配PropertyPlaceholderConfigurer實(shí)現(xiàn)對(duì)配置文件的切換,簡(jiǎn)單寫(xiě)了個(gè)demo記錄下實(shí)現(xiàn)。
基本知識(shí)介紹
@Profile
@Profile 通過(guò)對(duì)bean進(jìn)行修飾,來(lái)限定spring在bean管理時(shí)的初始化情況,只有環(huán)境中激活的profile狀態(tài)和修飾的value值對(duì)應(yīng)時(shí),該bean才會(huì)被順利加載并管理。
PropertyPlaceholderConfigurer
PropertyPlaceholderConfigurer 是PlaceholderConfigurerSupport的實(shí)現(xiàn)類(lèi),是spring提供的一個(gè)解析yml或properties配置文件并將對(duì)應(yīng)的值進(jìn)行映射,通過(guò) ${} 形式進(jìn)行調(diào)用的配置讀取類(lèi)。
舉例來(lái)說(shuō),配置文件中 akb.num=48 ,那么在bean或配置文件中使用 ${akb.num} 即可獲取配置的值48。
簡(jiǎn)單實(shí)現(xiàn)
profile修飾的bean實(shí)例在不同環(huán)境下的切換
首先定義bean接口與default、dev、prob三種情況下的bean。
接口 DeafultProfileBean
@Component
public interface DefaultProfileBean {
String getInfo();
}default bean ProfileBeanDefault
@Profile("default")
@Component
public class ProfileBeanDefault implements DefaultProfileBean{
@Override
public String getInfo() {
return "這是default狀態(tài)下的bean";
}
}dev bean ProfileBeanDev
@Profile("dev")
@Component
public class ProfileBeanDev implements DefaultProfileBean{
@Override
public String getInfo(){
return "這是dev環(huán)境使用的bean";
}
}dev bean ProfileBeanProd
@Profile("prod")
@Component
public class ProfileBeanProd implements DefaultProfileBean{
@Override
public String getInfo() {
return "這是prod環(huán)境使用的bean";
}
}加載上下文并輸出加載的bean結(jié)果
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
// 設(shè)置profile環(huán)境
context.getEnvironment().setActiveProfiles("dev");
context.scan("com.huiluczP");
context.refresh();
System.out.println("loading success");
DefaultProfileBean bean = context.getBean(DefaultProfileBean.class);
System.out.println(bean.getInfo());切換profile環(huán)境后的不同輸出結(jié)果:



可以看出@Profile注解生效了。
配置類(lèi)和配置文件
SpringConfigure 配置類(lèi)
@Configuration
public class SpringConfigure {
@Bean
@Profile("default")
// 默認(rèn)狀態(tài)配置加載類(lèi)
public PropertyPlaceholderConfigurer defaultConfig(){
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
Resource resource = new ClassPathResource("config/default.properties");
ppc.setLocation(resource);
return ppc;
}
@Bean
@Profile("dev")
// dev狀態(tài)配置加載類(lèi)
public PropertyPlaceholderConfigurer devConfig(){
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
Resource resource = new ClassPathResource("config/dev.properties");
ppc.setLocation(resource);
return ppc;
}
@Bean
@Profile("prod")
// prod狀態(tài)配置加載類(lèi)
public PropertyPlaceholderConfigurer prodConfig(){
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
Resource resource = new ClassPathResource("config/prod.properties");
ppc.setLocation(resource);
return ppc;
}
}管理了三個(gè) PropertyPlaceholderConfigurer 類(lèi)型的配置讀取類(lèi),分別對(duì)應(yīng)不同的profile狀態(tài)。通過(guò) ClassPathResource 讀取對(duì)應(yīng)的配置文件,如果用xml配置文件進(jìn)行PropertyPlaceholderConfigurer bean的管理,直接增加property location,將value設(shè)置為對(duì)應(yīng)的配置文件地址即可。
三個(gè)不同的配置文件內(nèi)容
default.properties
config.info=default information
dev.properties
config.info=dev information
prod.properties
config.info=prod information
配置獲取測(cè)試接口和bean類(lèi)
public interface DefaultConfigBean {
String getConfigInfo();
}
@Component
public class DifferentConfigBean implements DefaultConfigBean{
@Value("${config.info}")
private String config;
@Override
public String getConfigInfo() {
return "當(dāng)前環(huán)境下的config信息為:" + config;
}
}通過(guò) ${config.info} 實(shí)現(xiàn)對(duì)配置文件的獲取。
加載上下文進(jìn)行處理
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
// 設(shè)置profile環(huán)境
context.getEnvironment().setActiveProfiles("prod");
context.scan("com.huiluczP");
context.refresh();
DefaultConfigBean configBean = context.getBean(DefaultConfigBean.class);
System.out.println(configBean.getConfigInfo());切換環(huán)境輸出結(jié)果



profile激活
特別的,說(shuō)明下對(duì)項(xiàng)目profile環(huán)境怎么進(jìn)行設(shè)置以對(duì)profile進(jìn)行激活。沒(méi)有特別指定時(shí),默認(rèn)調(diào)用default修飾的bean。
1.直接上下文設(shè)定,也就是上文中使用的,對(duì)enviroment中的 activeProfile 進(jìn)行設(shè)置即可。
2.web項(xiàng)目中可以在web.xml中設(shè)置全局的變量:
<context-param>
<param-name>spring.profiles.alive</param-name>
<param-value>dev</param-value>
</context-param>3.如果是springMVC管理,可以在DispatcherServlet的配置中增加init-param:
<init-param>
<param-name>spring.profiles.alive</param-name>
<param-value>dev</param-value>
</init-param>4.可以在jvm的運(yùn)行屬性中設(shè)置,tomcat等服務(wù)器的啟動(dòng)option也可設(shè)置jvm屬性。
-Dspring.profiles.active=dev
5.對(duì)測(cè)試類(lèi)使用注解 @ActiveProfiles 進(jìn)行修飾,value設(shè)置為對(duì)應(yīng)的環(huán)境。 總結(jié)
簡(jiǎn)單記錄了一下spring profile和PropertyPlaceholderConfigurers類(lèi)實(shí)現(xiàn)不同環(huán)境下的不同配置文件加載的方法,在分支中進(jìn)行快速切換還是挺方便的,而且PropertyPlaceholderConfigurer映射的配置在spring讀取其他的配置文件時(shí)也可以通過(guò)${}進(jìn)行讀取,這樣不同的環(huán)境配置文件只需要一份,并將其中需要變動(dòng)的部分用PropertyPlaceholderConfigurer進(jìn)行管理即可。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot?+?Disruptor實(shí)現(xiàn)特快高并發(fā)處理及使用Disruptor高速實(shí)現(xiàn)隊(duì)列的過(guò)程
Disruptor是一個(gè)開(kāi)源的Java框架,它被設(shè)計(jì)用于在生產(chǎn)者—消費(fèi)者(producer-consumer problem,簡(jiǎn)稱(chēng)PCP)問(wèn)題上獲得盡量高的吞吐量(TPS)和盡量低的延遲,這篇文章主要介紹了SpringBoot?+?Disruptor?實(shí)現(xiàn)特快高并發(fā)處理,使用Disruptor高速實(shí)現(xiàn)隊(duì)列,需要的朋友可以參考下2023-11-11
使用Java將實(shí)體類(lèi)轉(zhuǎn)換為JSON并輸出到控制臺(tái)的完整過(guò)程
在軟件開(kāi)發(fā)的過(guò)程中,Java是一種廣泛使用的編程語(yǔ)言,而在眾多應(yīng)用中,數(shù)據(jù)的傳輸和存儲(chǔ)經(jīng)常需要使用JSON格式,用Java將實(shí)體類(lèi)轉(zhuǎn)換為JSON格式并輸出其實(shí)不難,只需掌握幾個(gè)步驟就可以做到!接下來(lái),我們來(lái)看看這一過(guò)程究竟是如何實(shí)現(xiàn)的,感興趣的小伙伴跟著小編一起來(lái)看看吧2025-05-05
Spring中使用atomikos+druid實(shí)現(xiàn)經(jīng)典分布式事務(wù)的方法
這篇文章主要介紹了Spring中使用atomikos+druid實(shí)現(xiàn)經(jīng)典分布式事務(wù)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
將本地服務(wù)注冊(cè)到nacos上的實(shí)現(xiàn)過(guò)程
文章介紹了如何在本地安裝和啟動(dòng)Nacos服務(wù),并配置本地應(yīng)用程序使用Nacos進(jìn)行服務(wù)注冊(cè),文章強(qiáng)調(diào)了啟動(dòng)日志窗口的重要性,并提供了一個(gè)簡(jiǎn)短的總結(jié),希望對(duì)讀者有所幫助2026-01-01
Java入門(mén)教程--帶包的類(lèi)如何編譯與運(yùn)行
我們一般都是通過(guò)IDE(如Eclipse、Intellij Idea,STS等)來(lái)開(kāi)發(fā),調(diào)試java項(xiàng)目。在不借助IDE的情況下,如何編譯、運(yùn)行Java程序。打包編譯時(shí),會(huì)自動(dòng)創(chuàng)建包目錄,不需要自己新建包名文件夾。2022-12-12
SocketIo+SpringMvc實(shí)現(xiàn)文件的上傳下載功能
這篇文章主要介紹了SocketIo+SpringMvc實(shí)現(xiàn)文件的上傳下載功能,socketIo不僅可以用來(lái)做聊天工具,也可以實(shí)現(xiàn)局域網(wǎng)。文中給出了實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-08-08
JAVA 對(duì)接騰訊云直播的實(shí)現(xiàn)
這篇文章主要介紹了JAVA 對(duì)接騰訊云直播的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
java 抽象類(lèi)與接口的區(qū)別總結(jié)
這篇文章主要介紹了java 抽象類(lèi)與接口的區(qū)別總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-02-02

