亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring Boot 配置文件不選擇屬性文件

Spring Boot 配置文件不選擇屬性文件

慕村225694 2023-10-13 14:48:05
我們正在開發 Spring Boot 2.1.6,我們需要在我們的應用程序中實現 spring boot 配置文件目前我們的項目中有兩個屬性文件 application.properties 和 bucket.properties(s3 configuration) 文件。因此,我們為開發環境創建了兩個屬性文件 resources/application-dev.properties 和 resources/bucket-dev.properties 文件。我們在下面的程序中傳遞 VM 參數 -Dspring.profiles.active=dev ,以便它正確拾取文件。我們使用基于 XML 的配置,因此我們使用以下定義加載屬性文件。<bean id="applicationProperties"        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >        <property name="locations" >            <list>                <value>classpath:application-${spring.profiles.active:dev}.properties</value>                <value>classpath:bucket-${spring.profiles.active:dev}.properties</value>            </list>        </property></bean>上面的配置工作正常,Spring Boot 能夠正確拾取文件。但我想在資源文件夾中創建以下類型的文件夾結構以正確分隔文件。|resources        |dev            |            application-dev.properties              bucket-dev.properties一旦我這樣做,我就對上面的 PropertyPlaceholderConfigurer 進行了更改,如下所示。<bean id="applicationProperties"        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >        <property name="locations" >            <list>                <value>classpath:${spring.profiles.active}/application-${spring.profiles.active:dev}.properties</value>                <value>classpath:${spring.profiles.active}/bucket-${spring.profiles.active:dev}.properties</value>            </list>        </property></bean>一旦我使用上述配置啟動應用程序,就無法找到上述文件中定義的屬性,并且無法啟動應用程序。請讓我知道上面的配置中缺少什么。注意:我們在 Spring Boot 應用程序中沒有使用基于注釋的配置,而是僅使用基于 XML 的配置。
查看完整描述

1 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

Springboot 不會立即執行此操作,但您可以使用它PropertySourcesPlaceholderConfigurer來執行此操作。


@Configuration

public class PropertyFileLoaderConfig {


    private static final Logger LOG = LoggerFactory.getLogger(PropertyFileLoaderConfig.class);


    private static final String PROFILE_DEV = "dev";

    private static final String PROFILE_STAGE = "stage";

    private static final String PROFILE_PROD = "prod";


    private static final String PATH_TEMPLATE = "classpath*:%s/*.properties";


    @Bean

    @Profile(PROFILE_DEV)

    public static PropertySourcesPlaceholderConfigurer devPropertyPlaceholderConfigurer() throws IOException {

        LOG.info("Initializing {} properties.", PROFILE_DEV);

        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();

        configurer.setLocations(new PathMatchingResourcePatternResolver().getResources(getResourcesFromPath(PROFILE_DEV)));//Loads all properties files from the path

        configurer.setIgnoreUnresolvablePlaceholders(true);


        return configurer;

    }


    @Bean

    @Profile(PROFILE_STAGE)

    public static PropertySourcesPlaceholderConfigurer stagePropertyPlaceholderConfigurer() throws IOException {

        LOG.info("Initializing {} properties.", PROFILE_STAGE);

        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();

        configurer.setLocations(new PathMatchingResourcePatternResolver().getResources(getResourcesFromPath(PROFILE_STAGE)));


        return configurer;

    }


    @Bean

    @Profile(PROFILE_PROD )

    public static PropertySourcesPlaceholderConfigurer prodPropertyPlaceholderConfigurer() throws IOException {

        LOG.info("Initializing {} properties.", PROFILE_PROD );

        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();

        configurer.setLocations(new PathMatchingResourcePatternResolver().getResources(getResourcesFromPath(PROFILE_PROD )));


        return configurer;

    }


    private static String getResourcesFromPath(String path) {

        return PATH_TEMPLATE.replaceFirst("%s", path);

    }

}


查看完整回答
反對 回復 2023-10-13
  • 1 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號