公司配置表中的数据,一改动就需要重新去启动服务器,因为这些数据是放在了map缓存中,这样很麻烦,就写了一个方法,在改变数据的时候,执行一下方法,更新一下缓存,这样就避免了重新启动服务器。
public class DeleteMapCacheController {
@Resource
private AdvChannelConfigMapper configMapper;
@Resource
private SystemPropertyMapper dao;
private static Map<String, SystemProperty> systemProperty;
private static Map<String, AdvChannelConfig> configCahche = new HashMap<String,AdvChannelConfig>();
/**
* 清除system配置緩存
* @return
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
* @throws NoSuchMethodException
* @throws SecurityException
*/
@RequestMapping("/deleteSysteMapCache")
@ResponseBody
public String deleteSysteMapCache() throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException, SecurityException {
systemProperty = dao.getSystemProperty();
// 获得对象的类型
Class classType = SystemPropertyService.class;
// 通过默认构造方法创建一个新的对象
Object objectCopy = null;
try {
objectCopy = classType.getConstructor(new Class[] {}).newInstance(new Object[] {});
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 获得对象的所有属性
Field fields[] = classType.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
String fieldName = field.getName();
if (fieldName.equals("dao")) {
continue;
}
String firstLetter = fieldName.substring(0, 1).toUpperCase();
// 获得和属性对应的setXXX()方法的名字
String setMethodName = "set" + firstLetter + fieldName.substring(1);
// 获得和属性对应的setXXX()方法
Method setMethod = classType.getMethod(setMethodName, new Class[] { field.getType() });
// 调用原对象的getXXX()方法
Object value = null;
// 调用拷贝对象的setXXX()方法
setMethod.invoke(objectCopy, new Object[] { systemProperty });
}
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("data", 0);
resultMap.put("result", Constants.RESULT_SUCCESS);
String returnResult = JSON.toJSONString(resultMap);
return returnResult;
}
}