Java 使用自定义类路径资源配置Spring4属性资源占位符配置器
如何为不同的环境(生产环境、开发环境、暂存环境)配置带有自定义.properties文件的PropertySourcesPlaceholderConfigurer?在部署时,spring抛出“无法解析字符串值中的占位符”property.placeholder“类路径:${property.placeholder}” 这是我的pom.xmlJava 使用自定义类路径资源配置Spring4属性资源占位符配置器,java,spring,maven,Java,Spring,Maven,如何为不同的环境(生产环境、开发环境、暂存环境)配置带有自定义.properties文件的PropertySourcesPlaceholderConfigurer?在部署时,spring抛出“无法解析字符串值中的占位符”property.placeholder“类路径:${property.placeholder}” 这是我的pom.xml <profiles> <profile> <id>dev</id>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<property.placeholder>_developer.properties</property.placeholder>
</properties>
</profile>
<profile>
<id>staging</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<property.placeholder>_staging.properties</property.placeholder>
</properties>
</profile>
<profile>
<id>production</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<property.placeholder>_production.properties</property.placeholder>
</properties>
</profile>
</profiles>
它在XMLSpring配置中工作,但如果我使用java配置,它就不工作。你知道怎么做吗 使用
System.getProperty()
获取环境变量
示例代码:
Environment env = new Environment(new ClassResourceLocator(ClassUtils.getDefaultClassLoader()), System.getProperty("env"));
创建属性资源占位符配置器Bean:
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer()
{
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setPropertySources(new MyPropertySources());
return propertySourcesPlaceholderConfigurer;
}
MyPropertySources.java
public class MyPropertySources implements PropertySources{
private List<PropertySource<?>> sources = new ArrayList<>();
public EnvironmentPropertySources()
{
Environment env = new Environment(new ClassResourceLocator(ClassUtils.getDefaultClassLoader()), System.getProperty("env"));
PropertySource<?> source = ...;
// create class that extends PropertySource<String> and get property values from Environment
sources.add(source);
}
@Override
public Iterator<PropertySource<?>> iterator() {
return sources.iterator();
}
@Override
public boolean contains(String name) {
return true;
}
@Override
public PropertySource<?> get(String name) {
return sources.get(0);
}
}
公共类MyPropertySources实现PropertySources{
私有列表源=。。。;
//创建扩展PropertySource并从环境中获取属性值的类
来源。添加(来源);
}
@凌驾
公共迭代器get(字符串名称){
返回源。获取(0);
}
}
我找到了另一种方法来做我想做的事:
首先,我将此插件添加到我的pom.xml中的builds部分:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<id>1</id>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>src/main/resources/${property.placeholder}</file>
</files>
</configuration>
</execution>
<execution>
<id>2</id>
<phase>generate-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
<configuration>
<outputFile>
${project.build.outputDirectory}/application.properties
</outputFile>
</configuration>
</execution>
</executions>
</plugin>
我还将此注释添加到我的spring配置类中:
@PropertySource("classpath:application.properties")
因此,现在我将依赖于环境的属性写入不同的属性文件(如“\u developer.properties”或“\u staging.properties”),当我使用maven构建项目时,它已复制到“application.properties”,我在PropertySourcesPlaceholder配置中使用它
static @Bean
public PropertySourcesPlaceholderConfigurer myPropertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer p = new PropertySourcesPlaceholderConfigurer();
org.springframework.core.io.Resource[] resourceLocations = new org.springframework.core.io.Resource[] {
new ClassPathResource("${property.placeholder}")
};
p.setLocations(resourceLocations);
return p;
}
@PropertySource("classpath:application.properties")