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>