欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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進(jìn)行修飾,來限定spring在bean管理時的初始化情況,只有環(huán)境中激活的profile狀態(tài)和修飾的value值對應(yīng)時,該bean才會被順利加載并管理。

PropertyPlaceholderConfigurer

PropertyPlaceholderConfigurer 是PlaceholderConfigurerSupport的實現(xiàn)類,是spring提供的一個解析yml或properties配置文件并將對應(yīng)的值進(jìn)行映射,通過 ${} 形式進(jìn)行調(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")
    // 默認(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;
    }
}

管理了三個 PropertyPlaceholderConfigurer 類型的配置讀取類,分別對應(yīng)不同的profile狀態(tài)。通過 ClassPathResource 讀取對應(yīng)的配置文件,如果用xml配置文件進(jìn)行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)對配置文件的獲取。

加載上下文進(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激活

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

1.直接上下文設(shè)定,也就是上文中使用的,對enviroment中的 activeProfile 進(jìn)行設(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 進(jìn)行修飾,value設(shè)置為對應(yīng)的環(huán)境。 總結(jié)

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

總結(jié)

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

相關(guān)文章

最新評論