有这个想法是在很早以前了,那时的我没有接触什么缓存技术,只知道hibernate有个二级缓存。没有用过memcache,也没有使用过redis。

只懂得将数据放到数组里或者集合里,一直不去销毁它(只有随着tomcat服务停止而销毁),用的时候从内存中读取就相当于缓存了,但是这么做有利也有弊。

好处:操作方便,随时取,随时存,只要方法封装好,代码也很清晰,易扩展。

弊端:因为只要一重启服务器,放在内存中的静态集合或静态数组肯定被回收了。导致一些重要的数据被干掉了。

话题扯远了,本文所讲的就是如何在spring 加载完毕后监听它的事件,并且保证只初始化一次。

配置其实很简单,如下:

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <!-- 初始化引擎使用 -->
<bean id="InitDataListener" class="cfs.wsdl.cache.InitDataListener">
<property name="dao">
<ref bean="commonBaseDaoHib" />
</property>
</bean> </beans>

spring配置文件

 package cfs.wsdl.cache;

 import java.util.Date;
import java.util.List;
import java.util.UUID; import org.springframework.beans.factory.InitializingBean; import com.google.gson.JsonObject;
import cfs.core.dao.CommonBaseDao; public class InitDataListener implements InitializingBean { private CommonBaseDao dao; public CommonBaseDao getDao() {
return dao;
} public void setDao(CommonBaseDao dao) {
this.dao = dao;
} @Override
public void afterPropertiesSet() throws Exception {
//将需要缓存的数据从数据库里缓存到内存中 //启动一个线程线程:
new Thread() {
public void run() {
while (true) {
try { //这里写一些需要定时执行的代码 //休眠30分钟,半个小时执行一次
Thread.sleep(60 * 1000*30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}.start(); } }

监听的事件类

最新文章

  1. YYModel 源码解读(二)之NSObject+YYModel.h (1)
  2. shell:遍历目录和子目录的所有文件
  3. Linq查询操作之聚合操作(count,max,min,sum,average,aggregate,longcount)
  4. View Focus的处理过程及ViewGroup的mFocused字段分析
  5. div居中
  6. activiti 介绍
  7. SQL-PIVOT 数据透视 行列转换
  8. Intellij IDEA的Hibernate简单应用
  9. java.lang.NoClassDefFoundError的原因及解决
  10. Mac OS 安装 Port
  11. hibernate 一张数据表的流程
  12. Java nextInt()函数
  13. 辛星解读mysql的用户管理
  14. SQL学习之高级数据过滤
  15. IOS启动其他应用程序
  16. ES6 正则的扩展
  17. 测者的测试技术手册:Java中的null类型是测试不可超越的鸿沟
  18. Memcache,redis,rabbitMQ,SQLAlchemy
  19. main 团队项目厨娘:用例图、类图、时序图
  20. vue2.3时使用手机调试,提示媒体已断开的解决方案

热门文章

  1. [python]python元类
  2. C++中的静态绑定和动态绑定
  3. mysql update时报错You are using safe update mode
  4. scrum1.4---Sprint 计划
  5. Array,List,Struct可能被大家忽略的问题
  6. 2015百度之星 IP聚合
  7. lavarel框架中如何使用ajax提交表单
  8. java之StringBuilder类详解
  9. ahjesus 创建msdn一样的帮助文档
  10. UltraEdit打开UTF-8文件后显示中文乱码的问题