Bird
0
0

Why does this caching setup fail?

medium📝 Debug Q7 of 15
Spring Boot - Caching
Why does this caching setup fail?
@EnableCaching
@Configuration
public class CacheConfig {
  @Cacheable("items")
  public List getItems() { return List.of("a", "b"); }
}
AMethod getItems is not public
BCaching annotations must be on Spring-managed beans
CCache name "items" is invalid
D@EnableCaching must be on the main application class
Step-by-Step Solution
Solution:
  1. Step 1: Understand Spring bean management

    For caching to work, methods must be in Spring-managed beans (e.g., @Service, @Component).
  2. Step 2: Analyze given code

    CacheConfig is a configuration class, but getItems() is not exposed as a bean method or in a bean class.
  3. Final Answer:

    Caching annotations must be on Spring-managed beans -> Option B
  4. Quick Check:

    Caching needs Spring beans [OK]
Quick Trick: Put @Cacheable on Spring beans like @Service [OK]
Common Mistakes:
  • Assuming any class with @EnableCaching works
  • Thinking cache names must be special
  • Believing @EnableCaching only works on main class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes