使用自定义ClassPathResource配置Spring 4 PropertySourcesPlaceholderConfigurer

时间:2015-04-17 16:38:41

标签: java spring maven

如何为不同的环境(生产,开发,登台)配置带有自定义.properties文件的PropertySourcesPlaceholderConfigurer?在部署春季投掷"无法解决占位符&property; placeholder'在字符串值" classpath:$ {property.placeholder}" "

这是我的pom.xml

<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>

这里是PropertySourcesPlaceholderConfigurer配置

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

它适用于xml spring配置,但如果我使用java配置则不起作用。知道它是如何工作的吗?

2 个答案:

答案 0 :(得分:1)

我找到了另一种方法来做我想要的事情:

首先,我将此插件添加到我的pom.xml中的构建部分:

public static void Look(LEBAEntities db, object obj) {
    // if getPrimaryKey(obj) == 0
    db.Entry(obj).State = EntityState.Added;

    // else
    db.Entry(obj).State = EntityState.Modified;
} 

第二次,我在spring配置文件中修改了PropertySourcesPlaceholder配置:

<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配置类:

static @Bean
public PropertySourcesPlaceholderConfigurer myPropertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer p 
        = new PropertySourcesPlaceholderConfigurer();
    org.springframework.core.io.Resource[] resourceLocations 
        = new org.springframework.core.io.Resource[] {
            new ClassPathResource("application.properties")
    };
    p.setLocations(resourceLocations);
    return p;
}

所以现在我将env依赖属性写入不同的属性文件(如“_developer.properties”或“_staging.properties”),当我用maven构建项目时,它已被复制到“application.properties”,我使用它在PropertySourcesPlaceholder配置

答案 1 :(得分:0)

使用System.getProperty()获取环境变量。

示例代码:

Environment env = new Environment(new ClassResourceLocator(ClassUtils.getDefaultClassLoader()), System.getProperty("env"));

创建PropertySourcesPlaceholderConfigurer 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);
    }
}