【问题标题】:Spring placeholder doesn't resolve properties in JavaConfigSpring占位符不解析JavaConfig中的属性
【发布时间】:2015-05-22 08:14:17
【问题描述】:

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

context.properties

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

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

@Component
public class MyComponent {

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

}

这很好用,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;
    }

}

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

ps:我也尝试实例化 PropertySourcesPlaceholderConfigurer 而不是 PropertyPlaceholderConfigurer ,但没有取得更多成功。

【问题讨论】:

  • 即使不能解决你的问题,我可以确认你应该使用PropertySourcesPlaceholderConfigurer,而不是PropertyPlaceholderConfigurer
  • my.app.service 属性解析是否正常?检查@Value("${my.app.service}"),如果问题来自属性嵌套,我会徘徊。

标签: java spring properties-file spring-java-config


【解决方案1】:

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

import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;

@Import(PropertyPlaceholderAutoConfiguration.class)

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

【讨论】:

    【解决方案2】:

    更新为使用配置PropertySourcesPlaceholderConfigurer。仅仅有@PropertySource 注释是不够的:

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

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

    以下 JIRA 票证有更多关于此设计背后原理的信息:

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

    更新: 创建了简单的 Spring Boot 应用程序以使用嵌套属性。上面的配置运行良好。

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

    【讨论】:

    • 我认为 OP 已经这样做了,但没有奏效,请参阅问题中的“ps”。如果问题似乎与属性嵌套有关,我会徘徊(my.app.other = ${my.app.service}/sample
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 2014-03-23
    • 2015-01-16
    • 2019-08-18
    • 2017-01-12
    • 2016-03-04
    • 1970-01-01
    相关资源
    最近更新 更多