spring整合ehcache缓存不了数据

玫瑰情书 发布于 2014/05/13 13:39
阅读 663
收藏 0

spring整合ehcache缓存不了数据,求助!

这个是我的ehcache.xml配置文件  
Java代码   
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  3.     xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"    
  4.     monitoring="autodetect">    
  5.     <diskStore path="d:\\temp\\cache"/>    
  6.     <defaultCache    
  7.             maxElementsInMemory="10000"    
  8.             eternal="false"    
  9.             timeToIdleSeconds="120"    
  10.             timeToLiveSeconds="120"    
  11.             overflowToDisk="true"    
  12.             maxElementsOnDisk="10000000"    
  13.             diskPersistent="false"    
  14.             diskExpiryThreadIntervalSeconds="120"    
  15.             memoryStoreEvictionPolicy="LRU"    
  16.             />    
  17.      <cache name="demoCache"    
  18.            maxElementsInMemory="10000"    
  19.            maxElementsOnDisk="1000"    
  20.            eternal="false"    
  21.            overflowToDisk="true"    
  22.            diskSpoolBufferSizeMB="20"    
  23.            timeToIdleSeconds="300"    
  24.            timeToLiveSeconds="600"    
  25.            memoryStoreEvictionPolicy="LFU"    
  26.             />    
  27. </ehcache> 
     
 
 
spring配置文件  
Java代码   
  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"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="  
  7.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  9.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"  
  10.             default-lazy-init="true">  
  11.     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
  12.         <property name="configLocation">  
  13.             <value>classpath:ehcache.xml</value>  
  14.         </property>  
  15.     </bean>  
  16.     <bean id="deptCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">  
  17.         <property name="cacheManager" ref="cacheManager"/>  
  18.         <property name="beanName" value="democache"/>  
  19.     </bean>  
  20.     <bean id="cacheInterceptor" class="com.xiaolu.interceptor.MethodCacheInterceptor">  
  21.         <property name="cache" ref="deptCache"/>  
  22.     </bean>  
  23.     <bean id="methodCachePointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
  24.         <property name="advice" ref="cacheInterceptor"/>  
  25.         <property name="patterns">  
  26.             <list>  
  27.                 <value>.*getAllDept</value>  
  28.             </list>  
  29.         </property>  
  30.     </bean>  
  31.     <bean id="deptService" class="com.xiaolu.service.DeptService"/>  
  32.     <bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">  
  33.         <property name="target" ref="deptService"/>  
  34.         <property name="interceptorNames">  
  35.             <list>  
  36.                 <value>methodCachePointcutAdvisor</value>  
  37.             </list>  
  38.         </property>  
  39.     </bean>  
  40. </beans>  
 
 
监 听 器  
Java代码   
  1. package com.xiaolu.interceptor;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. import net.sf.ehcache.Cache;  
  6. import net.sf.ehcache.Element;  
  7.   
  8. import org.aopalliance.intercept.MethodInterceptor;  
  9. import org.aopalliance.intercept.MethodInvocation;  
  10. import org.springframework.beans.factory.InitializingBean;  
  11. import org.springframework.util.Assert;  
  12.   
  13. public class MethodCacheInterceptor implements MethodInterceptor,InitializingBean {  
  14.     private Cache cache;  
  15.     @Override  
  16.     public Object invoke(MethodInvocation invocation) throws Throwable {  
  17.         String targetName = invocation.getThis().getClass().getName();  
  18.         String methodName = invocation.getMethod().getName();  
  19.         Object[] arguments = invocation.getArguments();  
  20.         Object result = null;  
  21.         String cacheKey = getCacheKey(targetName, methodName, arguments);  
  22.         System.out.println("cacheKey:"+cacheKey);  
  23.         Element element = null;  
  24.         System.out.println("-----1-----");  
  25.         synchronized(this){  
  26.             element = cache.get(cacheKey);  
  27.             if(null == element){  
  28.                 result = invocation.proceed();  
  29.                 System.out.println("-----2----");  
  30.                 element = new Element(cacheKey, (Serializable)result);  
  31.                 cache.put(element);  
  32.             }  
  33.         }  
  34.         return element.getValue();  
  35.     }  
  36.   
  37.     @Override  
  38.     public void afterPropertiesSet() throws Exception {  
  39.         Assert.notNull(cache,  
  40.                 "A cache is required. Use setCache(Cache) to provide one.");  
  41.     }  
  42.     private String getCacheKey(String targetName,String methodName,Object[] arguments){  
  43.         StringBuilder sb = new StringBuilder();  
  44.         sb.append(targetName).append(".").append(methodName);  
  45.         if((arguments.length !=0)&&(arguments != null)){  
  46.             for(int i=0;i<arguments.length;i++){  
  47.                 sb.append(".").append(arguments[i]);  
  48.             }  
  49.         }  
  50.         return sb.toString();  
  51.     }  
  52.   
  53.     public void setCache(Cache cache) {  
  54.         this.cache = cache;  
  55.     }  
  56.   
  57. }  
 
 
访问数据库的类  
Java代码   

  1. package com.xiaolu.service;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9.   
  10. import com.xiaolu.pojo.Dept;  
  11.   
  12. public class DeptService {  
  13.     Connection conn = null;  
  14.     PreparedStatement pstmt = null;  
  15.     ResultSet rs = null;  
  16.     public List<Dept> getAllDept(){  
  17.         String sql = "select * from dept";  
  18.         List<Dept> list = new ArrayList<Dept>();  
  19.         try {  
  20.             Class.forName("oracle.jdbc.driver.OracleDriver");  
  21.             conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1 :1521:orcl","scott","tiger");  
  22.             pstmt = conn.prepareStatement(sql);  
  23.             rs = pstmt.executeQuery();  
  24.             while(rs.next()){  
  25.                 Dept dept = new Dept();  
  26.                 dept.setDeptno(rs.getInt("deptno"));  
  27.                 dept.setDname(rs.getString("dname"));  
  28.                 dept.setLoc(rs.getString("loc"));  
  29.                 list.add(dept);  
  30.             }  
  31.         } catch (Exception e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.         return list;  
  35.     }  

  36.  
 
 
实体类  
Java代码   

  1. package com.xiaolu.pojo;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Dept implements Serializable{  
  6.     /**
  7.      *  
  8.      */  
  9.     private static final long serialVersionUID = 1L;  
  10.     private int deptno;  
  11.     private String dname;  
  12.     private String loc;  
  13.     public int getDeptno() {  
  14.         return deptno;  
  15.     }  
  16.     public void setDeptno(int deptno) {  
  17.         this.deptno = deptno;  
  18.     }  
  19.     public String getDname() {  
  20.         return dname;  
  21.     }  
  22.     public void setDname(String dname) {  
  23.         this.dname = dname;  
  24.     }  
  25.     public String getLoc() {  
  26.         return loc;  
  27.     }  
  28.     public void setLoc(String loc) {  
  29.         this.loc = loc;  
  30.     }  
  31.       

  32.  
 
 
测试代码  

Java代码 


  1. package test;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.springframework.context.ApplicationContext;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7.   
  8. import com.xiaolu.pojo.Dept;  
  9. import com.xiaolu.service.DeptService;  
  10.   
  11. import net.sf.ehcache.Cache;  
  12. import net.sf.ehcache.CacheManager;  
  13. import net.sf.ehcache.Element;  
  14.   
  15. public class Test {  
  16.     public static void main(String[] args) {  
  17.           
  18.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
  19.         DeptService service = (DeptService) context.getBean("proxyFactoryBean");  
  20.         List<Dept> list = service.getAllDept();  
  21.         for(Dept dept:list){  
  22.             System.out.println(dept.getDname());  
  23.         }  
  24.     }  
  25. }
     

原帖求助:http://bbs.ibei#feng.com/read-htm-tid-65400.html

复制网址时需把ibei#feng中的#去掉

加载中
0
hlevel
hlevel
该贴的不贴。。applicationContext.xml  需要贴出来,能不能缓存在于 你有没有配置。
返回顶部
顶部