Top Spring Boot Interview Questions & Answers
Core Framework Questions
Q1. What is Apache Spring Boot and what are its four foundational technical pillars?
Spring Boot is an opinionated framework built on top of Spring Framework designed to simplify application bootstrapping and deployment. Its core pillars are: (1) Auto-configuration (dynamically instantiating beans based on classpath inspection); (2) Starter Dependencies (curated transitively managed POM packages); (3) Embedded Servlet Containers (packaging Tomcat, Jetty, or Undertow programmatically inside executable JARs); (4) Production-Ready Actuator Metrics (health indicators, metrics, and environment inspection).
Q2. How does Spring Boot Auto-Configuration work under the hood?
Auto-configuration is initiated by
@EnableAutoConfigurationinside@SpringBootApplication.
AutoConfigurationImportSelectorreads candidate configuration class names:
- Spring Boot 2.x: From
META-INF/spring.factories.- Spring Boot 3.x: From
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports.- Each candidate class is filtered against
@Conditionalannotations:
@ConditionalOnClass: Checks if necessary library classes exist on the classpath.@ConditionalOnMissingBean: Checks if the developer has already registered a custom bean.@ConditionalOnProperty: Checks if target configuration properties are enabled.- Passing candidates register default bean definitions into the
ApplicationContext.
@SpringBootApplication ---> @EnableAutoConfiguration
โ
โผ
AutoConfigurationImportSelector
โ
โผ
Read AutoConfiguration.imports ---> Filter @Conditional Annotations ---> Register Beans
Q3. How does Spring Boot manage starter dependencies and version resolution via BOMs?
Spring Boot uses a Bill of Materials (BOM) pattern. Projects inheriting from
spring-boot-starter-parentleveragespring-boot-dependencies. The BOM defines compatible version numbers in a<dependencyManagement>section for hundreds of libraries (Jackson, Hibernate, JUnit). Sub-projects declare dependencies (e.g.,spring-boot-starter-data-jpa) without specifying explicit<version>tags, eliminating transitive dependency version conflicts.
Q4. How are Embedded Servlet Containers (Tomcat/Jetty) bootstrapped during application startup?
When
SpringApplication.run()executes, it instantiates aServletWebServerApplicationContext. During theonRefresh()lifecycle phase, the context searches the bean registry for aServletWebServerFactoryimplementation (e.g.,TomcatServletWebServerFactory). The factory programmatically instantiates an embeddedWebServerobject (TomcatWebServer), configures listener ports, binds theDispatcherServlet, and starts the container natively.
Q5. What is the difference between @Controller and @RestController in Spring MVC?
@Controlleris the traditional Spring MVC stereotype annotation used to handle requests and return view template names (e.g., Thymeleaf/JSP). To return raw JSON/XML data, handler methods must be explicitly annotated with@ResponseBody.@RestControlleris a composite annotation that combines@Controllerand@ResponseBody, automatically serializing all handler method return values into HTTP response body bytes viaHttpMessageConverter(e.g., JacksonObjectMapper).
Q6. What are the common Lombok traps in Spring Boot domain models and JPA Entities?
While Project Lombok (
@Data,@Getter,@Setter,@Builder) eliminates boilerplate code, using it carelessly on JPA entities introduces serious production bugs:
@Data/@EqualsAndHashCodeon JPA Entities:@Datageneratesequals()andhashCode()using all non-static fields. In JPA, loading lazy-loaded collections insidehashCode()triggers unexpected database queries (N+1 problem) or throwsLazyInitializationException. For JPA entities,equals()andhashCode()should rely strictly on a stable business key or database Primary Key (id).@BuilderField Initialization: Fields initialized at inline declaration (e.g.,private List<Item> items = new ArrayList<>();) are ignored when constructed via@Builder, resulting innullpointers unless annotated with@Builder.Default.
Q7. How does Global Custom Exception Handling work in Spring Boot?
Spring Boot provides centralized exception handling using
@RestControllerAdvice(or@ControllerAdvice) and@ExceptionHandler.
@RestControllerAdviceintercepts exceptions thrown by any@RestControlleracross the application.@ExceptionHandler(SpecificException.class)handles matching exception types and formats a standardized JSON response body (e.g.,ErrorResponse(timestamp, status, message, path)).
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<ErrorResponse> handleNotFound(ResourceNotFoundException ex, WebRequest req) {
ErrorResponse error = new ErrorResponse(
LocalDateTime.now(),
HttpStatus.NOT_FOUND.value(),
ex.getMessage(),
req.getDescription(false)
);
return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
}
}
Q8. How do you implement the Strategy Design Pattern in Spring Boot using Dependency Injection?
Spring automatically injects all implementations of an interface into a
Map<String, PaymentStrategy>orList<PaymentStrategy>.
- When injecting
Map<String, PaymentStrategy>, Spring uses the Spring bean name as the Map key.- This eliminates manual
switch-caseorif-elseblocks for dynamic strategy selection.
public interface PaymentStrategy {
void processPayment(double amount);
}
@Service("CREDIT_CARD")
public class CreditCardStrategy implements PaymentStrategy { ... }
@Service("PAYPAL")
public class PayPalStrategy implements PaymentStrategy { ... }
@Service
public class PaymentContext {
// Spring automatically populates map key = bean name ("CREDIT_CARD", "PAYPAL")
private final Map<String, PaymentStrategy> strategies;
@Autowired
public PaymentContext(Map<String, PaymentStrategy> strategies) {
this.strategies = strategies;
}
public void execute(String type, double amount) {
PaymentStrategy strategy = strategies.get(type);
if (strategy == null) throw new IllegalArgumentException("Invalid strategy: " + type);
strategy.processPayment(amount);
}
}
Q9. What are the key Spring REST Annotations and how do @PathVariable vs @RequestParam differ?
@PathVariable: Extracts dynamic values directly embedded in the URI path template (e.g.,/api/users/{id}โ@PathVariable Long id). Used to identify a specific resource.@RequestParam: Extracts query parameters appended after?in the URL (e.g.,/api/users?page=1&size=10โ@RequestParam int page). Used for filtering, sorting, or pagination.@RequestBody: Deserializes incoming HTTP POST/PUT JSON payload into a Java DTO via JacksonObjectMapper. Paired with@Validto trigger Bean Validation.
Q10. What is the difference between @Component, @Service, @Repository, and @Configuration?
All four are sub-stereotypes of
@Component, making them candidates for Spring auto-detection and component scanning:
@Component: Generic archetype for any Spring-managed component.@Service: Denotes service-layer business logic. Carries semantic intent.@Repository: Denotes data access objects (DAOs). Automatically enables Spring'sPersistenceExceptionTranslationPostProcessor, translating native DB driver exceptions (e.g., SQLException, HibernateException) into Spring's unifiedDataAccessExceptionhierarchy.@Configuration: Denotes bean definition source containing@Beanmethods. Spring proxies@Configurationclasses via CGLIB to ensure inter-bean method calls reuse existing singleton instances (Full@Configurationmode).
