Core Spring Boot Interview Questions
On this page19
- 1What is Spring Boot and how is it different from Spring Framework?
- 2What are the advantages of using Spring Boot?
- 3What is “starter” in Spring Boot?
- 4What is auto-configuration in Spring Boot?
- 5How do you create a Spring Boot application?
- 6What is the purpose of the @SpringBootApplication annotation?
- 7How do you externalize configuration in Spring Boot?
- 8How do you define a custom configuration property?
- 9What is the difference between @Component, @Service, @Repository, and @Controller?
- 10How do you handle exceptions in Spring Boot REST APIs?
- 11What is Spring Boot Actuator?
- 12How do you connect Spring Boot to a database?
- 13What is Spring Data JPA?
- 14How do you implement pagination and sorting in Spring Boot?
- 15How do you secure a Spring Boot application?
- 16What is the use of @RestController?
- 17How do you create a custom endpoint in Spring Boot Actuator?
- 18How do you profile environments in Spring Boot?
- 19How do you run code at application startup?
1. What is Spring Boot and how is it different from Spring Framework?
Spring Boot is an extension of the Spring Framework that simplifies the setup, configuration, and deployment of Spring applications. It provides auto-configuration, embedded servers, and production-ready features with minimal boilerplate code.
2. What are the advantages of using Spring Boot?
- Auto-configuration
- Embedded servers (Tomcat, Jetty, Undertow)
- Production-ready features (metrics, health checks)
- No code generation or XML configuration required
- Rapid development and easy deployment
3. What is “starter” in Spring Boot?
A starter is a set of convenient dependency descriptors you can include in your application. For example, spring-boot-starter-web includes all dependencies for building a web application.
4. What is auto-configuration in Spring Boot?
Auto-configuration automatically configures your Spring application based on the dependencies present in the classpath. It reduces the need for manual configuration.
5. How do you create a Spring Boot application?
- Use Spring Initializr
- Add
@SpringBootApplicationannotation to your main class - Run the main method
6. What is the purpose of the @SpringBootApplication annotation?
It is a combination of @Configuration, @EnableAutoConfiguration, and @ComponentScan. It marks the main class of a Spring Boot application.
7. How do you externalize configuration in Spring Boot?
Using application.properties or application.yml files, environment variables, or command-line arguments.
8. How do you define a custom configuration property?
Create a POJO with fields, annotate with @ConfigurationProperties, and enable it with @EnableConfigurationProperties or @Component.
9. What is the difference between @Component, @Service, @Repository, and @Controller?
All are stereotypes for Spring beans, but they indicate different roles:
@Component: Generic bean@Service: Service layer@Repository: Data access layer, adds exception translation@Controller: Web controller
10. How do you handle exceptions in Spring Boot REST APIs?
- Use
@ControllerAdviceand@ExceptionHandler - Return custom error responses
11. What is Spring Boot Actuator?
A set of production-ready features to help you monitor and manage your application (health, metrics, info, environment, etc.).
12. How do you enable and use Actuator endpoints?
Add spring-boot-starter-actuator dependency and configure endpoints in application.properties.
13. How do you connect Spring Boot to a database?
Add the appropriate starter (e.g., spring-boot-starter-data-jpa), configure datasource properties, and use Spring Data repositories.
14. What is Spring Data JPA?
A part of Spring Data that makes it easy to implement JPA-based repositories with minimal boilerplate.
15. How do you implement pagination and sorting in Spring Boot?
Use PagingAndSortingRepository or pass Pageable and Sort parameters to repository methods.
16. How do you secure a Spring Boot application?
Add spring-boot-starter-security, configure security settings, and use annotations like @PreAuthorize.
17. What is the use of @RestController?
It is a combination of @Controller and @ResponseBody, used to create RESTful web services.
18. How do you create a custom endpoint in Spring Boot Actuator?
Implement Endpoint or use @Endpoint annotation.
19. How do you profile environments in Spring Boot?
Use @Profile annotation and separate property files like application-dev.properties.
20. How do you run code at application startup?
Implement CommandLineRunner or ApplicationRunner interface.
