Spring环境属性源配置 [英] Spring Environment Property Source Configuration

查看:240
本文介绍了Spring环境属性源配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用名为"Config"的实用程序类在应用程序库上工作,该实用程序类由Spring Environment对象支持,并为所有应用程序配置值提供强类型的吸气剂.

I'm working on an application library with a utility class called "Config" which is backed by the Spring Environment object and provides strongly typed getters for all the applications configuration values.

用于配置的属性源可以根据环境(DEV/PROD)和使用情况(独立/测试/Web应用程序)而有所不同,范围可以从默认值(系统和环境道具)到自定义数据库和JNDI源.

The property sources for the configuration can vary depending on environment (DEV/PROD) and usage (standalone/test/webapp), and can range from the default ones (system & env props) to custom database and JNDI sources.

我正在苦苦挣扎的是如何让使用此库的应用轻松配置Environment所使用的属性源,以使这些属性可在我们的Config类中以及通过PropertySourcesPlaceholderConfigurer.

What I'm struggling with is how to let the apps consuming this library easily configure the property source(s) used by Environment, such that the properties are available for use in our Config class and via the PropertySourcesPlaceholderConfigurer.

我们仍在使用XML配置,因此理想情况下可以使用类似XML的配置.

We're still using XML configuration, so ideally this could be configured in XML something like.

<bean id="propertySources" class="...">
    <property name="sources">
        <list>
            <ref local="jndiPropertySource"/>
            <ref local="databasePropertySource"/>
        </list>
    </property>
</bean>

...然后以某种方式将其注入到环境的属性源集合中.

...and then injected somehow into the Environment's property sources collection.

我已经读到,由于应用程序上下文生命周期的时间安排,可能无法实现此类操作,并且可能需要使用应用程序初始化程序类来完成此操作.

I've read that something like this may not be possible due to the timing of the app context lifecycle, and that this may need to be done using an application initializer class.

有什么想法吗?

推荐答案

我想出了以下似乎可行的方法,但是我对Spring还是很陌生,所以我不确定它在下面如何保持不同的用例.

I came up with the following which seems to work, but I'm fairly new to Spring, so I'm not so sure how it will hold up under different use cases.

基本上,该方法是扩展PropertySourcesPlaceholderConfigurer并添加一个setter,以允许用户轻松地配置XML中的PropertySource对象列表.创建后,属性源将复制到当前的Environment.

Basically, the approach is to extend PropertySourcesPlaceholderConfigurer and add a setter to allow the user to easily configure a List of PropertySource objects in XML. After creation, the property sources are copied to the current Environment.

这基本上允许将属性源配置在一个位置,但同时由placholder配置和Environment.getProperty场景使用.

This basically allows the property sources to be configured in one place, but used by both placholder configuration and Environment.getProperty scenarios.

扩展了PropertySourcesPlaceholderConfigurer

Extended PropertySourcesPlaceholderConfigurer

public class ConfigSourcesConfigurer 
        extends PropertySourcesPlaceholderConfigurer
        implements EnvironmentAware, InitializingBean {

    private Environment environment;
    private List<PropertySource> sourceList;

    // Allow setting property sources as a List for easier XML configuration
    public void setPropertySources(List<PropertySource> propertySources) {

        this.sourceList = propertySources;
        MutablePropertySources sources = new MutablePropertySources();
        copyListToPropertySources(this.sourceList, sources);        
        super.setPropertySources(sources);
    }

    @Override
    public void setEnvironment(Environment environment) {
        // save off Environment for later use
        this.environment = environment;
        super.setEnvironment(environment);
    }

    @Override
    public void afterPropertiesSet() throws Exception {

        // Copy property sources to Environment
        MutablePropertySources envPropSources = ((ConfigurableEnvironment)environment).getPropertySources();
        copyListToPropertySources(this.sourceList, envPropSources);
    }

    private void copyListToPropertySources(List<PropertySource> list, MutablePropertySources sources) {

        // iterate in reverse order to insure ordering in property sources object
        for(int i = list.size() - 1; i >= 0; i--) {
            sources.addFirst(list.get(i));
        }
    }
}

beans.xml文件显示基本配置

<beans>
    <context:annotation-config/>
    <context:component-scan base-package="com.mycompany" />

    <bean class="com.mycompany.ConfigSourcesConfigurer">
        <property name="propertySources">
            <list>
                <bean class="org.mycompany.CustomPropertySource" />
                <bean class="org.springframework.core.io.support.ResourcePropertySource">
                    <constructor-arg value="classpath:default-config.properties" />
                </bean>
            </list>
        </property>
    </bean>
    <bean class="com.mycompany.TestBean">
        <property name="stringValue" value="${placeholder}" />
    </bean>
</beans>

这篇关于Spring环境属性源配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆