spring容器读取.properties结尾的配置文件

package cn.*.cache.util;

import java.util.HashMap;
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  xxx
 * @version  [版本号, 2017年5月17日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class PropertiesConfig extends PropertyPlaceholderConfigurer
{
    private static final Map<String, String> propertyMap = new HashMap<String, String>();
    
    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
        throws BeansException
    {
        super.processProperties(beanFactoryToProcess, props);
        if (propertyMap.isEmpty())
        {
            for (Object key : props.keySet())
            {
                String keyStr = key.toString();
                String value = props.getProperty(keyStr);
                propertyMap.put(keyStr, value);
            }
        }
    }
    /**
     * 
     * 
     *
     * @author xxxx
     * @param name
     * @param def
     * @return
     */
    public static String getProperty(String name,String def)
    {
        if (propertyMap == null || propertyMap.isEmpty() || null == propertyMap.get(name))
        {
            return def;
        }
        return propertyMap.get(name);
    }
    /**
     * 
     * 
     *
     * @author xxxx
     * @param name
     * @return
     */
    public static String getProperty(String name)
    {
        if (propertyMap == null || propertyMap.isEmpty())
        {
            return null;
        }
        return propertyMap.get(name);
    }
    
    /**
     * 
     * 
     *
     * @author xxxx
     * @param name
     * @param paramInt
     * @return
     */
    public static int getInt(String name , int paramInt)
    {
        if(propertyMap.containsKey(name))
        {
            try
            {
                return Integer.valueOf(propertyMap.get(name));
            }
            catch (NumberFormatException e)
            {
                return paramInt;
            }
        }
        
        return paramInt;
    }
    /**
     * 获取接口设置的并发控制量
     * 
     *
     * @author xxxx
     * @param name
     * @return
     */
    public static int getForumLimit(String name)
    {
        int value = 50;
        if (propertyMap.containsKey(name))
        {
            value = getInt(name,50);
        }
        else
        {
            value = getInt("default_forum_controller", 50);
        }
        
        return value;
    }
    
    /**
     * 如果没有配置该接口URL,则取默认URL
     */
    public static String getUrlPrefix(String name)
    {
        return getUrlPrefix(name, "default");
    }
    
    /**
     * 获取接口配置地址
     * @param name 接口名
     * @param defName 默认配置
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String getUrlPrefix(String name, String defName)
    {
        String value = null;
        if (propertyMap.containsKey(name))
        {
            value = propertyMap.get(name);
        }
        else
        {
            value = propertyMap.get(defName);
        }
        
        return value;
    }
}
// 还要在spring配置文件中加入 扫描文件配置

<bean id="PropertyConfig" class="上面那个类的全路径">
        <property name="locations">
            <list>
                <value>classpath:sysconfig.properties</value>
                <value>classpath:配置文件全名,例如上面</value>
            </list>
        </property>
        <property name="fileEncoding">
            <value>UTF-8</value>
        </property>
        <property name="ignoreResourceNotFound" value="true" /> // 加载不到忽略
        <property name="ignoreUnresolvablePlaceholders" value="true" />
    </bean>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值