🚀 Introduction
Spring Boot simplifies backend development by reducing boilerplate code, auto-configuring dependencies, and enabling fast delivery of production-ready applications. Whether you're preparing for interviews or refreshing your knowledge, this guide covers the 20 most important Spring Boot questions with simple, practical answers.
1. What is Spring Boot?
Spring Boot is a Java-based framework that lets you create stand-alone, production-grade applications quickly with minimal configuration.
2. What are the key features of Spring Boot?
- Auto-configuration based on dependencies
- Embedded servers (Tomcat, Jetty)
- Production-ready tools like Actuator
- No need for XML configuration
- Project scaffolding using Spring Initializr
3. What is @SpringBootApplication
?
It combines:
-
@Configuration
– Declares configuration -
@EnableAutoConfiguration
– Enables auto-configuration -
@ComponentScan
– Scans packages for components
4. What is a Spring Bean?
A Bean is an object managed by Spring. Spring creates, configures, and injects beans into your app automatically.
5. What is Auto-Configuration?
Spring Boot configures your app based on the libraries present in the classpath. For example, adding spring-boot-starter-web
sets up a web server and MVC config.
6. What is Dependency Injection?
It's a design pattern where Spring automatically provides a class with the objects it needs instead of the class creating them.
7. What is Spring Boot Actuator?
It provides endpoints to monitor and manage your app in production:
-
/actuator/health
-
/actuator/metrics
-
/actuator/info
8. What is a Circular Dependency?
Occurs when two or more beans depend on each other in a cycle. Spring fails to resolve it and throws an exception.
9. What is @Qualifier
?
When multiple beans exist of the same type, use @Qualifier
to specify which one to inject.
@Autowired
@Qualifier("myServiceImpl")
private MyService service;
10. What is @RequestMapping
?
Used to map HTTP requests to controller methods. Variants like @GetMapping
, @PostMapping
make this more specific and easier to use.
11. What are the types of Bean Scopes?
-
singleton
– One instance per Spring container (default) -
prototype
– A new instance every time it’s requested -
request
– One instance per HTTP request -
session
– One instance per HTTP session -
application
– One instance per ServletContext
12. How does Spring Boot handle REST communication?
-
RestTemplate
– Traditional synchronous HTTP client. Easy to use and widely adopted, but being replaced in modern applications. -
WebClient
– Non-blocking, reactive HTTP client. Preferred for scalable and asynchronous systems. -
RestClient
– Introduced in Spring Boot 3. A simplified and fluent alternative toRestTemplate
, designed for clean and type-safe REST calls.
13. What is the Spring Boot Starter Dependency?
Spring Boot starters are a set of convenient dependency descriptors you can include in your project. They simplify build configuration by bundling commonly used libraries.
Examples:
-
spring-boot-starter-web
– for building RESTful APIs -
spring-boot-starter-data-jpa
– for database access using JPA -
spring-boot-starter-security
– for authentication and authorization
14. What is the difference between @Component
, @Service
, @Repository
, and @Controller
?
These annotations mark classes as Spring-managed beans and define their roles:
-
@Component
– Generic stereotype for any Spring-managed component -
@Service
– Marks a service class (used for business logic) -
@Repository
– Indicates a DAO class and adds exception translation -
@Controller
– Used for web controllers to handle HTTP requests
15. How do you handle exceptions globally?
Use @ControllerAdvice
and @ExceptionHandler
:
java
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity handle(Exception e) {
return ResponseEntity.status(500).body(e.getMessage());
}
}