监听spring加载完成后事件

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

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

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

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

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

配置其实很简单,如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jaxws="http://cxf.apache.org/jaxws"
 7     xmlns:soap="http://cxf.apache.org/bindings/soap" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
 8     xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
 9          http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
10          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
11          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 
12          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
13          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
14          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
15          http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
16       
17     <!-- 初始化引擎使用 -->
18     <bean id="InitDataListener" class="cfs.wsdl.cache.InitDataListener">  
19             <property name="dao">
20             <ref bean="commonBaseDaoHib" />
21         </property>
22     </bean>  
23     
24     
25     
26 </beans>
spring配置文件
 1 package cfs.wsdl.cache;
 2 
 3 import java.util.Date;
 4 import java.util.List;
 5 import java.util.UUID;
 6 
 7 import org.springframework.beans.factory.InitializingBean;
 8 
 9 import com.google.gson.JsonObject;
10 import cfs.core.dao.CommonBaseDao;
11 
12 
13 
14 public class InitDataListener implements InitializingBean {  
15       
16     private CommonBaseDao dao;
17 
18     public CommonBaseDao getDao() {
19         return dao;
20     }
21 
22     public void setDao(CommonBaseDao dao) {
23         this.dao = dao;
24     }
25   
26     @Override  
27     public void afterPropertiesSet() throws Exception {  
28                 //将需要缓存的数据从数据库里缓存到内存中
29     
30                     
31             //启动一个线程线程:
32                     new Thread() {  
33                         public void run() {  
34                             while (true) {  
35                                 try  {  
36                              
37                                     
38                                //这里写一些需要定时执行的代码
39                                      
40                                      
41                                      //休眠30分钟,半个小时执行一次  
42                                     Thread.sleep(60 * 1000*30);  
43                                 } catch (InterruptedException e) {  
44                                     e.printStackTrace();  
45                                 }  
46                             }  
47                         };  
48                     }.start();  
49             
50                     
51     }  
52           
53   
54     
55     
56     
57 }          
监听的事件类

 

posted @ 2016-09-18 09:47  刺风  阅读(2106)  评论(0编辑  收藏  举报