Spring加载资源文件并静态化

说明:

      在实际项目中有时候需要用到很多的配置文件,而这些配置文件我们又想按功能分文件加载。

 

第一步

      我们需要在Spring的配置文件中加入一段配置,配置如下

 <bean id="propertyBean" class="com.funo.util.WcityProperty">
  <property name="locations">
   <list>
    <value>/WEB-INF/config/wcity-*.properties</value>
    </list>
  </property>
 </bean> 

      以上配置中我们需要建立一个类com.funo.util.WcityProperty 这个类继承了Spring读取资源文件的PropertyPlaceholderConfigurer类,该类可以帮你自动加载配置文件到全局资源文件中。当然<value>/WEB-INF/config/wcity-*.properties</value>部分你也可以读取外部的文件如:<value>file:c:/wcity-*.properties</value> 还可以配置他的编码,不过我建议使用全局编码。

 

第二步

      我们需要开始写com.funo.util.WcityProperty这个类

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

/**
 * 采用Spring的加载属性信息 在应用中获取
 * 
 * @author zhys513
 * 
 */
public class WcityProperty extends PropertyPlaceholderConfigurer {

    private Map<String, String> resolvedProps;// 将属性保存起来

    @SuppressWarnings("unchecked")
    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
            throws BeansException {
        super.processProperties(beanFactoryToProcess, props);
        resolvedProps = new HashMap<String, String>();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            resolvedProps.put(keyStr, parseStringValue(props.getProperty(keyStr), props, new HashSet()));
        }
    }

    public Map<String, String> getResolvedProps() {
        return resolvedProps;
    }

    public void setResolvedProps(Map<String, String> resolvedProps) {
        this.resolvedProps = resolvedProps;
    }
}

 这个类其实只是做了一个转换,到这里,直接从Spring的上下文已经可以取到这个propertyBean,但是为了更方便使用我们需要把Spring加载进来的资源文件读取到一个静态的常量工具类中,代码如下:

 

import java.util.HashMap;
import java.util.Map;

/**
 * 表常量类
 * zhys513
 */
public class WcityConstant {

    @SuppressWarnings("unchecked")
    private static HashMap properties;

    @SuppressWarnings("unchecked")
    public static void setProperties(Map<String, String> maps) {
        WcityConstant.properties = (HashMap) maps;
    }

    /**
     * 通过key,查找property中key的内容
     * 
     * @param key
     *            property的key
     * @return 返回对应的value
     */
    public static String getProValue(String key) {
        String returnValue = (String)properties.get(key);
        if(returnValue == null || "".equals(returnValue)) 
            returnValue = "";
        return returnValue.trim();
    } 
}

到这里这个常量类还不能使用。我们还需要把这个常量累和资源文件建立一个关系。想到的方法是启动的时候直接加载,如果有需要每几分钟就加载一次的童鞋可以另外配置。这里就不多说了。以下为监听器代码:

import java.util.Map;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class WcityServletContextListener implements ServletContextListener {
    @SuppressWarnings("unused")
    private static final Log log = LogFactory.getLog(WcityServletContextListener.class);

    public void contextInitialized(ServletContextEvent event) {
        WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
        WcityProperty propertyBean = (WcityProperty) wac.getBean("propertyBean");
        Map<String, String> maps = propertyBean.getResolvedProps();
        WcityConstant.setProperties(maps);
    }

    public void contextDestroyed(ServletContextEvent event) {

    }

}

 既然我们使用到了监听器,那就需要配置下监听器,WEB.xml监听器的配置如下:

<listener>
    	<listener-class>com.funo.util.WcityServletContextListener</listener-class></listener>

 

资源文件里面我就不多说了。大家都懂。如果资源文件里面需要放中文字,请设置编码。资源文件:

# 平台验证登录开关 0 0FF  1ON
PORTAL_CHECK_LOGIN_STATUS=0

 

具体使用如下:

    

WcityConstant.getProValue("PORTAL_CHECK_LOGIN_STATUS")

 

直到这里,可以由Spring模糊加载资源文件并在类中直接使用。

 

 来源:优特产     个人博客:优特品

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值