🇮🇳
🇮🇳
Limited-Time Offer!Get 20% OFF on all live courses
Enroll Now
P
PrakalpanaLive online tech training
Interview Prep⏱️ 14 min read📅 Jul 13

40 Spring Boot Interview Questions (2026) — With Detailed Answers

PM
Priya MenonBackend Tech Lead
📑 Contents (29 sections)

📌How to Use This Guide

These are the Spring Boot questions I hear most often as an interviewer, grouped from fundamentals to advanced. If you can answer these confidently, you will clear most backend rounds. Pair this with our Spring Boot course.

📌Fundamentals

1. What is Spring Boot and why use it?

Spring Boot is an opinionated layer on top of the Spring Framework that removes boilerplate. It gives you auto-configuration, starter dependencies, an embedded server (Tomcat/Netty), and production-ready features (Actuator). You go from zero to a running REST API in minutes.

2. Spring vs Spring Boot?

Spring is the core DI/IoC framework; Spring Boot is a rapid-development layer over it. Full breakdown: Spring vs Spring Boot.

3. What does @SpringBootApplication do?

It combines three annotations: @Configuration, @EnableAutoConfiguration, and @ComponentScan.

4. How does auto-configuration work?

Spring Boot scans the classpath and applies configuration conditionally using @Conditional annotations (e.g. @ConditionalOnClass, @ConditionalOnMissingBean). If spring-boot-starter-data-jpa and an H2 driver are present, it auto-configures a DataSource and EntityManager.

5. What is a starter dependency?

A curated set of dependencies. spring-boot-starter-web pulls in Spring MVC, Jackson, and Tomcat with compatible versions — no manual version juggling.

📌Dependency Injection

6. Constructor vs field injection — which is better?

Constructor injection. It makes dependencies explicit, allows final fields, and enables easy unit testing without reflection. Field injection (@Autowired on a field) hides dependencies and can't be made immutable.

7. What is the default bean scope?

Singleton — one instance per Spring container.

8. @Component vs @Service vs @Repository?

Functionally similar stereotypes. @Repository adds exception translation for persistence; @Service marks business logic; @Component is generic.

📌REST APIs

9. @Controller vs @RestController?

@RestController = @Controller + @ResponseBody, so return values are serialized directly to the response body (usually JSON).

10. How do you handle exceptions globally?

@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<ApiError> handleNotFound(ResourceNotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(new ApiError(ex.getMessage()));
}
}

11. How do you validate request bodies?

Use @Valid on the parameter and Bean Validation annotations (@NotNull, @Email, @Size) on the DTO.

12. @RequestParam vs @PathVariable?

@PathVariable binds a URI template segment (/users/{id}); @RequestParam binds a query parameter (/users?role=admin).

📌Data & Transactions

13. What does @Transactional do?

It wraps the method in a database transaction — commit on success, rollback on a runtime exception. By default it only rolls back on unchecked exceptions.

14. What is the N+1 query problem?

When fetching a list of entities triggers one extra query per associated entity. Fix it with JOIN FETCH, @EntityGraph, or batch fetching.

15. JpaRepository vs CrudRepository?

JpaRepository extends PagingAndSortingRepository (which extends CrudRepository) and adds JPA-specific methods like flush() and batch operations.

📌Security

16. How do you secure a Spring Boot API?

Add spring-boot-starter-security, configure a SecurityFilterChain, and typically use JWT for stateless auth — validate the token in a filter, set the SecurityContext.

17. Difference between authentication and authorization?

Authentication = who you are (login). Authorization = what you're allowed to do (roles/permissions).

📌Production & Advanced

18. What is Spring Boot Actuator?

It exposes production endpoints — /actuator/health, /metrics, /info — for monitoring and observability.

19. How do you manage configuration across environments?

application-{profile}.yml files activated via spring.profiles.active, plus externalized config (env vars, config server).

20. How do you improve startup time?

Lazy initialization, minimizing auto-config, and GraalVM native images via Spring Boot 3 AOT processing.

📌The Other 20

  • 21Explain the Spring bean lifecycle. 22. What is @ConditionalOnProperty? 23. How does @Async work? 24. What is a CommandLineRunner? 25. How do you write an integration test with @SpringBootTest? 26. What is @MockBean? 27. Difference between @Mock and @MockBean. 28. How does connection pooling (HikariCP) work? 29. What is optimistic vs pessimistic locking? 30. How do you paginate results? 31. What is CORS and how do you enable it? 32. How do you rate-limit an endpoint? 33. What are Spring profiles? 34. How does caching (@Cacheable) work? 35. What is idempotency and why does it matter for APIs? 36. How do you document APIs (OpenAPI/Swagger)? 37. What is the difference between RestTemplate and WebClient? 38. How do virtual threads change Spring Boot concurrency? 39. How do you handle database migrations (Flyway/Liquibase)? 40. How do you deploy Spring Boot with Docker + GitHub Actions?
  • 📌Final Tip

    Interviewers care about *why*, not just *what*. For every annotation, know the problem it solves. Practice explaining out loud.

    PM

    Written by

    Priya Menon

    Backend Tech Lead

    🚀 Master Interview Prep

    Join 5000+ developers

    Explore Courses →