利用反射更新类中map缓存数据

公司配置表中的数据,一改动就需要重新去启动服务器,因为这些数据是放在了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;
}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值