1. What is Spring Boot, and how does it differ from the Spring Framework?
Spring Boot is an extension of the Spring Framework that simplifies application development by offering default configurations, embedded servers, and reduced boilerplate code. Unlike the traditional Spring Framework, Spring Boot eliminates the need for manual configurations and XML-based setups.
2. What are the key features of Spring Boot?
- Auto-configuration
- Embedded servers (Tomcat, Jetty, Undertow)
- Spring Boot Starter dependencies
- Production-ready features like Actuator
- Opinionated Defaults to reduce complexity
3. What are Spring Boot Starters?
Spring Boot Starters are dependency descriptors that simplify application development. They bundle commonly used dependencies into a single starter, reducing the need for manual dependency management. Example: spring-boot-starter-web
for web applications.
4. What is Spring Boot Auto-Configuration?
Spring Boot’s Auto-Configuration feature automatically configures components based on classpath settings, beans, and property files. This reduces manual setup requirements.
5. What is the purpose of the @SpringBootApplication
annotation?
@SpringBootApplication
is a convenience annotation that combines @Configuration
, @EnableAutoConfiguration
, and @ComponentScan
into a single declaration, simplifying configuration.
6. How do you create a Spring Boot application?
You can create a Spring Boot application using:
- Spring Initializr (https://start.spring.io/)
- Manually setting up dependencies in a Maven/Gradle project
- Using Spring Boot CLI
7. What are embedded servers in Spring Boot?
Spring Boot provides built-in support for embedded servers like Tomcat, Jetty, and Undertow, allowing applications to run as standalone JAR files without requiring an external application server.
8. What is Spring Boot Actuator?
Spring Boot Actuator provides production-ready features such as monitoring, metrics, and health checks through built-in REST endpoints.
9. How do you configure properties in a Spring Boot application?
Spring Boot applications use application.properties
or application.yml
to define configurations like database URLs, port numbers, and logging levels.
10. What is Spring Boot DevTools?
Spring Boot DevTools is a developer-friendly feature that enables auto-restart, live reload, and faster application development.
11. How does Spring Boot handle logging?
Spring Boot uses SLF4J with Logback by default. It also supports Log4j2 and Java Util Logging (JUL) for better logging capabilities.
12. What is the difference between @RestController
and @Controller
?
@RestController
is a combination of @Controller
and @ResponseBody
, simplifying RESTful web services development. @Controller
is used for traditional MVC applications.
13. How do you connect Spring Boot with a database?
Spring Boot uses Spring Data JPA along with Hibernate to simplify database interactions. Configuration is done via application.properties
.
14. What is Spring Boot’s default database connection pool?
Spring Boot uses HikariCP as the default connection pool for database interactions.
15. How can you handle exceptions in a Spring Boot application?
Spring Boot provides exception-handling mechanisms such as @ExceptionHandler
, @ControllerAdvice
, and ResponseStatusException
.
16. What are Profiles in Spring Boot?
Profiles allow different configurations for different environments (e.g., dev, test, prod). They can be set using application-{profile}.properties
or @Profile
annotation.
17. What is Spring Boot Security?
Spring Boot Security simplifies authentication and authorization, integrating seamlessly with OAuth2, JWT, and database-based authentication.
18. How do you enable CORS in a Spring Boot application?
CORS can be enabled using @CrossOrigin
annotation at the controller level or by configuring CorsRegistry
in a WebMvcConfigurer
bean.
19. What are Microservices, and how does Spring Boot support them?
Microservices are independent services that communicate over REST or messaging. Spring Boot simplifies microservices development by offering Spring Cloud features, Eureka for service discovery, and Spring Boot Actuator for monitoring.
20. How do you deploy a Spring Boot application?
Spring Boot applications can be deployed as JAR or WAR files on cloud platforms (AWS, GCP), containerized using Docker, or hosted on traditional servers like Tomcat.