SpringMvc 系统启动时加载数据到内存中
来源:互联网 发布:网络新技术课程介绍 编辑:程序博客网 时间:2024/05/25 08:14
在项目启动时加载数据到内存中(我这里是数据字典),以后再代码中就不用每次从数据库去查询了
Dictionary 数据字典实体
package com.wonders.framework.dictionary.entity.po;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;import org.hibernate.annotations.GenericGenerator;@Entity@Table(name = "WC_DICTIONARY",schema="WST")public class Dictionary {private String id;
//key值private String key;
//value值private String value;
//数据字典类型private String type;@Id@GenericGenerator(name="idGenerator", strategy="uuid") //这个是hibernate的注解@GeneratedValue(generator="idGenerator")public String getId() {return id;}public void setId(String id) {this.id = id;}@Column(name = "KEY", nullable = true, length = 50)public String getKey() {return key;}public void setKey(String key) {this.key = key;}@Column(name = "VALUE", nullable = true, length = 50)public String getValue() {return value;}public void setValue(String value) {this.value = value;}@Column(name = "TYPE", nullable = true)public String getType() {return type;}public void setType(String type) {this.type = type;}}
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就行了,系统自动调用@Componentpublic class SysInitBean implements ServletContextAware {@Autowiredprivate DictionaryService dictionaryService;@Overridepublic 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>>();@Autowiredprivate DictionaryDao dictionaryDao;private ServletContext sc;// @PostConstructpublic 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;}}
在js中就可以用el表达式直接获取如: '${dicCategory}'
在java代码中可以这样获取( String getValue(String type,String key);)
type代表数据字典类型(如 区县)
key (01)
value (黄浦区)
dis = dictionaryService.getValue("1", ‘01’);
返回 汉字 黄浦区
0 0
- SpringMvc 系统启动时加载数据到内存中
- SpringMvc 系统启动时加载数据到内存中
- Java+SpringMVC中自实现定时加载更新数据库数据到内存的方法
- 从文件中加载数据到内存
- web服务刚启动时SpringMVC注解就加载一定的数据库数据进入内存中
- SSH开发框架中,实现系统启动加载类,读取数据库常用数据进入内存以及将数据放在application
- SSH开发框架中,实现系统启动加载类,读取数据库常用数据进入内存以及将数据放在application
- 14web启动将数据加载到内存中
- 系统启动PropertyPlaceholderConfigurer加载配置到spring容器中
- 系统启动时,数据动态加载业务若干思考
- 【原创】SSH开发框架中,实现系统启动加载类,读取数据库常用数据进入内存,利用Spring托管,并完成reload功能
- SSH开发框架中,实现系统启动加载类,读取数据库常用数据进入内存,利用Spring托管,并完成reload功能
- SSH开发框架中,实现系统启动加载类,读取数据库常用数据进入内存,利用Spring托管,并完成reload功能
- Linux中实现在系统启动时自动加载模块
- 简单的Servlet,tomcat启动将数据加载到内存中去
- 第一步:加载内核到内存中
- android中加载大图片到内存
- 缩放图片并加载到内存中
- WINCE5.0和WINCE6.0的内存与系统架构
- OpenERP登录页面调整
- 房地产行业名词解释
- Aqua Date Studio乱码
- 系统托盘的应用
- SpringMvc 系统启动时加载数据到内存中
- OE模块中js方法初始化
- 判图的连通性(dfs,并查集)
- 位运算-互换2个整数值
- String和char
- 唱吧 的直播K歌新歌要与YY正面交锋了
- 野指针
- Base64编码在开发中应用
- QT中找不到qrc中注册的图片