首页 > 解决方案 > Spring Boot 配置文件未选择属性文件

问题描述

我们正在开发 Spring Boot 2.1.6,我们需要在我们的应用程序中实现 Spring Boot 配置文件

我们的项目中目前有两个属性文件 application.properties 和 bucket.properties(s3 configuration) 文件。

所以我们为开发环境创建了两个属性文件 resources/application-dev.properties 和 resources/bucket-dev.properties 文件。

我们在下面的程序 -Dspring.profiles.active=dev 中传递 VM 参数,以便它正确拾取文件。

我们正在使用基于 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>

一旦我使用上述配置启动应用程序,它就无法找到在上述文件中定义的属性并且无法启动 App。

请让我知道我在上述配置中缺少什么。

注意:我们在 Spring Boot App 中没有使用基于注释的配置,而是仅使用基于 XML 的配置。

标签: javaspringspring-boot

解决方案


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

推荐阅读