第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

慕村225694 2023-10-13 14:48:05
我們正在開(kāi)發(fā) Spring Boot 2.1.6,我們需要在我們的應(yīng)用程序中實(shí)現(xiàn) spring boot 配置文件目前我們的項(xiàng)目中有兩個(gè)屬性文件 application.properties 和 bucket.properties(s3 configuration) 文件。因此,我們?yōu)殚_(kāi)發(fā)環(huán)境創(chuàng)建了兩個(gè)屬性文件 resources/application-dev.properties 和 resources/bucket-dev.properties 文件。我們?cè)谙旅娴某绦蛑袀鬟f VM 參數(shù) -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 能夠正確拾取文件。但我想在資源文件夾中創(chuàng)建以下類型的文件夾結(jié)構(gòu)以正確分隔文件。|resources        |dev            |            application-dev.properties              bucket-dev.properties一旦我這樣做,我就對(duì)上面的 PropertyPlaceholderConfigurer 進(jìn)行了更改,如下所示。<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>一旦我使用上述配置啟動(dòng)應(yīng)用程序,就無(wú)法找到上述文件中定義的屬性,并且無(wú)法啟動(dòng)應(yīng)用程序。請(qǐng)讓我知道上面的配置中缺少什么。注意:我們?cè)?Spring Boot 應(yīng)用程序中沒(méi)有使用基于注釋的配置,而是僅使用基于 XML 的配置。
查看完整描述

1 回答

?
回首憶惘然

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊

Springboot 不會(huì)立即執(zhí)行此操作,但您可以使用它PropertySourcesPlaceholderConfigurer來(lái)執(zhí)行此操作。


@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);

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-10-13
  • 1 回答
  • 0 關(guān)注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)