Bird
0
0

What is wrong with this Spring Boot caching code?

medium📝 Debug Q14 of 15
Spring Boot - Caching
What is wrong with this Spring Boot caching code?
@Cacheable("users")
public User getUser(int id) {
  return userRepository.findById(id).get();
}

// Missing @EnableCaching annotation in main class
AThe method signature is incorrect
BThe cache name "users" is invalid
CCaching won't work without @EnableCaching annotation
DThe repository call is wrong
Step-by-Step Solution
Solution:
  1. Step 1: Check caching setup requirements

    Spring Boot requires @EnableCaching on a configuration or main class to activate caching support.
  2. Step 2: Identify missing annotation impact

    Without @EnableCaching, @Cacheable annotations are ignored and caching does not work.
  3. Final Answer:

    Caching won't work without @EnableCaching annotation -> Option C
  4. Quick Check:

    @EnableCaching is required to activate caching [OK]
Quick Trick: Always add @EnableCaching to activate caching [OK]
Common Mistakes:
  • Assuming @Cacheable works without enabling caching
  • Thinking cache name must be special format
  • Believing method signature causes caching failure

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes