SpringMvc 系统启动时加载数据到内存中

转:http://blog.csdn.net/newstruts/article/details/18668269

 在项目启动时加载数据到内存中(我这里是数据字典),以后再代码中就不用每次从数据库去查询了

  Dictionary 数据字典实体


SysInitBean  要初始化的Bean,要实现  ServletContextAware 接口

package com.wonders.framework.base;

import java.util.Properties;

import javax.servlet.ServletContext;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import com.wonders.framework.dictionary.service.DictionaryService;
import com.wonders.framework.util.FileUtil;

//实现ServletContextAware,可以获得servletcontext
//@Component注解了,直接在xml里配置这个bean就行了,系统自动调用
@Component
public class SysInitBean implements  ServletContextAware {
	@Autowired
	private DictionaryService dictionaryService;
	
	@Override
	public void setServletContext(ServletContext sc) {
		// 把项目名称放到application中
		String ctxPath=sc.getContextPath();
		sc.setAttribute("ctxPath",ctxPath);
		
		//初始化数据字典到application中
		dictionaryService.init(sc);
	}
}

DictionaryServiceImpl 

     用来查询数据字典

package com.wonders.framework.dictionary.service.impl;

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

import javax.servlet.ServletContext;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wonders.framework.dictionary.dao.DictionaryDao;
import com.wonders.framework.dictionary.entity.po.Dictionary;
import com.wonders.framework.dictionary.service.DictionaryService;
import com.wonders.framework.util.FileUtil;

@Component("dictionaryServiceImpl")
public class DictionaryServiceImpl implements DictionaryService {
	private static Logger logger = Logger.getLogger(DictionaryServiceImpl.class
			.getName());

	// private static Map<String, Map<String, String>> localDics = new
	// HashMap<String, Map<String, String>>();
	@Autowired
	private DictionaryDao dictionaryDao;
	private ServletContext sc;

	// @PostConstruct
	public void init(ServletContext sc) {
		this.sc = sc;
		List<Dictionary> list = null;
		try {
			list = dictionaryDao.findAll();
		} catch (Exception e) {
			logger.error("数据字典数据查找失败!");
		}
		if (null == list) {
			return;
		}
		Map<String, Map<String, String>> localDics = new HashMap<String, Map<String, String>>();
		Map<String, String> map = null;
		for (Dictionary dic : list) {
			String type = dic.getType();
			if (localDics.containsKey(type)) {
				map = localDics.get(type);
				map.put(dic.getKey(), dic.getValue());
			} else {
				map = new HashMap<String, String>();
				map.put(dic.getKey(), dic.getValue());
				localDics.put(type, map);
			}
		}
		Properties prop = FileUtil
				.getProperties("/config/dictionary.properties");
		for (Object key : prop.keySet()) {
			Object value = prop.get(key);
			sc.setAttribute((String) key,
					JSONObject.fromObject(localDics.get(value)));
		}
	}

	public String getValue(String type, String key) {
		JSONObject jsonObject = (JSONObject) sc.getAttribute(type);
		return (String) jsonObject.get(key);
	}

	public boolean isDic(String type) {
		JSONObject jsonObject = (JSONObject) sc.getAttribute(type);
		return jsonObject == null ? false : true;
	}

	public String getKey(String type, String value) {
		JSONObject jsonObject = (JSONObject) sc.getAttribute(type);
		for (Object key : jsonObject.keySet()) {
			String sValue = (String) jsonObject.get(key);
			if (sValue.equals(value)) {
				return (String) key;
			}
		}
		return null;
	}

	// @PreDestroy
	// public void destroy() {
	// localDics = null;
	// }

	@SuppressWarnings("unchecked")
	public Map<String, String> findDictionary(String type) {
		JSONObject jsonObject = (JSONObject) sc.getAttribute(type);
		return jsonObject;
	}
}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值