有这个想法是在很早以前了,那时的我没有接触什么缓存技术,只知道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 > 19 <property name="dao"> 20 <ref bean="commonBaseDaoHib" /> 21 </property> 22 </bean> 23 24 25 26 </beans>