我们正在开发 Spring Boot 2.1.6,我们需要在我们的应用程序中实现 spring boot 配置文件
我们的项目中当前有两个属性文件 application.properties 和 bucket.properties(s3 配置)文件。
因此,我们为开发环境创建了两个属性文件 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 的配置。
最佳答案
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);
}
}
关于java - Spring Boot 配置文件不选择属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58343008/