亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

springboot獲取profile的操作

 更新時(shí)間:2021年09月09日 09:32:30   作者:有夢想的攻城獅  
這篇文章主要介紹了springboot獲取profile的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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)境會(huì)生效
@Service

spring profile的基本使用

Spring profile是Spring 3引入的概念,一般系統(tǒng)開發(fā)的時(shí)候更喜歡使用Maven中的profile來進(jìn)行不同環(huán)境配置文件的區(qū)分。Spring的profile一直沒有怎么使用,最近在一個(gè)公司系統(tǒng)開發(fā)過程中同事使用了Spring profile??墒窃谠O(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文件里,因?yàn)閟pring的加載順序。我們不想改變系統(tǒng)的啟動(dòng)參數(shù),所以選擇了jndi的方式來讀取profile的默認(rèn)啟動(dòng),關(guān)鍵來了,在配置jndi的時(shí)候我們進(jìn)行了以下設(shè)置

 <Environment name="spring.profiles.active" type="java.lang.String" value="local">

悲哀的發(fā)現(xiàn)不起作用,spring的log里面也沒有任何有用的提示,還沒到獲取profile相關(guān)的log就因?yàn)樽x取不到bean掛了。只好去掉了profile相關(guān)的xml文件重啟啟動(dòng)一次系統(tǒng)才發(fā)現(xiàn)spring默認(rèn)讀取的jndi名字是spring.profiles.default而不是spring.profiles.active。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論