猿问

Spring Boot 配置文件不选择属性文件

我们正在开发 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 的配置。


慕村225694
浏览 99回答 1
1回答

回首忆惘然

Springboot 不会立即执行此操作,但您可以使用它PropertySourcesPlaceholderConfigurer来执行此操作。@Configurationpublic class PropertyFileLoaderConfig {&nbsp; &nbsp; private static final Logger LOG = LoggerFactory.getLogger(PropertyFileLoaderConfig.class);&nbsp; &nbsp; private static final String PROFILE_DEV = "dev";&nbsp; &nbsp; private static final String PROFILE_STAGE = "stage";&nbsp; &nbsp; private static final String PROFILE_PROD = "prod";&nbsp; &nbsp; private static final String PATH_TEMPLATE = "classpath*:%s/*.properties";&nbsp; &nbsp; @Bean&nbsp; &nbsp; @Profile(PROFILE_DEV)&nbsp; &nbsp; public static PropertySourcesPlaceholderConfigurer devPropertyPlaceholderConfigurer() throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; LOG.info("Initializing {} properties.", PROFILE_DEV);&nbsp; &nbsp; &nbsp; &nbsp; PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();&nbsp; &nbsp; &nbsp; &nbsp; configurer.setLocations(new PathMatchingResourcePatternResolver().getResources(getResourcesFromPath(PROFILE_DEV)));//Loads all properties files from the path&nbsp; &nbsp; &nbsp; &nbsp; configurer.setIgnoreUnresolvablePlaceholders(true);&nbsp; &nbsp; &nbsp; &nbsp; return configurer;&nbsp; &nbsp; }&nbsp; &nbsp; @Bean&nbsp; &nbsp; @Profile(PROFILE_STAGE)&nbsp; &nbsp; public static PropertySourcesPlaceholderConfigurer stagePropertyPlaceholderConfigurer() throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; LOG.info("Initializing {} properties.", PROFILE_STAGE);&nbsp; &nbsp; &nbsp; &nbsp; PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();&nbsp; &nbsp; &nbsp; &nbsp; configurer.setLocations(new PathMatchingResourcePatternResolver().getResources(getResourcesFromPath(PROFILE_STAGE)));&nbsp; &nbsp; &nbsp; &nbsp; return configurer;&nbsp; &nbsp; }&nbsp; &nbsp; @Bean&nbsp; &nbsp; @Profile(PROFILE_PROD )&nbsp; &nbsp; public static PropertySourcesPlaceholderConfigurer prodPropertyPlaceholderConfigurer() throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; LOG.info("Initializing {} properties.", PROFILE_PROD );&nbsp; &nbsp; &nbsp; &nbsp; PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();&nbsp; &nbsp; &nbsp; &nbsp; configurer.setLocations(new PathMatchingResourcePatternResolver().getResources(getResourcesFromPath(PROFILE_PROD )));&nbsp; &nbsp; &nbsp; &nbsp; return configurer;&nbsp; &nbsp; }&nbsp; &nbsp; private static String getResourcesFromPath(String path) {&nbsp; &nbsp; &nbsp; &nbsp; return PATH_TEMPLATE.replaceFirst("%s", path);&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答