Spring引导application.properties重用值

时间:2017-06-19 08:28:59

标签: spring-boot properties-file

我的问题似乎很简单,但我找不到实现它的方法。

考虑以下情况:

prop1 = value1
prop2 = value2
prop3 = value3

prop4 = value2     (Value same as prop2)
prop5 = value3     (Value same as prop3)

如何重用值2和3(这些实际上是数据库特定的属性),因为我希望用户只提供一次而不重复它。

感谢。

案例1: 我会详细说明我的情况如下: 我有两个属性文件 - application.propertiesquartz.properties

application.properties:

prop1 = value1
prop2 = value2
prop3 = value3

quartz.properties

prop4 = value2     (Value same as prop2)
prop5 = value3     (Value same as prop3)

请注意:我无法合并两个属性文件的内容,并且出于某种原因将它们分开放置。

在正常情况下,我预计$ {}可以工作,但是当我想要属性在两个不同的文件中时,它不起作用。

案例2:[简单场景] 我尝试只使用一个application.properties。但即使这样,我也无法在同一属性文件中重用属性值。

我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.abc</groupId>
    <artifactId>abc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>xyz</name>
    <description>xyz</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <!-- Quartz dependencies start -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>

        <!-- Includes spring's support classes for quartz -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>

        <!-- Quartz dependencies End -->
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

1 个答案:

答案 0 :(得分:2)

您可以像这样使用占位符:

prop1 = value1
prop2 = value2
prop3 = value3

prop4 = ${prop2}     (Value same as prop2)
prop5 = ${prop3}     (Value same as prop3)

但是请注意,这应该在application.properties,所以春天就知道了。如果您希望自定义文件具有属性,则配置PropertyPlaceholderConfigurer是有意义的,如下所示:

@Configuration
public class PropertyPlaceholderConfigurerConfig {

    @Bean
    public static PropertyPlaceholderConfigurer ppc() throws IOException {
        PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        ppc.setLocations(new ClassPathResource("custom.properties"));
        ppc.setIgnoreUnresolvablePlaceholders(true);
        return ppc;
    }
}

现在你应该知道:

@Value("${prop4}")
private String someValue;

它应该在你的bean中注入正确的值。