Spring / Spring Boot 4 Basics Interview Questions
How does Spring Boot 4 handle dependency injection and what are the core stereotypes?
Spring Boot 4 retains the same core dependency injection (DI) model as all previous Spring versions. The DI container, stereotype annotations, and bean lifecycle are unchanged -- Spring Boot 4's changes are at the infrastructure and ecosystem level, not the DI model itself.
| Annotation | Use case | Specialisation of |
|---|---|---|
| @Component | Generic Spring-managed component | (base) |
| @Service | Business logic layer | @Component |
| @Repository | Data access layer; translates persistence exceptions | @Component |
| @Controller | Spring MVC controller (returns view names) | @Component |
| @RestController | REST API controller (returns response body) | @Controller + @ResponseBody |
| @Configuration | Bean factory class | @Component |
// Constructor injection (recommended in Spring Boot 4) @Service public class OrderService { private final OrderRepository repo; private final ProductClient client; private final EventPublisher events; // Spring Boot 4 recommends constructor injection: // - Makes dependencies explicit // - Enables final fields (immutability) // - No @Autowired annotation needed on single-constructor classes public OrderService( OrderRepository repo, ProductClient client, EventPublisher events) { this.repo = repo; this.client = client; this.events = events; } // With Lombok @RequiredArgsConstructor: // @Service // @RequiredArgsConstructor // public class OrderService { // private final OrderRepository repo; // private final ProductClient client; // ... // } } // @Primary: preferred bean when multiple implementations exist @Service @Primary public class DefaultOrderService implements OrderService { ... } // @Qualifier: inject specific implementation by name @Autowired @Qualifier("premiumOrderService") private OrderService orderService;
More Related questions...