Tuesday, March 6, 2012

Spring Cache - Using SimpleCacheManager

We will create a simple application that uses Spring Cache Abstraction using JDK ConcurrentMap based cache. This will allow ConcurrentHashMap to be used as a backing Cache storage.

Overview of our Library:
POJOs - Book.java, Author.java
Service - LibraryService.java
Implementation of Library - Library interface, HomeLibraryImpl.java

Before we plug in Spring, we will create the classes mentioned above. Then we will use Spring to Autowire and Cache.
  1. Create a Maven project. Here is a pom file with all the jar dependencies needed for this application.

  2. Create simple POJOs. We have a Book which has a Title, Author and ISBN.


  3. LibraryService is our service class that exposes our Library operations. We will add Spring specific annotations later on to this class.

  4. We have a Library interface that has 3 methods, getBook(String title, Author author), getBook(long isbn) and addBook(Book book).

  5. We will create 1 implemention class for our Library. Since we are not hitting a database, all the books will be preloaded in a HashMap. We will call our implementation class HomeLibraryImpl. To make this example simple we will have 2 maps with different keys storing our books to make our search easier.

  6. Adding Spring Autowiring into our classes. We need to inject the HomeLibraryImpl into our LibraryService to be used as implementing class. Add @Autowired annotation in LibraryService to our Library property.

  7. Create spring application context file.

  • Line 16 : Used to activate annotations in beans that are registered in the application context, Lines 19 and 21. Spring scans for @Autowired annotation and the bean name defined in application context matches property name in LibraryService.
  • Line 17 : Informs Spring that it should process the cache annotations (i.e. @Cacheable)
  • Line 24 : We declare cacheManager bean for Spring cache and use SimpleCacheManager
  • Line 27, 28 : Nested beans to declare Concurrent Cache implementations


  • Adding Spring Cache annotations to your Service class. @Cacheable is used to demarcate methods that are cacheable - that is, methods for whom the result is stored into the cache so on subsequent invocations (with the same arguments), the value in the cache is returned without having to actually execute the method.

    Value "books" matches our nested bean in application context (Line 21). This means that the result of this method will be cached under "books" concurrent map. I added key to show you how we can concatenate method parameters to create a key. Spring uses SpEL for key expressions.
  • Testing your application with JUnit

    If you put a break point or a log statement inside getBook method in HomeLibraryImpl class, you will notice that on the second call to retrieve the book the method is not invoked and the Book object returned is from Cache and not from our HashMap.
  • Thursday, March 1, 2012

    Spring - Multiple Application Context files from Different Projects

    • Web Application needs to use application context files bundled in separate projects.
      1. Insert the following into web.xml file



        classpath* - searches for files in the classpath.


        Project A - contains projectA-context.xml


        Project B - contains projectB-context.xml


        Project C - contains application-context.xml


        Project D - contains application-context.xml


        All 4 context files will be loaded in that order if all 4 projects are in the classpath

    • JUnit testing with multiple application context files bundled in separate projects.
      1. Add all dependent projects to the classpath when executing JUnit
      2. Include other resources in your main application context

        Project A - executed with JUnit, Spring container initiallized with projectA-context.xml

        Project B - contains projectB-context.xml

        Project C - contains application-context.xml

        Project D - contains application-context.xml

        All 4 context files will be loaded in that order if all 4 projects are in the classpath
      3. Spring application context injecting into JUnit