《玩转Spring》第一章 PropertyPlaceholderConfigurer扩展


引——


做了那么多j2ee项目,一直在使用Spring,积累了很多不错的实践,如利用Spring扩展点完成特殊需求,如何对Spring项目进行单元测试,如何优化Spring的配置文件,Spring对其他框架的扩展支持等等,想通过一个系列文章和大家分享下,没有严格的顺序,哪些有意思就先写哪些。



在使用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的值,不用我多说了吧。

这样做最直接的好处是,不用我们再手写代码读取配置文件了,而且设计看起来更优雅。

下篇继续……


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值