Spring 3.1 Caching and Config

I’ve recently being blogging about Spring 3.1 and its new caching annotations @Cacheable and @CacheEvict. As with all Spring features you need to do a certain amount of setup and, as usual, this is done with Spring’s XML configuration file. In the case of caching, turning on @Cacheable and @CacheEvict couldn’t be simpler as all you need to do is to add the following to your Spring config file:

<cache:annotation-driven />

…together with the appropriate schema definition in your beans XML element declaration:

<beans xmlns='http://www.springframework.org/schema/beans' 
  xmlns:p='http://www.springframework.org/schema/p'
  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
  xmlns:cache='http://www.springframework.org/schema/cache' 
  xmlns:context='http://www.springframework.org/schema/context'
   xsi:schemaLocation='http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.1.xsd


http://www.springframework.org/schema/cache

        http://www.springframework.org/schema/cache/spring-cache.xsd'>

…with the salient lines being:

xmlns:cache='http://www.springframework.org/schema/cache'

…and:

http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd

However, that’s not the end of the story, as you also need to specify a caching manager and a caching implementation. The good news is that if you’re familiar with the set up of other Spring components, such as the database transaction manager, then there’s no surprises in how this is done.

A cache manager class seems to be any class that implements Spring’s org.springframework.cache.CacheManager interface. It’s responsible for managing one or more cache implementations where the cache implementation instance(s) are responsible for actually caching your data.

The XML sample below is taken from the example code used in my last two blogs.

<bean id='cacheManager' class='org.springframework.cache.support.SimpleCacheManager'>
  <property name='caches'>
 <set>
   <bean
             class='org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean'
             p:name='employee'/>
   <!-- 
   TODO Add other cache instances in here
    -->
 </set>
  </property>
</bean>

In the above configurtion, I’m using Spring’s SimpleCacheManager to manage an instance of their ConcurrentMapCacheFactoryBean with a cache implementation named: “ employee”.

One important point to note is that your cache manager MUST have a bean id of cacheManager. If you get this wrong then you’ll get the following exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.interceptor.CacheInterceptor#0': Cannot resolve reference to bean 'cacheManager' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'cacheManager' is defined
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.
resolveReference(BeanDefinitionValueResolver.java:328)
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.
resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.
applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.
populateBean(AbstractAutowireCapableBeanFactory.java:1118)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.
doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
:
:  trace details removed for clarity
:
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.
runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.
run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.
main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No bean named 'cacheManager' is defined
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.
getBeanDefinition(DefaultListableBeanFactory.java:553)
 at org.springframework.beans.factory.support.AbstractBeanFactory.
getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
 at org.springframework.beans.factory.support.AbstractBeanFactory.
doGetBean(AbstractBeanFactory.java:277)
 at org.springframework.beans.factory.support.AbstractBeanFactory.
getBean(AbstractBeanFactory.java:193)
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.
resolveReference(BeanDefinitionValueResolver.java:322)

As I said above, in my simple configuration, the whole affair is orchestrated by the SimpleCacheManager. This, according to the documentation, is normally “Useful for testing or simple caching declarations”. Although you could write your own CacheManager implementation, the Guys at Spring have provided other cache managers for different situations

  • SimpleCacheManager – see above.
  • NoOpCacheManager – used for testing, in that it doesn’t actually cache anything, although be careful here as testing your code without caching may trip you up when you turn caching on.
  • CompositeCacheManager – allows the use multiple cache managers in a single application.
  • EhCacheCacheManager – a cache manager that wraps an ehCache instance. See http://ehcache.org

Selecting which cache manager to use in any given environment seems like a really good use for Spring Profiles. See:?

And, that just about wraps things up, although just for completeness, below is the complete configuration file used in my previous two blogs:

<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns='http://www.springframework.org/schema/beans' 
  xmlns:p='http://www.springframework.org/schema/p'
  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
  xmlns:cache='http://www.springframework.org/schema/cache' 
  xmlns:context='http://www.springframework.org/schema/context'
   xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd'>
 
  <!-- Switch on the Caching -->
   <cache:annotation-driven />

 <!-- Do the component scan path -->
 <context:component-scan base-package='caching' />

 <!-- simple cache manager -->
 <bean id='cacheManager' class='org.springframework.cache.support.SimpleCacheManager'>
   <property name='caches'>
     <set>
       <bean class='org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean' p:name='employee'/>
       <!-- 
       TODO Add other cache instances in here
        -->
     </set>
   </property>
 </bean>


</beans>

As a Lieutenant Columbo is fond of saying “And just one more thing, you know what bothers me about this case…”; well there are several things that bother me about cache managers, for example:

  • What do the Guys at Spring mean by “Useful for testing or simple caching declarations” when talking about the SimpleCacheManager? Just exactly when should you use it in anger rather than for testing?
  • Would it ever be advisable to write your own CacheManager implementation or even a Cache implementation?
  • What exactly are the advantages of using the EhCacheCacheManager?
  • How often would you really need CompositeCacheManager?

All of which I may be looking into in the future…

Happy coding and don’t forget to share!

Reference: Spring 3.1 Caching and Config from our JCG partner Roger Hughes at the Captain Debug’s Blog blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


+ six = 15



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books