java Spring 占位符不解析 JavaConfig 中的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29140794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:45:01  来源:igfitidea点击:

Spring placeholder doesn't resolve properties in JavaConfig

javaspringproperties-filespring-java-config

提问by herau

Currently i have a Spring xml configuration (Spring 4) which load a properties file.

目前我有一个加载属性文件的 Spring xml 配置(Spring 4)。

context.properties

上下文属性

my.app.service = myService
my.app.other = ${my.app.service}/sample

Spring xml configuration

Spring xml 配置

<bean id="contextProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="fileEncoding" value="UTF-8" />
    <property name="locations">
        <list>
            <value>classpath:context.properties</value>
        </list>
    </property>
</bean>
<bean id="placeholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="properties" ref="contextProperties" />
    <property name="nullValue" value="@null" />
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
</bean>

Bean which use the properties

使用属性的Bean

@Component
public class MyComponent {

    @Value("${my.app.other}")
    private String others;

}

This works perfectly and the othersvalue is MyService/sample, as excepted. But when i try to replace this configuration by a JavaConfig, the @Valuein my component doesn't works in the same way. the value isn't myService/samplebut ${my.app.service}/sample.

这完美地工作并且others值是MyService/sample,作为例外。但是当我尝试用 JavaConfig 替换此配置时@Value,我的组件中的工作方式不同。值不是myService/sample但是${my.app.service}/sample

@Configuration
@PropertySource(name="contextProperties", ignoreResourceNotFound=true, value={"classpath:context.properties"})
public class PropertiesConfiguration {

    @Bean
    public static PropertyPlaceholderConfigurer placeholder() throws IOException {
        PropertyPlaceholderConfigurer placeholder = new PropertyPlaceholderConfigurer();
        placeholder.setNullValue("@null");
        placeholder.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
        return placeholder;
    }

}

Did i miss something in the conversion from xml to Javaconfig ?

我在从 xml 到 Javaconfig 的转换中遗漏了什么吗?

ps : I also try to instantiate a PropertySourcesPlaceholderConfigurerinstead of a PropertyPlaceholderConfigurerwithout more success.

ps:我也尝试实例化 aPropertySourcesPlaceholderConfigurer而不是 aPropertyPlaceholderConfigurer没有更多的成功。

采纳答案by Mithun

Update to use configure PropertySourcesPlaceholderConfigurer. Just having @PropertySourceannotation will not be sufficient:

更新以使用 configure PropertySourcesPlaceholderConfigurer。仅仅有@PropertySource注释是不够的:

@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
    return new PropertySourcesPlaceholderConfigurer();
}

@PropertySourceannotation does not automatically register a PropertySourcesPlaceholderConfigurerwith Spring. Hence we need to explicitly configure PropertySourcesPlaceholderConfigurer

@PropertySource注解不会自动PropertySourcesPlaceholderConfigurer向 Spring注册 a 。因此我们需要显式配置PropertySourcesPlaceholderConfigurer

Below JIRA ticket has more information on the rationale behind this design:

下面 JIRA 票有更多关于这种设计背后的基本原理的信息:

https://jira.spring.io/browse/SPR-8539

https://jira.spring.io/browse/SPR-8539

UPDATE:Created simple Spring boot application to use nested properties. It is working fine with the above configuration.

更新:创建了简单的 Spring Boot 应用程序以使用嵌套属性。使用上述配置可以正常工作。

https://github.com/mgooty/property-configurer/tree/master/complete

https://github.com/mgooty/property-configurer/tree/master/complete

回答by Alfredo Diaz

Another option is to import PropertyPlaceholderAutoConfiguration.class.

另一种选择是导入 PropertyPlaceholderAutoConfiguration.class。

import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;

@Import(PropertyPlaceholderAutoConfiguration.class)

The annotation includes a PropertySourcesPlaceholderConfigurer in the context if it doesn't exist.

如果不存在,则注释在上下文中包含一个 PropertySourcesPlaceholderConfigurer。