2012-11-09 2 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

스프링을 사용하지 않고 new를 사용하여 DAO를 인스턴스화하므로 봄에 의해 관리되지 않으므로 스프링 주석을 적용하지 않습니다.

관련 문제