【问题标题】:Define Spring @PropertySource in xml and use it in Environment在xml中定义Spring @PropertySource 并在Environment中使用
【发布时间】:2012-11-24 04:29:45
【问题描述】:

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

@PropertySource("classpath:application.properties")

@Inject private Environment environment;

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

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

【问题讨论】:

    标签: java spring properties spring-environment


    【解决方案1】:

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

    首先,测试:

    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);
    
        }
    
    }
    

    然后上课:

    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:

    <?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 文件:

    hello=world
    

    这很简单,只需使用ApplicationContextAware bean 并从(Web)ApplicationContext 获取ConfigurableEnvironment。然后只需将PropertiesPropertySource 添加到MutablePropertySources

    【讨论】:

    • 当您使用 @ImportResource("classpath:properties.xml") 指向 xml 时,它不适用于 @Configure 类,但是当您使用 applicationcontext 加载它时,就像在您的示例中一样。
    【解决方案2】:

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

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

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

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

    【讨论】:

    • 问题是使用 Environment 检索属性而不是使用属性占位符。
    猜你喜欢
    • 2013-08-12
    • 2019-10-07
    • 2020-12-19
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 2020-10-17
    相关资源
    最近更新 更多