最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Profile與PropertyPlaceholderConfigurer項目多環(huán)境配置切換

 更新時間:2023年09月11日 15:39:30   作者:回爐重造P  
這篇文章主要介紹了Spring Profile與PropertyPlaceholderConfigurer項目多環(huán)境配置切換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

最近考慮項目在不同環(huán)境下配置的切換,使用profile注解搭配PropertyPlaceholderConfigurer實現(xiàn)對配置文件的切換,簡單寫了個demo記錄下實現(xiàn)。

基本知識介紹

@Profile

@Profile 通過對bean進行修飾,來限定spring在bean管理時的初始化情況,只有環(huán)境中激活的profile狀態(tài)和修飾的value值對應時,該bean才會被順利加載并管理。

PropertyPlaceholderConfigurer

PropertyPlaceholderConfigurer 是PlaceholderConfigurerSupport的實現(xiàn)類,是spring提供的一個解析yml或properties配置文件并將對應的值進行映射,通過 ${} 形式進行調(diào)用的配置讀取類。

舉例來說,配置文件中 akb.num=48 ,那么在bean或配置文件中使用 ${akb.num} 即可獲取配置的值48。

簡單實現(xiàn)

profile修飾的bean實例在不同環(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結果

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        // 設置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)境后的不同輸出結果:

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

可以看出@Profile注解生效了。

配置類和配置文件

SpringConfigure 配置類

@Configuration
public class SpringConfigure {
    @Bean
    @Profile("default")
    // 默認狀態(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;
    }
}

管理了三個 PropertyPlaceholderConfigurer 類型的配置讀取類,分別對應不同的profile狀態(tài)。通過 ClassPathResource 讀取對應的配置文件,如果用xml配置文件進行PropertyPlaceholderConfigurer bean的管理,直接增加property location,將value設置為對應的配置文件地址即可。

三個不同的配置文件內(nèi)容

default.properties

config.info=default information

dev.properties

config.info=dev information

prod.properties

config.info=prod information

配置獲取測試接口和bean類

public interface DefaultConfigBean {
    String getConfigInfo();
}
@Component
public class DifferentConfigBean implements DefaultConfigBean{
    @Value("${config.info}")
    private String config;
    @Override
    public String getConfigInfo() {
        return "當前環(huán)境下的config信息為:" + config;
    }
}

通過 ${config.info} 實現(xiàn)對配置文件的獲取。

加載上下文進行處理

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        // 設置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)境輸出結果

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

profile激活

特別的,說明下對項目profile環(huán)境怎么進行設置以對profile進行激活。沒有特別指定時,默認調(diào)用default修飾的bean。

1.直接上下文設定,也就是上文中使用的,對enviroment中的 activeProfile 進行設置即可。

2.web項目中可以在web.xml中設置全局的變量:

<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的運行屬性中設置,tomcat等服務器的啟動option也可設置jvm屬性。

-Dspring.profiles.active=dev

5.對測試類使用注解 @ActiveProfiles 進行修飾,value設置為對應的環(huán)境。 總結

簡單記錄了一下spring profile和PropertyPlaceholderConfigurers類實現(xiàn)不同環(huán)境下的不同配置文件加載的方法,在分支中進行快速切換還是挺方便的,而且PropertyPlaceholderConfigurer映射的配置在spring讀取其他的配置文件時也可以通過${}進行讀取,這樣不同的環(huán)境配置文件只需要一份,并將其中需要變動的部分用PropertyPlaceholderConfigurer進行管理即可。

總結

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

相關文章

  • SpringBoot?+?Disruptor實現(xiàn)特快高并發(fā)處理及使用Disruptor高速實現(xiàn)隊列的過程

    SpringBoot?+?Disruptor實現(xiàn)特快高并發(fā)處理及使用Disruptor高速實現(xiàn)隊列的過程

    Disruptor是一個開源的Java框架,它被設計用于在生產(chǎn)者—消費者(producer-consumer problem,簡稱PCP)問題上獲得盡量高的吞吐量(TPS)和盡量低的延遲,這篇文章主要介紹了SpringBoot?+?Disruptor?實現(xiàn)特快高并發(fā)處理,使用Disruptor高速實現(xiàn)隊列,需要的朋友可以參考下
    2023-11-11
  • 使用Java將實體類轉(zhuǎn)換為JSON并輸出到控制臺的完整過程

    使用Java將實體類轉(zhuǎn)換為JSON并輸出到控制臺的完整過程

    在軟件開發(fā)的過程中,Java是一種廣泛使用的編程語言,而在眾多應用中,數(shù)據(jù)的傳輸和存儲經(jīng)常需要使用JSON格式,用Java將實體類轉(zhuǎn)換為JSON格式并輸出其實不難,只需掌握幾個步驟就可以做到!接下來,我們來看看這一過程究竟是如何實現(xiàn)的,感興趣的小伙伴跟著小編一起來看看吧
    2025-05-05
  • Spring中使用atomikos+druid實現(xiàn)經(jīng)典分布式事務的方法

    Spring中使用atomikos+druid實現(xiàn)經(jīng)典分布式事務的方法

    這篇文章主要介紹了Spring中使用atomikos+druid實現(xiàn)經(jīng)典分布式事務的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-06-06
  • Java構建對象常用3種方法解析

    Java構建對象常用3種方法解析

    這篇文章主要介紹了Java構建對象常用3種方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • 將本地服務注冊到nacos上的實現(xiàn)過程

    將本地服務注冊到nacos上的實現(xiàn)過程

    文章介紹了如何在本地安裝和啟動Nacos服務,并配置本地應用程序使用Nacos進行服務注冊,文章強調(diào)了啟動日志窗口的重要性,并提供了一個簡短的總結,希望對讀者有所幫助
    2026-01-01
  • Java入門教程--帶包的類如何編譯與運行

    Java入門教程--帶包的類如何編譯與運行

    我們一般都是通過IDE(如Eclipse、Intellij Idea,STS等)來開發(fā),調(diào)試java項目。在不借助IDE的情況下,如何編譯、運行Java程序。打包編譯時,會自動創(chuàng)建包目錄,不需要自己新建包名文件夾。
    2022-12-12
  • Java基礎知識總結之繼承

    Java基礎知識總結之繼承

    這一篇我們來學習面向?qū)ο蟮牡诙€特征——繼承,文中有非常詳細的基礎知識總結,對正在學習java的小伙伴們很有幫助,需要的朋友可以參考下
    2021-06-06
  • SocketIo+SpringMvc實現(xiàn)文件的上傳下載功能

    SocketIo+SpringMvc實現(xiàn)文件的上傳下載功能

    這篇文章主要介紹了SocketIo+SpringMvc實現(xiàn)文件的上傳下載功能,socketIo不僅可以用來做聊天工具,也可以實現(xiàn)局域網(wǎng)。文中給出了實現(xiàn)代碼,需要的朋友可以參考下
    2018-08-08
  • JAVA 對接騰訊云直播的實現(xiàn)

    JAVA 對接騰訊云直播的實現(xiàn)

    這篇文章主要介紹了JAVA 對接騰訊云直播的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • java 抽象類與接口的區(qū)別總結

    java 抽象類與接口的區(qū)別總結

    這篇文章主要介紹了java 抽象類與接口的區(qū)別總結的相關資料,需要的朋友可以參考下
    2017-02-02

最新評論

隆尧县| 岗巴县| 惠东县| 屏东市| 樟树市| 开原市| 呈贡县| 邮箱| 庆阳市| 南澳县| 东乡族自治县| 江都市| 宜州市| 泸定县| 永年县| 育儿| 海原县| 疏附县| 白银市| 门头沟区| 铜山县| 饶河县| 阜宁县| 大连市| 石楼县| 教育| 乐亭县| 武山县| 张掖市| 兴安县| 固镇县| 凉山| 水富县| 万州区| 龙江县| 清远市| 清苑县| 益阳市| 吴江市| 许昌县| 白水县|