Functional Example of Declarative Caching with Spring

Source Code for Funcational Example

I recently had the dubious pleasure of working with Spring-Modules’ Declarative Caching with EHCache. The package I’m developing involves database integration through iBATIS, Spring Web Services, and caching to increase performance. I chose to use Spring Modules’ Declarative Caching because it obviously integrates well with the rest of the Spring architecture, and allows for the use of more than one caching engine, of which, I chose EHCache.

Let me just say, as simple as Spring has made things to work with in the past, the Spring-Modules package has been less than simple to work with. The Spring core, and many of it’s sub-projects, are clearly documented and inclusive of examples, however, the Declarative Caching module seems to lack both. It took many hours of trolling through the Internet and posting in forums to finally get something to work. Ultimately, I’m pleased with the outcome, but not content with the effort it took to get here. There are a few “gotchas” to take note of, which I’ll explain below.

After getting my own project to work, I decided to build a functional example of Declarative Caching to hopefully save someone else time in the future. What is attached to this post is the source and configurations for a fully functional servlet utilizing Spring-Modules’ Declarative Caching. All that is provided is the java source and configuration files. I originally built this in Netbeans 6 on Tomcat 6 with Java 1.5, however, with tweaks, it should work everywhere.

The “gotchas” I alluded to earlier is three-part:

  1. Spring-Modules v0.8 is the latest release, but v0.9 was slated for early 2007, which did not occur.
  2. Spring-Modules’ Caching provides functions for caching a single object, and flushing the entire cache, but it does not provide a means of removing a single object from the cache even though EHCache does. There are some ways around this (get access to the CacheProvider in your code and call it yourself, or create a custom interceptor). It is rumored that the fix would be out in v0.9, which as noted in #1, did not occur.
  3. Namespaces – There’s presently a namespace issue with the XSDs for the caching bean definitions. This is due to the fact that www.springmodules.org is not the URL for the Spring-Modules project, and therefore, the XSD’s cannot be found there. To work around it, I placed the XSD’s in the “classes” folder of the web-app and referenced them as such in the AppContext file. You’ll see it when you look at the namespaces in “cache-AppContext.xml”

Also, the attached package does not include all of the necessary libraries. You can find out what libraries and versions I used by looking at the documentation in the source of “ExampleController.java”. The source and config files are clearly documented. So, if you’re planning on using Declarative Caching, make sure you have a firm understanding of how Spring works before you delving into this sub-project. If you have a firm understanding already, first look at “cache-AppContext.xml” to understand how the beans are defined, then “CachedClass.java” to understand how the bean definitions reference the cached methods, and finally, the “ExampleController.java” class to understand how you call the cached methods.

And if you’re like me, and you read the documentation at the Spring-Modules site regarding Declarative Caching, you’ll notice that the documentation does not mesh with the example configuration. The example configuration is MUCH less verbose than what the documentation shows, so don’t panic, it still works.

Enjoy!

Source Code for Funcational Example

2 Responses to “Functional Example of Declarative Caching with Spring”

  1. edalquist says:

    I am one of the authors of a new project intended to provide Ehcache integration for Spring 3 projects via annotations:

    http://code.google.com/p/ehcache-spring-annotations/

    The library provides two method-level annotations in the spirit of Spring’s @Transactional:

    @Cacheable
    @TriggersRemove

    When appropriately configured in your Spring application, this project will create caching aspects at runtime around your @Cacheable annotated methods.

    Usage documentation can be found on the project wiki:

    http://code.google.com/p/ehcache-spring-annotations/wiki/UsingCacheAnnotations
    http://code.google.com/p/ehcache-spring-annotations/wiki/UsingTriggersRemove

  2. Halcyon says:

    It’s good to see this finally getting some attention. Since the original pass with spring-modules lacked a “remove from cache” option, it was virtually useless. The documentation looks very promising. Thanks for taking time to fix this up!

Leave a Reply

You must be logged in to post a comment.