Here are 20 top important Spring Boot interview questions every developer should prepare for:
1. What is Spring Boot? Why is it used?
- Answer: Spring Boot is a framework designed to simplify the development of Java applications by eliminating boilerplate configurations. It provides features like auto-configuration, embedded servers, and starter dependencies for faster and easier application development.
2. What are the key features of Spring Boot?
- Auto-configuration of Spring components.
- Embedded servers like Tomcat, Jetty, and Undertow.
- Starter dependencies for preconfigured setups.
- Spring Boot Actuator for monitoring and management.
- Externalized configuration using
application.properties
orapplication.yml
.
3. What is the purpose of the @SpringBootApplication
annotation?
-
Answer:
Combines:
-
@Configuration
: Marks the class as a configuration class. -
@EnableAutoConfiguration
: Enables Spring Boot's auto-configuration. -
@ComponentScan
: Scans the package for components.
-
4. What is the difference between Spring and Spring Boot?
-
Answer:
- Spring Framework: Requires manual configuration of beans, XMLs, and dependencies.
- Spring Boot: Provides auto-configuration, embedded servers, and opinionated defaults to simplify setup.
5. How do you configure a database connection in Spring Boot?
-
Answer:
Add the following in
application.properties
:
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
Include the database driver dependency in your pom.xml
.
6. How do you create a RESTful web service in Spring Boot?
-
Answer:
Use
@RestController
to create endpoints. Example:
@RestController
@RequestMapping("/api")
public class UserController {
@GetMapping("/users")
public List<String> getUsers() {
return List.of("Alice", "Bob", "Charlie");
}
}
7. What are Spring Boot starters?
-
Answer:
Starters are pre-configured dependencies for specific functionalities. Examples:
-
spring-boot-starter-web
: For web applications. -
spring-boot-starter-data-jpa
: For JPA and Hibernate. -
spring-boot-starter-security
: For Spring Security.
-
8. What is the Spring Boot Actuator?
-
Answer:
A module for monitoring and managing applications in production. It provides endpoints like
/actuator/health
and/actuator/metrics
.
9. How do you handle exceptions in Spring Boot?
-
Answer:
Use
@ControllerAdvice
and@ExceptionHandler
for global exception handling:
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<String> handleException(RuntimeException ex) {
return new ResponseEntity<>("Error: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
10. What is the difference between @RestController
and @Controller
?
-
Answer:
-
@RestController
: Combines@Controller
and@ResponseBody
. It returns data (JSON/XML). -
@Controller
: Used for building web pages (views).
-
11. What is Spring Data JPA?
-
Answer:
A Spring module for simplifying data access using JPA. It provides repository interfaces like
JpaRepository
for common database operations.
12. What is the difference between CrudRepository
and JpaRepository
?
-
Answer:
-
CrudRepository
: Basic CRUD operations. -
JpaRepository
: ExtendsCrudRepository
and adds support for JPA-specific features like pagination and batch processing.
-
13. What is dependency injection, and how does Spring Boot implement it?
-
Answer:
Dependency Injection (DI) is a design pattern where objects are injected into a class rather than being created within it. Spring Boot uses annotations like
@Autowired
to implement DI.
14. What are Spring Profiles?
- Answer: Profiles allow you to define environment-specific configurations (e.g., dev, prod). Activate a profile using:
spring.profiles.active=dev
15. How does Spring Boot implement security?
-
Answer:
By adding the
spring-boot-starter-security
dependency, basic authentication is enabled by default. For custom security configurations, you can extendSecurityFilterChain
or useWebSecurityConfigurerAdapter
.
16. What is the purpose of the @Qualifier
annotation?
- Answer: Used to resolve ambiguity when multiple beans of the same type exist. Example:
@Service
public class MyService {
@Autowired
@Qualifier("beanName")
private MyBean myBean;
}
17. What is the use of @EnableAutoConfiguration
?
- Answer: It enables Spring Boot’s auto-configuration mechanism, scanning the classpath and configuring beans automatically based on the dependencies.
18. How does Spring Boot support microservices?
-
Answer:
- Embedded servers for standalone deployment.
- RESTful APIs for communication.
- Integration with Spring Cloud for service discovery, configuration management, and circuit breaking.
19. How do you test a Spring Boot application?
-
Answer:
Use Spring Boot Test features:
-
@SpringBootTest
: Loads the entire application context. -
@WebMvcTest
: Tests only the web layer. -
MockMvc
: Simulates HTTP requests for testing controllers.
-
20. How do you externalize configuration in Spring Boot?
-
Answer:
Store configurations in
application.properties
,application.yml
, or external files. You can override them with environment variables or command-line arguments.
Top comments (0)