Top Spring Boot Interview Questions
Java developers are now switching from Spring Framework to Spring Boot for enterprise application development in microservice architecture. Spring Boot has become a trending topic to be asked in interviews from Java backend developers in 2024.
I have spent quite some time to prepare a very comprehensive list of questions and answers being asked in spring boot interviews. I hope, this will benefit both freshers as well as experienced developers in their interview preparation.
Q1. What is Spring Boot and why we need this?
Spring framework has become complicated over the years with lot of features. Initial Spring project setup takes time due to following :-
- Include all required spring modules and related thirdparty libraries
- Take care of version compatibility of spring module with other thirdparty jars
- Understanding of writing spring context xml
- Setup tomcat and web.xml for web application deployment
- Build process to generate jar/war and deployment process
- Best practices to use spring modules
✰ Spring boot solves all this problems and help to create stand-alone, production-grade Spring based applications that you can just run.
Spring Boot take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
Features
- Create stand-alone Spring applications
- Embedded server (like Tomcat, Jetty or Undertow) to avoid complexity in application (WAR files) deployment
- Provide opinionated ‘starter’ dependencies to simplify your build and application configuration
- Automatically configure Spring and 3rd party libraries whenever possible
- Provide production-ready features such as metrics, health checks and externalized configuration
- Absolutely no code generation and no requirement for XML configuration
Q2. What @SpringBootApplication annotation do?
@SpringBootApplication comprises of three annotations which are widely used:-
- @EnableAutoConfiguration enable Spring Boot’s auto-configuration mechanism
- @ComponentScan enable @Component scan on the package where the application is located
- @Configuration allow to register extra beans in the context or import additional configuration classes
Q3. What happens in the background when a spring boot application runs?
- Looks for active profile and initialize properties and beans based on profile
- Automatically launch an embedded tomcat server if it is a web application means project has spring-boot-starter-web dependency in pom.xml
Q4. What is Spring Initializer?
We can quickly generate spring boot project by choosing language (Java, Kotlin, Groovy), builder (Maven,, Gradle), spring boot version, project metadata (group, artifact, name, description, package name, java version) and spring boot dependencies (spring-boot-starter-web, spring-boot-starter-jpa etc.) using Spring Initializer
Q5. What is spring-boot-starter-parent?
The spring-boot-starter-parent
is spring boot starter project. It can be used as a parent in our project’s pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
</parent>
parent project provide following things:-
- Default Java compiler version
- UTF-8 source encoding
- Dependency management inherited from
spring-boot-dependencies
POM which allow us to omitversion
tag for common dependencies. - Resource filtering only import required thirdparty libraries based on
application.properties
orapplication.yml
- Default configuration for maven plugins such as maven-failsafe-plugin, maven-jar-plugin, maven-surefire-plugin, maven-war-plugin.
Q6. What is default embedded server and port in spring boot? How to change server port?
Spring boot provides support for Tomcat, Jetty and Undertow embedded servers. Default embedded server is Tomcat and default port is 8080.
Default server port can be changed using server.port
property or command line argument -Dserver.port
.
Also read different ways to change server port in spring boot
You can always change the default server Tomcat to another embedded server such as Jetty or Undertow by:-
- excluding the dependency of tomcat
spring-boot-starter-tomcat
fromspring-boot-starter-web
and, - adding the dependency of other server like jetty
spring-boot-starter-jetty
.
<!-- Exclude Spring Boot's Default Tomcat Server -->
<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 Server Dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
Q7. What is default logging in Sprint Boot? How to change default logging?
Spring boot provides logback as default logging.
If you want to change default logging to log4j2 then:-
- exclude
spring-boot-starter-logging
fromspring-boot-starter
and, - add
spring-boot-starter-log4j2
dependency
<!-- Exclude Spring Boot's Default Logback Logging -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Add Log4j2 Dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Q8. How to generate deployable JAR and WAR file in Spring Boot?
Spring boot provides a maven plugin spring-boot-maven-plugin
to generate JAR and WAR files which we can add in pom.xml as follows:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
JAR
When this plugin is in place, we get a fat executable JAR after executing maven package phase. This JAR contains all the necessary dependencies, including an embedded server. Thus, we do not need to worry about configuring an external server.
WAR
When we want to generate a WAR file, we change packaging to war and change scope of embedded server as provided in pom.xml
<!-- Set packaging to war,
default value is jar if not provided -->
<packaging>war</packaging>
<!-- Change the scope of embedded server as provided,
so that WAR doesn't contain embedded server in its package -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Q9. How to configure properties in Spring Boot?
Spring Boot provides support for external configuration, allowing us to run the same application in various environments. We can use properties files, YAML files, environment variables, system properties, and command-line option arguments to specify configuration properties.
We can then gain access to those properties using the @Value
annotation, a bound object via the @ConfigurationProperties
annotation, or the Environment abstraction.
Here are the most common sources of external configuration:
- Command-line properties: Command-line option arguments are program arguments starting with a double hyphen, such as –-server.port=8080. Spring Boot converts all the arguments to properties and adds them to the set of environment properties.
- Application properties: Application properties are those loaded from the application.properties file or its YAML counterpart. By default, Spring Boot searches for this file in the current directory, classpath root, or their config subdirectory.
- Profile-specific properties: Profile-specific properties are loaded from the
application-{profile}.properties
file or its YAML counterpart. The{profile}
placeholder refers to an active profile. These files are in the same locations as, and take precedence over, non-specific property files.
Q10. What is Spring Boot Actuator?
Spring Boot Actuator provides production-ready features for monitoring and managing spring boot application by exposing many useful HTTP endpoints related to application health, info etc.
To Enable Actuator in spring boot application, we just need to include spring-boot-starter-actuator
dependency in the pom.xml file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Some of the most common built-in endpoints Actuator provides:
- info: Displays arbitrary application information
- health: Shows application health information
- auditevents: Exposes audit events information
- env: Exposes environment properties
- httptrace: Displays HTTP trace information
- metrics: Shows metrics information
- loggers: Shows and modifies the configuration of loggers in the application
- mappings: Displays a list of all @RequestMapping paths
- scheduledtasks: Displays the scheduled tasks in your application
- threaddump: Performs a thread dump
Q11. What is Spring Boot Devtools?
Spring Boot DevTools, is a set of tools making the development process easier. To Enable DevTools in spring boot application, we just need to add a dependency to the pom.xml file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
The spring-boot-devtools module is automatically disabled if the application runs in production. The repackaging of archives also excludes this module by default. Hence, it won’t bring any overhead to our final product.
By default, DevTools applies properties suitable to a development environment. These properties disable template caching, enable debug logging for the web group, and so on. As a result, we have this sensible development-time configuration without setting any properties.
Applications using DevTools restart whenever a file on the classpath changes. This is a very helpful feature in development, as it gives quick feedback for modifications.
By default, static resources, including view templates, don’t set off a restart. Instead, a resource change triggers a browser refresh. Notice this can only happen if the LiveReload extension is installed in the browser to interact with the embedded LiveReload server that DevTools contains.
Q12. What does it mean that Spring Boot supports relaxed binding?
Relaxed binding in Spring Boot is applicable to the type-safe binding of configuration properties.
With relaxed binding, the key of an environment property doesn’t need to be an exact match of a property name. Such an environment property can be written in camelCase, kebab-case, snake_case, or in uppercase with words separated by underscores.
For example, if a property in a bean class with the @ConfigurationProperties
annotation is named myProp, it can be bound to any of these environment properties: myProp, my-prop, my_prop, or MY_PROP.