guration文件与PropertyPlaceholderCo

guration文件与PropertyPlaceholderCo

本文介绍了Spring @Configuration文件与PropertyPlaceholderConfigurer bean不解析@Value注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下配置文件:

@Configuration
public class PropertyPlaceholderConfigurerConfig {

    @Value("${property:defaultValue}")
    private String property;

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



我使用以下VM选项运行我的应用程序:

I run my application with following VM option:

-Dproperty=propertyValue

所以我想让我的应用程序在启动时加载特定的属性文件。但由于某些原因,在这个阶段 @Value 注释不处理,属性 null 。另一方面,如果我有 PropertyPlaceholderConfigurer 通过xml文件配置 - 一切正常工作。 Xml文件示例:

So I'd like my application to load specific property file on startup. But for some reason at this stage @Value annotations are not processed and property is null. On the other hand if I have PropertyPlaceholderConfigurer configured via xml file - everything works perfectly as expected. Xml file example:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="location">
        <value>classpath:properties/${property:defaultValue}.properties</value>
    </property>
</bean>

如果我尝试在另一个Spring配置文件中注入属性值,如果我将我的 PropertyPlaceholderConfigurer bean创建移动到该配置文件 - 字段值再次为null。

If I try to to inject property value in another Spring configuration file - it is properly injected. If I move my PropertyPlaceholderConfigurer bean creation to that configuration file - field value is null again.

作为解决方法,我使用这行代码:

As workaround, I use this line of code:

System.getProperties().getProperty("property", "defaultValue")

我想知道为什么会发生这种行为,也许可以用其他方式重写它,但是没有xml?

Which is also works, but I'd like to know why such behavior is occurs and maybe it is possible to rewrite it in other way but without xml?

推荐答案

从春天:

因此,您尝试在启用占位符处理所需的代码块中使用占位符。

So, you are trying to use a placeholder in the code block required to enable placeholder processing.

As @ M.Deinum提到,你应该使用一个PropertySource(默认或自定义实现)。

As @M.Deinum mentioned, you should use a PropertySource (default or custom implementation).

下面的示例显示如何在PropertySource注释中使用属性以及如何从PropertySource注入属性到字段中。

Example below shows how to use properties in a PropertySource annotation as well as how to inject properties from the PropertySource in a field.

@Configuration
@PropertySource(
          value={"classpath:properties/${property:defaultValue}.properties"},
          ignoreResourceNotFound = true)
public class ConfigExample {

    @Value("${propertyNameFromFile:defaultValue}")
    String propertyToBeInjected;

    /**
     * Property placeholder configurer needed to process @Value annotations
     */
     @Bean
     public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
     }
}

这篇关于Spring @Configuration文件与PropertyPlaceholderConfigurer bean不解析@Value注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 06:05