Thanks Imilina,
I tried as you said.
I configured like this:
Code:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">
<!-- Cache manager -->
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>
<!-- Cache provider -->
<bean id="cacheProvider"
class="org.springmodules.cache.provider.ehcache.EhCacheFacade">
<property name="cacheManager" ref="cacheManager" />
</bean>
<!-- Cache regions for annotated cache -->
<ehcache:annotations>
<ehcache:caching id="petsCacheModel" cacheName="CACHE_NAME" />
<ehcache:flushing id="petsFlushModel" cacheNames="CACHE_NAME" when="after" />
</ehcache:annotations>
<bean id="test" class="com.cit.ecommerce.test.TestCache"/>//This is my
new entry
</beans>
Java Code:
Code:
public class TestCache {
@Cacheable(modelId = "petsCacheModel")
public Person getMethod(){
return getObject("ABCD",34);
}
@Cacheable(modelId = "petsCacheModel")
private Person getObject(String name, int age){
Person p = new Person();
p.name = "Sachin";
p.age = 25;
System.out.println("Called----private method (**)");
return p;
}
@CacheFlush(modelId = "petsFlushModel")
public void updateObject(String name,int age){
Person p = new Person();
p.name = name;
p.age = age;
}
}
TEST CLIENT CODE:
Code:
public class Test {
public static void main(String[] args) {
// ApplicationContext ctx = new ClassPathXmlApplicationContext("beans4cache.xml");
ClassPathResource resource = new ClassPathResource("CachingInAction.xml");
BeanFactory factory = new XmlBeanFactory(resource);
TestCache testCache=null;
Person p=null;
testCache = (TestCache)factory.getBean("test");
p = testCache.getMethod();
System.out.println("Called---- (1)");
p = testCache.getMethod();
System.out.println("Called---- (2)");
testCache.updateObject("ABCD",22);
p = testCache.getMethod();
System.out.println("Called---- (3)");
}
}
OUTPUT:
Called----private method (**)
Called---- (1)
Called----private method (**)
Called---- (2)
Called----private method (**)
Called---- (3)
It means caching is not working. i'm getting no errors but caching is not happening.
Waiting for your reply
Ganesh