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)類,是spring提供的一個(gè)解析yml或properties配置文件并將對(duì)應(yīng)的值進(jìn)行映射,通過(guò) ${} 形式進(jìn)行調(diào)用的配置讀取類。
舉例來(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注解生效了。
配置類和配置文件
SpringConfigure 配置類
@Configuration
public class SpringConfigure {
@Bean
@Profile("default")
// 默認(rèn)狀態(tà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)配置加載類
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)配置加載類
public PropertyPlaceholderConfigurer prodConfig(){
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
Resource resource = new ClassPathResource("config/prod.properties");
ppc.setLocation(resource);
return ppc;
}
}管理了三個(gè) PropertyPlaceholderConfigurer 類型的配置讀取類,分別對(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類
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è)試類使用注解 @ActiveProfiles 進(jìn)行修飾,value設(shè)置為對(duì)應(yīng)的環(huán)境。 總結(jié)
簡(jiǎn)單記錄了一下spring profile和PropertyPlaceholderConfigurers類實(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?@Scheduled?Cron表達(dá)式使用方式
這篇文章主要介紹了SpringBoot?@Scheduled?Cron表達(dá)式使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
Java中Stream?Filter多條件篩選過(guò)濾代碼舉例
這篇文章主要給大家介紹了關(guān)于Java中Stream?Filter多條件篩選過(guò)濾的相關(guān)資料,Java Stream中的filter方法可以使用多個(gè)條件來(lái)過(guò)濾數(shù)據(jù),文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-12-12
Java使用itextpdf找出PDF中文字的坐標(biāo)
這篇文章主要為大家詳細(xì)介紹了Java如果使用itextpdf找出PDF中文字的坐標(biāo),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-01-01
SpringBoot視圖解析實(shí)現(xiàn)原理深入分析
視圖解析其實(shí)就是SpringBoot某一個(gè)controller的方法執(zhí)行完成之后,它是跳轉(zhuǎn)到那個(gè)頁(yè)面。由于我們springboot項(xiàng)目默認(rèn)打包為jar包,是形成壓縮包的形式,而jsp又不支持壓縮,所以我們SpringBoot不知JSP的,需要引入第三方模板引擎才可以處理2022-10-10
java中extends與implements的區(qū)別淺談
java中extends與implements的區(qū)別淺談,需要的朋友可以參考一下2013-03-03
JAVA中實(shí)現(xiàn)鏈?zhǔn)讲僮鳎ǚ椒ㄦ湥┑暮?jiǎn)單例子
這篇文章主要介紹了JAVA中實(shí)現(xiàn)鏈?zhǔn)讲僮鞯睦?模仿jQuery的方法鏈實(shí)現(xiàn),需要的朋友可以參考下2014-04-04

