您的位置:首页 > 编程语言 > Java开发

扩展spring类实现自动读取配置文件

2014-04-15 11:25 751 查看
在使用spring的时候,我们使用Properties配置器把properties文件装载到Spring的上下文中,如下:

<!-- Loads a Properties instance -->
<util:properties id="evn" location="classpath:config/jndi.properties"></util:properties>

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

这样在Spring的配置文件中可以用表达式来获得load进来的properties内容,如下:

<jee:jndi-lookup jndi-name="${ejb-name}" id="login-loginBean"
lookup-on-startup="true" cache="true" proxy-interface="${ejb-interface}"
environment-ref="evn">
</jee:jndi-lookup>

既然spring已经能把properties读到它的上下文,在配置文件中轻松地使用,那么是不是也可以想办法让我们在代码中也能取到这些值?

那我们何不利用这一点,让我们能从代码中也使用这些值。

整理一下思路:

1、准备好配置文件,将properties的值设置到Bean中

2、初始化Spring上下文,加载properties文件

3、可以通过相应的bean去访问需要的properties的值

第1点很简单,不多说

第2点初始化Spring上下文,有很多方式,大家可以根据自己的方式选择其一即可。

方式1:

FileSystemXmlApplicationContext——从指定的目录中加载

ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");

方式2:

ClassPathXmlApplicationContext——从classpath路径加载:

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

方式3:

ServletContext servletContext = servlet.getServletContext();

WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

方式4:http://blog.csdn.net/dqatsh/article/details/3469278

第3步,查资料知道Spring使用

org.springframework.beans.factory.config.PropertyPlaceholderConfigurer 读取properties文件。

我们可以扩展这个类来满足我们的需求。 怎么办?继承!

/**
 * @author     : jl
 * @group      : tgb8
 * @Date       : 2014-4-4 下午4:38:19
 * @Comments   : 方便开发人员使用可以使用PropertyConfigurer.getContextProperty()来取得上下文中的properties的值了。
 * @Version    : 1.0.0
 */
public class PropertyConfigure extends PropertyPlaceholderConfigurer {
    private static Map<String, Object> ctxPropertiesMap;
    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactory,
            Properties props) throws BeansException {
        super.processProperties(beanFactory, props);
        
        ctxPropertiesMap = new HashMap<String, Object>();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            String value = props.getProperty(keyStr);
            ctxPropertiesMap.put(keyStr, value);
        }
    }
    public static Object getContextProperty(String name) {
        //初始化全局配置
        initApplicationContext("context-properties.xml");
        return ctxPropertiesMap.get(name);
    }
    
    private static void initApplicationContext(String key) {
        CacheHandler cacheHandler = CacheHandler.getInstance();
        Cache cache = cacheHandler.getCacheByName("ApplicationContext");
        
        //如果没有初始化ApplicationContext,则初始化,并放入缓存,防止下次再初始化
        if (cache == null) {
            System.out.println("创建ApplicationContext缓存");
            cache = cacheHandler.addCache("ApplicationContext");
            System.out.println("初始化ApplicationContext,key="+key);
            ApplicationContext context = new ClassPathXmlApplicationContext(
                    key);
            Element elementVal = new Element(key, context);
            cache.put(elementVal);
        }
    }
}


那么我们如何在Spring中配置呢?

<bean id="configBean" class="common.tool.config.PropertyConfigure">
<!-- <property name="location" value="classpath:url.properties" /> -->
<property name="locations">
<list>
<value>url.properties</value>
<value>wsdl.properties</value>
</list>
</property>
</bean>

怎么在代码中获取properties的值,不用我多说了吧。

这样做最直接的好处是,不用我们再手写代码读取配置文件了,而且设计看起来更优雅。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: