Chapter 30. Integration with the Spring Framework

30.1. Integration with the Spring Framework

JBoss Data Grid allows users to define a Spring Cache provider, providing applications a method of easily adding caching support, and allowing users familiar with Spring’s programming model a way to have caching fulfilled by JBoss Data Grid.

30.2. Defining the Spring Maven Dependency

The Spring module is bundled separately from the Library and Remote Client-Server dependencies. The following Maven configuration should be used depending on how JBoss Data Grid is used:

pom.xml for Spring 4 in Library Mode

<dependency>
    <groupId>org.infinispan</groupId>
    <artifactId>infinispan-spring4-embedded</artifactId>
    <version>${infinispan.version}</version>
</dependency>

pom.xml for Spring 4 in Remote Client-Server Mode

<dependency>
    <groupId>org.infinispan</groupId>
    <artifactId>infinispan-spring4-remote</artifactId>
    <version>${infinispan.version}</version>
</dependency>

30.3. Enabling Spring Cache Support Programmatically (Library Mode)

Spring’s cache support can be enabled programmatically in the application. To enable Spring support perform the following steps:

  1. Add the @EnableCaching annotation to the Spring configuration class in use.
  2. Define a method returning a SpringEmbeddedCacheManager annotated with @Bean.

The following code snippet highlights these changes:

Sample Programmatic Configuration

import org.infinispan.configuration.cache.Configuration;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.eviction.EvictionStrategy;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.spring.provider.SpringEmbeddedCacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
[...]

@org.springframework.context.annotation.Configuration
@EnableCaching
public class Config {

[...]

    @Bean
    public SpringEmbeddedCacheManager cacheManager() throws Exception {
        Configuration config = new ConfigurationBuilder()
            .eviction()
            .strategy(EvictionStrategy.LRU)
            .maxEntries(150)
            .build();

        return SpringEmbeddedCacheManager(new DefaultCacheManager(config));
    }
[...]

30.4. Enabling Spring Cache Support Programmatically (Remote Client-Server Mode)

Spring’s cache support can be enabled programmatically in the application by performing the following steps:

  1. Add the @EnableCaching annotation to the Spring configuration class in use.
  2. Define a method returning a SpringRemoteCacheManager annotated with @Bean.

The following code snippet highlights these changes:

Sample Programmatic Configuration

import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.configuration.Configuration;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.spring.provider.SpringRemoteCacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
[...]

@org.springframework.context.annotation.Configuration
@EnableCaching
public class Config {

[...]

    @Bean
    public SpringRemoteCacheManager cacheManager() throws Exception {
        Configuration config = new ConfigurationBuilder()
            .addServer()
            .host(ADDRESS)
            .port(PORT)
            .build();

        return new SpringRemoteCacheManager(new RemoteCacheManager(config));
    }
[...]

30.5. Adding Caching to Application Code

Caching can be added to each application by utilizing the Spring annotations found in the Spring Cache Abstraction.

Adding a Cache Entry

To add entries to the cache add the @Cacheable annotation to select methods. This annotation will add any returned values to the indicated cache. For instance, consider a method that returns a Book based on a particular key. By annotating this method with @Cacheable:

@Cacheable(value = "books", key = "#bookId")
public Book findBook(Integer bookId) {...}

Any Book instances returned from findBook(Integer bookId) will be placed in a named cache books, using the bookId as the value’s key.

Important

If the key attribute is not specified then Spring will generate a hash from the supplied arguments and use this generated value as the cache key. If your application needs to reference the entries directly it is recommended to include the key attribute so that entries may be easily obtained.

Deleting a Cache Entry

To remove entries from the cache annotate the desired methods with @CacheEvict. This annotation can be configured to evict all entries in a cache, or to only affect entries with the indicated key. Consider the following examples:

// Evict all entries in the "books" cache
@CacheEvict (value="books", key = "#bookId", allEntries = true)
public void deleteBookAllEntries() {...}

// Evict any entries in the "books" cache that match the passed in bookId
@CacheEvict (value="books", key = "#bookId")
public void deleteBook(Integer bookId) {...]}