首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在xml中定义Spring @PropertySource并在环境中使用它

在xml中定义Spring @PropertySource并在环境中使用它
EN

Stack Overflow用户
提问于 2012-12-06 15:08:49
回答 2查看 15.4K关注 0票数 18

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

代码语言:javascript
复制
@PropertySource("classpath:application.properties")

@Inject private Environment environment;

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

代码语言:javascript
复制
<context:property-placeholder location="classpath:application.properties" />
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-07 00:03:31

AFAIK,纯XML是无法做到这一点的。不管怎样,这是我今天早上做的一小段代码:

首先,测试:

代码语言:javascript
复制
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);

    }

}

然后这个类:

代码语言:javascript
复制
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;

    }

}

和testContext.xml:

代码语言:javascript
复制
<?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>

和props.properties文件:

代码语言:javascript
复制
hello=world

这非常简单,只需使用ApplicationContextAware bean并从(Web)ApplicationContext获取ConfigurableEnvironment即可。然后,只需向MutablePropertySources添加一个PropertiesPropertySource

票数 7
EN

Stack Overflow用户

发布于 2016-11-14 07:17:32

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

代码语言:javascript
复制
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="application.properties"/>
</bean>

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

代码语言:javascript
复制
<bean id="myBean" class="foo.bar.MyClass">
        <property name="myProperty" value="${xx}"/>
</bean>
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13738623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档