@EnableCaching — Spring Technical Interview Questions & Answers

Turning on Spring's caching abstraction.

@EnableCaching

  • @EnableCaching turns on Spring’s caching features.
  • Put it on a configuration/main application class.
  • Without it, cache annotations may not work as you expect.
  • Enable first, then annotate service methods.

Technical Interview Tip: Enable caching before using cache annotations.

Code Examples for @EnableCaching

@EnableCaching — working example

// Topic: @EnableCaching
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
class TopicController {
  @GetMapping("/api/topic")
  String topic() {
    return "Spring Boot: @EnableCaching";
  }
}

@EnableCaching: GET /api/topic returns this title string.

How to Explain @EnableCaching to an Interviewer

What is @EnableCaching (sb cache)?

Used for a short @EnableCaching sb cache example a small REST app Not just the heading

Give one small example of @EnableCaching (sb cache).

the smallest @EnableCaching sb cache example you can type Turning on Spring's caching abstraction

What mistake do beginners make with @EnableCaching (sb cache)?

only saying “@EnableCaching sb cache” with no example Show the fix

Where do you use @EnableCaching (sb cache)?

a small REST app a short @EnableCaching sb cache example