Spring Boot interview questions for 5 years experience

By | October 11, 2024

1. Why Spring Boot over Spring framework?

Spring vs Spring Boot

The Spring Boot framework is built on top of the Spring framework, and it is used for a rapid production-ready environment that enables the developers to directly focus on the business logic instead of struggling with the configuration and setup; those will be automatically configured as we add dependencies to the pom.xml file. Spring Boot autoconfigures the necessary beans and components, allowing us to skip boilerplate codes.

2. How does autoconfiguration work in Spring Boot?

When we add any spring boot starter project or dependency, Maven pulls all the required jars of that project. In those jars, one of the jars is called the configuration jar. This configuration jar has a directory called META-INF, which contains the spring.factories file. 

The spring.factories file is actually responsible for the autoconfiguration; it contains all the configuration that needs to be active or deactivated depending on some conditions. For example: If we add spring-data-jap, then the JPA will be active and spring boot will autoconfigure JPA.

spirng.factories

3. What is Spring Boot starter parent maven dependency?

The spring boot starter parent dependency is a parent POM (Project Object Model) that simplifies the configuration in a Spring Boot application. It adds a wide range of default settings and dependencies provided by the Spring Boot framework, which helps to reduce the boilerplate configuration and ensure consistent dependency management. Following things are done by the spring boot starter parent dependencies:

  • Dependency Management: The spring boot starter parent dependency implicitly manages the versions for common dependencies such as Spring, Hibernate, Jackson, etc.
  • Common Properties: It implicitly defines the common properties in a Spring Boot application, such as Java version, encoding, etc.; if required, we can override these properties.
  • Default Plugin Configuration: It adds the default configuration of Maven plugins such as Maven JAR, Maven Surefire, etc.
XML
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.1.4</version> <!-- Example version -->
    <relativePath/> <!-- Lookup parent from Maven repository -->
</parent>

4. How to change default server in Spring Boot?

By default, the embedded server that comes in Spring Boot is Tomcat. We can change the default embedded server from Tomcat to any other server such as Jetty or Undertow by using the exclusion tag in the pom.xml file. We need to exclude the Tomcat dependency and add the Jetty dependency.

pom.xml
<dependencies>
    <!-- Spring Boot Web without Tomcat -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
    <!-- Add Jetty as the server -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
</dependencies>

5. What is Spring Boot actuators?

The Spring Boot actuator is a production-ready feature that is used to manage and monitor the application with the help of HTTP endpoints. We need to add the spring-boot-starter-actuator dependency, and Spring Boot will automatically configure it and expose an endpoint “/actuator”. Say our application is running locally; it will expose http://localhost:8080/actuator. We can include endpoint urls for beans, health, environment, cache, etc. using the configuration in the application.properties file.

application.properties
management.endpoints.web.exposure.include=beans
management.endpoints.web.exposure.include=cache

#We can enable all the endpoints using * operator
management.endpoints.web.exposure.include=*

#In order to change the base path from /actuator to something else 
management.endpoints.web.exposure.base-path=/admin

6. How to configure custom endpoint in Spring Boot actuator?

application.properties
#In order to change the base path from /actuator to something else 
management.endpoints.web.exposure.base-path=/admin

Leave a Reply

Your email address will not be published. Required fields are marked *