我试图用EHcache实现Spring 3.1缓存。 当我运行代码时,它总是运行方法实现,从不使用缓存: 有我的代码:
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.springframework.context.support.ClassPathXmlApplicationContext@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.springframework.beans.factory.support.DefaultListableBeanFactory@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方法调用并且没有使用缓存,任何人都知道什么是错的?
答案 0 :(得分:1)
你正在使用new来实例化dao,而不是使用spring,因此它不会被弹簧注释应用,因为它不是由spring管理的。