Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8846113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:53:55+00:00 2026-06-14T11:53:55+00:00

im trying to implement Spring 3.1 cache with EHcache. When i run the code,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:53:56+00:00Added an answer on June 14, 2026 at 11:53 am

    you are instantiating dao using new, not using spring so it won’t get spring annotations applied as it not being managed by spring.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a generic thread-safe Cache method, and I wonder how
I'm trying to implement a MongoDB / Memory combined Output Cache Provider to use
I'm trying to implement scheduler to my application. I use spring and quartz support.
I was trying to implement Spring AOP in web app. Unfortunately all the sample
I'm trying to implement my first Spring MVC controller, following Spring documentation, some tutorials
I am trying to implement a Session-Per-Conversation pattern in a JSF2-Spring-Hibernate Web App so
Iam trying to implement a simple JMS(traditional not using springs) code in eclipse using
Trying to implement LoaderManager + CursorLoader. In onFinish method adapter should swap its cursor
I am trying to implement long polling in my Spring-MVC Web App but it
I've been trying to implement cache regions with fluent nhibernate and I've done the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.