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

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值對應(yīng)時,該bean才會被順利加載并管理。

PropertyPlaceholderConfigurer

PropertyPlaceholderConfigurer 是PlaceholderConfigurerSupport的實現(xiàn)類,是spring提供的一個解析yml或properties配置文件并將對應(yīng)的值進行映射,通過 ${} 形式進行調(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結(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")
    // 默認狀態(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 類型的配置讀取類,分別對應(yīng)不同的profile狀態(tài)。通過 ClassPathResource 讀取對應(yīng)的配置文件,如果用xml配置文件進行PropertyPlaceholderConfigurer bean的管理,直接增加property location,將value設(shè)置為對應(yīng)的配置文件地址即可。

三個不同的配置文件內(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 "當(dāng)前環(huán)境下的config信息為:" + config;
    }
}

通過 ${config.info} 實現(xià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激活

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

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

2.web項目中可以在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的運行屬性中設(shè)置,tomcat等服務(wù)器的啟動option也可設(shè)置jvm屬性。

-Dspring.profiles.active=dev

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

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

總結(jié)

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

相關(guān)文章

  • SpringBoot?@Scheduled?Cron表達式使用方式

    SpringBoot?@Scheduled?Cron表達式使用方式

    這篇文章主要介紹了SpringBoot?@Scheduled?Cron表達式使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Java查看和修改線程優(yōu)先級操作詳解

    Java查看和修改線程優(yōu)先級操作詳解

    JAVA中每個線程都有優(yōu)化級屬性,默認情況下,新建的線程和創(chuàng)建該線程的線程優(yōu)先級是一樣的。本文將為大家詳解Java查看和修改線程優(yōu)先級操作的方法,需要的可以參考一下
    2022-08-08
  • 淺談Spring @Async異步線程池用法總結(jié)

    淺談Spring @Async異步線程池用法總結(jié)

    本篇文章主要介紹了淺談Spring @Async異步線程池用法總結(jié),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Java中Stream?Filter多條件篩選過濾代碼舉例

    Java中Stream?Filter多條件篩選過濾代碼舉例

    這篇文章主要給大家介紹了關(guān)于Java中Stream?Filter多條件篩選過濾的相關(guān)資料,Java Stream中的filter方法可以使用多個條件來過濾數(shù)據(jù),文中給出了詳細的代碼示例,需要的朋友可以參考下
    2023-12-12
  • SpringBoot整合RabbitMQ及原理

    SpringBoot整合RabbitMQ及原理

    這篇文章主要介紹了SpringBoot整合RabbitMQ及其原理分析,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-06-06
  • Java使用itextpdf找出PDF中文字的坐標

    Java使用itextpdf找出PDF中文字的坐標

    這篇文章主要為大家詳細介紹了Java如果使用itextpdf找出PDF中文字的坐標,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-01-01
  • SpringBoot視圖解析實現(xiàn)原理深入分析

    SpringBoot視圖解析實現(xiàn)原理深入分析

    視圖解析其實就是SpringBoot某一個controller的方法執(zhí)行完成之后,它是跳轉(zhuǎn)到那個頁面。由于我們springboot項目默認打包為jar包,是形成壓縮包的形式,而jsp又不支持壓縮,所以我們SpringBoot不知JSP的,需要引入第三方模板引擎才可以處理
    2022-10-10
  • java15新功能的詳細講解

    java15新功能的詳細講解

    這篇文章主要介紹了java15的新功能,雖然java15并不是長期支持的版本,但是很多新功能還是很有用的。感興趣的小伙伴可以參考一下
    2021-08-08
  • java中extends與implements的區(qū)別淺談

    java中extends與implements的區(qū)別淺談

    java中extends與implements的區(qū)別淺談,需要的朋友可以參考一下
    2013-03-03
  • JAVA中實現(xiàn)鏈式操作(方法鏈)的簡單例子

    JAVA中實現(xiàn)鏈式操作(方法鏈)的簡單例子

    這篇文章主要介紹了JAVA中實現(xiàn)鏈式操作的例子,模仿jQuery的方法鏈實現(xiàn),需要的朋友可以參考下
    2014-04-04

最新評論