Este conteúdo não está disponível no idioma selecionado.
Chapter 30. Integration with the Spring Framework
30.1. Integration with the Spring Framework Copiar o linkLink copiado para a área de transferência!
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 Copiar o linkLink copiado para a área de transferência!
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>
<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>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-spring4-remote</artifactId>
<version>${infinispan.version}</version>
</dependency>
30.3. Enabling Spring Cache Support Programmatically (Library Mode) Copiar o linkLink copiado para a área de transferência!
Spring’s cache support can be enabled programmatically in the application. To enable Spring support perform the following steps:
-
Add the
@EnableCachingannotation to the Spring configuration class in use. -
Define a method returning a
SpringEmbeddedCacheManagerannotated with@Bean.
The following code snippet highlights these changes:
Sample Programmatic Configuration
30.4. Enabling Spring Cache Support Programmatically (Remote Client-Server Mode) Copiar o linkLink copiado para a área de transferência!
Spring’s cache support can be enabled programmatically in the application by performing the following steps:
-
Add the
@EnableCachingannotation to the Spring configuration class in use. -
Define a method returning a
SpringRemoteCacheManagerannotated with@Bean.
The following code snippet highlights these changes:
Sample Programmatic Configuration
30.5. Adding Caching to Application Code Copiar o linkLink copiado para a área de transferência!
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) {...}
@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.
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: