java 在xml中定义Spring@PropertySource并在Environment中使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13738623/
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-10-31 13:50:21  来源:igfitidea点击:

Define Spring @PropertySource in xml and use it in Environment

javaspringpropertiesspring-environment

提问by surajz

In spring JavaConfig, I can define property source and inject into Environment

在spring JavaConfig中,我可以定义属性源并注入到环境中

@PropertySource("classpath:application.properties")

@Inject private Environment environment;

How do I do that if in xml? I am using context:property-placeholder, and on the JavaConfig class @ImportResource to import the xml. But I cannot retrieve property defined in the properties file using environment.getProperty("xx")

如果在 xml 中我该怎么做?我正在使用 context:property-placeholder,并在 JavaConfig 类 @ImportResource 上导入 xml。但是我无法使用 environment.getProperty("xx") 检索属性文件中定义的属性

<context:property-placeholder location="classpath:application.properties" />

采纳答案by ElderMael

AFAIK, there is no way of doing this by pure XML. Anyways, here is a little code I did this morning:

AFAIK,没有办法通过纯 XML 来做到这一点。无论如何,这是我今天早上做的一个小代码:

First, the test:

一、测试:

public class EnvironmentTests {

    @Test
    public void addPropertiesToEnvironmentTest() {

        ApplicationContext context = new ClassPathXmlApplicationContext(
                "testContext.xml");

        Environment environment = context.getEnvironment();

        String world = environment.getProperty("hello");

        assertNotNull(world);

        assertEquals("world", world);

        System.out.println("Hello " + world);

    }

}

Then the class:

然后是班级:

public class PropertySourcesAdderBean implements InitializingBean,
        ApplicationContextAware {

    private Properties properties;

    private ApplicationContext applicationContext;

    public PropertySourcesAdderBean() {

    }

    public void afterPropertiesSet() throws Exception {

    PropertiesPropertySource propertySource = new PropertiesPropertySource(
            "helloWorldProps", this.properties);

    ConfigurableEnvironment environment = (ConfigurableEnvironment) this.applicationContext
            .getEnvironment();

    environment.getPropertySources().addFirst(propertySource);

    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {

        this.applicationContext = applicationContext;

    }

}

And the testContext.xml:

和 testContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans ...>

    <util:properties id="props" location="classpath:props.properties" />

    <bean id="propertySources" class="org.mael.stackoverflow.testing.PropertySourcesAdderBean">
        <property name="properties" ref="props" />
    </bean>


</beans>

And the props.properties file:

和 props.properties 文件:

hello=world

It is pretty simple, just use a ApplicationContextAwarebean and get the ConfigurableEnvironmentfrom the (Web)ApplicationContext. Then just add a PropertiesPropertySourceto the MutablePropertySources

这很简单,只需使用一个ApplicationContextAwarebean 并ConfigurableEnvironment(Web)ApplicationContext. 然后,只需添加一个PropertiesPropertySourceMutablePropertySources

回答by Juan

If what you need is just access the property "xx" of the file "application.properties", you can accomplish that without Java code by declaring the following bean in your xml file:

如果您需要的只是访问文件“application.properties”的属性“xx”,则可以通过在 xml 文件中声明以下 bean 来实现,无需 Java 代码:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="application.properties"/>
</bean>

Then if you want to inject the property in a bean just reference it as a variable:

然后,如果您想在 bean 中注入该属性,只需将其作为变量引用:

<bean id="myBean" class="foo.bar.MyClass">
        <property name="myProperty" value="${xx}"/>
</bean>