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;
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
