Sub topic 3
Enabling caching in a Spring Boot application
This chapter assumes you can build a Spring Boot project with Maven or Gradle, you understand beans and dependency injection, and you can edit application.properties. It covers the setup that every later caching annotation depends on.
What enabling caching does
Spring does not cache anything by default. Caching is applied by a proxy, which is a wrapper object Spring places around your bean. When you enable caching, Spring registers the infrastructure that creates these proxies for beans with caching annotations. Each proxy checks a cache before it calls your method.
Two things are needed:
- The cache starter on the classpath.
@EnableCachingon a configuration class.
Step 1: Add the starter
The starter pulls in the Spring cache support and a default cache provider.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>In Gradle, the same dependency is:
implementation 'org.springframework.boot:spring-boot-starter-cache'Step 2: Add @EnableCaching
Put the annotation on your main class or on any @Configuration class.
@SpringBootApplication
@EnableCaching
public class ShopApplication {
public static void main(String[] args) {
SpringApplication.run(ShopApplication.class, args);
}
}With these two steps in place, Spring Boot auto-configures a CacheManager. The CacheManager is the object that creates and looks up named caches. If you add no provider library and set no spring.cache.type, Boot falls back to a simple in-memory cache backed by ConcurrentHashMap. That is fine for learning, but it has no size limit and no expiration.
The flow at startup looks like this:
Common errors
The most common mistake is forgetting @EnableCaching. The application starts without any error, and the annotations on your methods are silently ignored. Every call runs the method body.
Other frequent problems:
- Missing starter. Without
spring-boot-starter-cache, the@EnableCachingannotation may compile (the core module has it) but you do not get Boot's cache auto-configuration or provider properties. - Unknown cache name. With some providers, using a name that is not listed in
spring.cache.cache-namesfails at runtime. The default in-memory manager creates caches on demand. - Annotating a non-bean. Only beans managed by Spring get proxies. An object created with
newis never cached.
Verifying that it works
A quick way to confirm that the setup is correct is to inject the CacheManager and print its cache names after you have called a cached method.
@Component
public class CacheReport implements CommandLineRunner {
private final CacheManager cacheManager;
public CacheReport(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}
@Override
public void run(String... args) {
System.out.println("CacheManager: " + cacheManager.getClass().getSimpleName());
System.out.println("Caches: " + cacheManager.getCacheNames());
}
}With the default setup, the output names ConcurrentMapCacheManager. Later chapters replace this with Caffeine or Redis by changing properties, with no change to your service code.
Exercise
Your service method has @Cacheable("products"), the application starts cleanly, but the method body runs on every call. Which two setup items do you check first?
Show answer
Check that @EnableCaching is present on a configuration class or the main class, and that spring-boot-starter-cache is on the classpath. Also confirm the call goes through a Spring-managed bean.