2012-11-09 102 views
2

即时通讯试图实现与EHcache的Spring 3.1缓存。 当我运行的代码,它总是运行的方法执行,从来没有使用缓存: 有我的代码:Spring 3.1和EHCache

的beans.xml:

<?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:cache="http://www.springframework.org/schema/cache" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd" 
    xmlns:p="http://www.springframework.org/schema/p" 
    > 
    <cache:annotation-driven cache-manager="cacheManager"/> 

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> 
     <property name="cacheManager"><ref local="ehcache"/></property> 
    </bean> 
    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" > 
     <property name="configLocation" value="classpath:ehcache.xml"/> 
    </bean> 
    <bean id="testDAO" class="com.sura.test.ejb.TestDAO" /> 

</beans> 

ehcache.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> 

    <defaultCache eternal="false" maxElementsInMemory="1000" 
     overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" 
     timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU"/> 

    <cache name="testCache" eternal="false" 
     maxElementsInMemory="100" overflowToDisk="false" diskPersistent="false" 
     timeToIdleSeconds="10" timeToLiveSeconds="60" 
     memoryStoreEvictionPolicy="LRU" 
     /> 

    </ehcache> 

DAO:

import org.springframework.cache.annotation.Cacheable; 


    public class TestDAO implements ITestDAO{ 
    @Cacheable(value = "testCache" , key = "#dpto") 
    public List<String> getCiudad(String dpto){ 
     System.out.println("cargando id:" + dpto); 
     List<String> retorno = new ArrayList<String>(); 
     retorno .add("1- "+dpto); 
     return retorno; 
    } 
    } 

Ther是我的主要课程:

public static void main(String[] args) { 
     Testing t = new Testing(); 
     t.testCache(); 

    } 

    public void testCache(){ 
     ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:beans.xml"); 
     TestDAO tdao = new TestDAO();  
     tdao.getCiudad("1"); 
     tdao.getCiudad("1"); 
     tdao.getCiudad("1"); 
     tdao.getCiudad("1"); 
    } 

控制台输出:

9/11/2012 03:13:58 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh 
INFO: Refreshing org[email protected]206be24: startup date [Fri Nov 09 15:13:58 COT 2012]; root of context hierarchy 
9/11/2012 03:13:59 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from class path resource [beans.xml] 
9/11/2012 03:13:59 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 
INFO: Pre-instantiating singletons in org.s[email protected]213db0d: defining beans [org.springframework.aop.config.internalAutoProxyCreator,org.springframework.cache.annotation.AnnotationCacheOperationSource#0,org.springframework.cache.interceptor.CacheInterceptor#0,org.springframework.cache.config.internalCacheAdvisor,cacheManager,ehcache,testDAO]; root of factory hierarchy 
9/11/2012 03:13:59 PM org.springframework.cache.ehcache.EhCacheManagerFactoryBean afterPropertiesSet 
INFO: Initializing EHCache CacheManager 
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". 
SLF4J: Defaulting to no-operation (NOP) logger implementation 
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. 

cargando id:1 
cargando id:1 
cargando id:1 
cargando id:1 

4方法调用并没有使用高速缓存的,任何人都知道什么是错?

回答

1

你正在使用新的实例化dao,而不是使用spring,所以它不会得到Spring注解,因为它不会被spring管理。