im trying to implement Spring 3.1 cache with EHcache.
When i run the code, it always run the method implementation and never use the cache:
There is my code:
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 is my main class:
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");
}
Console output:
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 method invokes and no use of cache, anyone knows whats wrong?
you are instantiating dao using new, not using spring so it won’t get spring annotations applied as it not being managed by spring.