springboot獲取profile的操作
springboot獲取profile
通過代碼獲取profile
@Component
public class ProfileUtils implements ApplicationContextAware {
private static ApplicationContext context = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
public static String getActiveProfile(){
String[] profiles = context.getEnvironment().getActiveProfiles();
if(profiles.length != 0){
return profiles[0];
}
return "";
}
}
通過注解的方式來獲取Profile
@Profile("dev","test")
//下面的配置信息只有在dev環(huán)境和test環(huán)境會生效
@Service
spring profile的基本使用
Spring profile是Spring 3引入的概念,一般系統(tǒng)開發(fā)的時候更喜歡使用Maven中的profile來進(jìn)行不同環(huán)境配置文件的區(qū)分。Spring的profile一直沒有怎么使用,最近在一個公司系統(tǒng)開發(fā)過程中同事使用了Spring profile。可是在設(shè)置default profile上遇到了麻煩。跟著一起研究了半天,才發(fā)現(xiàn)了問題所在。
Spring profile在我們系統(tǒng)中的使用非常簡單
并沒有使用runtime的特性,只是在xml中定義了不同profile環(huán)境中的beans
<!-- other beans -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
.......
<!-- production環(huán)境 -->
<beans profile="production">
<context:property-placeholder ignore-resource-not-found="true"
location="classpath*:/application.properties" />
....
</beans>
<!-- local環(huán)境 -->
<beans profile="local">
<context:property-placeholder ignore-resource-not-found="true"
location="classpath*:/application.properties
,classpath*:/application.development.properties"/>
....
</beans>
接下來我們一般可以通過在web.xml,在properties中來選擇使用什么環(huán)境的profile,例如
<!-- web.xml -->
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>production</param-value>
</context-param>
<!-- properties選擇 -->
System.setProperty("spring.profiles.active", "development");
我們的問題出在哪里呢?
出在了我們想通過jndi而不是jvm參數(shù)來選擇默認(rèn)的profile,首先我們知道spring profile的設(shè)置不能在properties文件里,因為spring的加載順序。我們不想改變系統(tǒng)的啟動參數(shù),所以選擇了jndi的方式來讀取profile的默認(rèn)啟動,關(guān)鍵來了,在配置jndi的時候我們進(jìn)行了以下設(shè)置
<Environment name="spring.profiles.active" type="java.lang.String" value="local">
悲哀的發(fā)現(xiàn)不起作用,spring的log里面也沒有任何有用的提示,還沒到獲取profile相關(guān)的log就因為讀取不到bean掛了。只好去掉了profile相關(guān)的xml文件重啟啟動一次系統(tǒng)才發(fā)現(xiàn)spring默認(rèn)讀取的jndi名字是spring.profiles.default而不是spring.profiles.active。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java?MyBatis傳出參數(shù)resultType和resultMap解讀
這篇文章主要介紹了Java?MyBatis傳出參數(shù)resultType和resultMap解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
Spring中AOP概念與兩種動態(tài)代理模式原理詳解
AOP是面向切面編程的技術(shù),AOP基于IoC基礎(chǔ),是對OOP的有益補充,流行的AOP框架有Sping AOP、AspectJ,這篇文章主要給大家介紹了關(guān)于Spring中AOP概念與兩種動態(tài)代理模式原理的相關(guān)資料,需要的朋友可以參考下2021-10-10
java基于正則表達(dá)式實現(xiàn)時間日期的常用判斷操作實例
這篇文章主要介紹了java基于正則表達(dá)式實現(xiàn)時間日期的常用判斷操作,簡單說明了正則表達(dá)式常用元字符含義并結(jié)合實例形式分析了java基于正則表達(dá)式針對常用日期時間格式的判斷操作技巧,需要的朋友可以參考下2017-10-10
Java枚舉類實現(xiàn)Key-Value映射的多種實現(xiàn)方式
在 Java 開發(fā)中,枚舉(Enum)是一種特殊的類,本文將詳細(xì)介紹 Java 枚舉類實現(xiàn) key-value 映射的多種方式,有需要的小伙伴可以根據(jù)需要進(jìn)行選擇2025-04-04
Java虛擬機(jī)調(diào)用Java主類的main()方法
這篇文章主要介紹了Java虛擬機(jī)調(diào)用Java主類的main()方法,前一篇文章我們介紹了關(guān)于Java虛擬機(jī)HotSpot2021-11-11

