Spring Boot Embedded Caching
What You Will Learn
How to use Spring Boot’s @EnableCaching with the Infinispan embedded Spring Boot starter to cache data in-process, configured via an infinispan.xml file.
Prerequisites
-
Java 17+
-
Spring Boot
Step 1: Add Dependencies
Add the Infinispan Spring Boot embedded starter:
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-spring-boot4-starter-embedded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Step 2: Configure the Embedded Cache
Point to an Infinispan XML configuration file in application.properties:
management.endpoints.health.show-details=always
management.endpoints.web.exposure.include=*
infinispan.embedded.config-xml=infinispan.xml
Define the cache in infinispan.xml:
<infinispan>
<cache-container>
<jmx domain="infinispan.embedded.example"/>
<local-cache name="basque-names" statistics="true"/>
<local-cache name="other"/>
</cache-container>
</infinispan>
Step 3: Enable Caching and Use @Cacheable
Enable caching in your Spring Boot application:
@SpringBootApplication
@EnableCaching
@EnableScheduling
public class BasqueNamesCachingApp {
public static void main(String[] args) {
SpringApplication.run(BasqueNamesCachingApp.class);
}
}
Annotate repository methods with @Cacheable:
@Component
@CacheConfig(cacheNames = Data.BASQUE_NAMES_CACHE)
public class BasqueNamesRepository {
private static final Logger logger = Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
@Cacheable
public BasqueName findById(int id) {
logger.info("Call database to retrieve name by id '" + id + "'");
return new BasqueName(id, Data.NAMES.get(id));
}
}
Step 4: Run the Tutorial
mvn spring-boot:run
The application periodically looks up Basque names. The first lookup for each id logs "Call database"; subsequent lookups return from the embedded Infinispan cache.
What’s Next
-
Spring Boot remote caching for caching with an Infinispan Server
-
Spring Boot embedded sessions for HTTP session storage with embedded Infinispan


