利用Spring BeanPostProcessor 初始化时加载数据到缓存

我们经常会在项目中碰到需要加载一些常量到内存中,以便不比每次需要使用时都去查询数据库, 如,一些国家信息,省份信息,地址信息等 不会变动 或变动很少,但数据量又比较大的数据, 这时我们就需要把这些数据加载进内存,需要使用时直接从内从中取。 下面是代码:

 

 

Dao 以下  我就不发了。

首先定义一个类,在这个类中进行调用DAO查询数据库 ,加载出数据

Java代码   收藏代码
  1. public class StartOnLoadService {  
  2.     public static Map<String,List> dictionaryInfoMap = new HashMap<String, List>(); //保存国籍信息   
  3.     private CountryService countryService; //受Spring 管理的Service 方法 调用Dao取数据  
  4.   
  5.     /** 
  6.      * Spring 容器初始化时加载 
  7.      */  
  8.     public void loadData() {  
  9.         List optionList = countryService.getAllCountry();  
  10.         dictionaryInfoMap.put("nationalMap", optionList);  
  11.     }  
  12.       
  13.     public CountryService getCountryService() {  
  14.         return countryService;  
  15.     }  
  16.   
  17.     public void setCountryService(CountryService countryService) {  
  18.         this.countryService = countryService;  
  19.     }  
  20.       
  21. }  

 

 

然后定义一个类 实现接口 BeanPostProcessor

Java代码   收藏代码
  1. public class PBSTrackManagerPostProcessor implements BeanPostProcessor {  
  2.   
  3.     public Object postProcessAfterInitialization(Object obj, String arg1)  
  4.             throws BeansException {  
  5.         if(obj instanceof StartOnLoadService) {  
  6.             ((StartOnLoadService)obj).loadData(); //调用方法加载数据  
  7.         }  
  8.         return obj;  
  9.     }  
  10.   
  11.     public Object postProcessBeforeInitialization(Object arg0, String arg1)  
  12.             throws BeansException {  
  13.         return arg0;  
  14.     }  
  15.   
  16. }  

 

Spring 配置

Java代码   收藏代码
  1. <!-- 自动加载国家信息到 内存 -->  
  2. <bean id="pbsTrackManagerPostProcess" class="com.mangocity.vacationhotel.business.action.PBSTrackManagerPostProcessor"/>  

  3. <!--如果用注解的方式,在需要加载的类上加上@Service即可,这样spring才会读取注解配置加载文件2014-11-14  by zhang--> 
  4. <bean id="startOnLoadService" class="com.mangocity.vacationhotel.business.service.impl.StartOnLoadService">  
  5.     <property name="countryService" ref="countryService"/> 

  6. </bean>  

 

就这样在Spring 启动时  数据就会被加载到 上面 StartOnLoadService 中的 dictionaryInfoMap 中。

 

 

/分割线/

下面把国籍信息显示在页面上:

Java代码   收藏代码
  1. public class OptionManager {  
  2.       
  3.     /** 
  4.      * 功能:生成国籍select表单下的option选项的字符串 
  5.      * @param value 设置为选中状态的option的值 
  6.      */  
  7.     public static String generateOptionString(long value) {  
  8.         List optionList = StartOnLoadService.dictionaryInfoMap.get("nationalMap");  
  9.           
  10.         StringBuffer optionString = new StringBuffer();  
  11.         Country country = null;  
  12.         if(null != optionList) {  
  13.             for(int i = 0; i<optionList.size()&&optionList.get(i)!=null; i++) {  
  14.                 country = (Country)optionList.get(i);  
  15.                 if( value== country.getId()) {  
  16.                     optionString.append("<option value='" + country.getId() + "' selected>" + country.getName() + "</option>");  
  17.                 } else {  
  18.                     optionString.append("<option value='" + country.getId() + "' >" + country.getName() + "</option>");  
  19.                 }  
  20.             }  
  21.         }  
  22.         return optionString.toString();  
  23.     }  
  24. }  

 

JSP页面使用:

Java代码   收藏代码
  1.  <select name="fellow.nation" id="nation" style="width:110px;" >  
  2.     <%=OptionManager.generateOptionString(70007)%> <!-- 默认选中 为 "Z-中国"-->  
  3. </select>  

 转载地址:http://ollevere.iteye.com/blog/1356352

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值