Announcement Announcement Module
Collapse
No announcement yet.
@EnableCaching Not Working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @EnableCaching Not Working

    I'm having trouble getting @EnableCaching to work in my java-based configuration. Here's my setup:

    Code:
    @Configuration
    @EnableCaching
    public class LdapConfig {
      @Bean
      public ILdapDao ldapDao() {
        return new LdapDaoImpl();
      }
    
      @Bean
      public CacheManager cacheManager() {
        //this cacheMgr has a Cache impl capable of handling "people"
        return new LdapCacheManager(ldapDao());
      }
    }
    
    public class LdapDaoImpl implements ILdapDao {
      @Cacheable(value="people", key="#name")
      public getByName(String name) {
         ...
      }
    }
    When I run my integration tests on methods that are cacheable, the caching logic is not being applied. I should mention that this exact setup is currently working. I was previously using an XML based definition to enable the caching but I wanted to get completely away from using XML (using @EnableCaching). Here's the only changes that are currently working using XML to enable the caching:

    Slight change to java configuration to import the caching definition...
    Code:
    @Configuration
    @ImportResource("classpath:META-INF/spring/ldap-cache.xml")
    public class AppConfig {
      //everything else in here is the same
      ...
    }
    And the actual XML to enable caching (META-INF/spring/ldap-cache.xml):
    Code:
    <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/schem...ring-beans.xsd
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
        <cache:annotation-driven />
    </beans>
    I'm sure this has something to do with proxying that's used internally in Spring, but why does this work when I enable caching using XML approache, and not using the Java-based approach?

  • #2
    Actually, one thing I just realized is I don't need to pass the LdapDao into LdapCacheManager. I am using Google Guava's cache abstraction, and I just realized I don't need this to load the cache automatically when there is a miss since Spring's cache abstraction will do this for me. This may solve the issue. I'll retry something in the next day or two and report back.

    Comment

    Working...
    X