Spring Boot Interview Questions: 1. Overview
Spring Boot Interview Questions: 1. Overview
▼ Guides ▼ About ▼
Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:
>> CHECK OUT THE COURSE
1. Overview
Since its introduction, Spring Boot has been a key player in the Spring ecosystem. This project makes our
life much easier with its auto-configuration ability.
In this tutorial, we'll cover some of the most common questions related to Spring Boot that may come up
during a job interview.
Further reading:
Top Spring Framework Interview Questions A Comparison Between
A quick discussion of common questions about the Spring Framework
Spring and Spring Boot
that might come up during a job interview.
Understand the difference between
Read more → Spring and Spring Boot.
Read more →
2. Questions
Q1. What Is Spring Boot and What Are Its Main Features?
Spring Boot is essentially a framework for rapid application development built on top of the Spring
Framework. With its auto-configuration and embedded application server support, combined with the
extensive documentation and community support it enjoys, Spring Boot is one of the most popular
technologies in the Java ecosystem as of date.
Here are a few salient features:
Starters – a set of dependency descriptors to include relevant dependencies at a go
Start Here Courses Guides
▼ About
▼ ▼
Q2. What Are the Differences Between Spring and Spring Boot?
The Spring Framework provides multiple features that make the development of web applications easier.
These features include dependency injection, data binding, aspect-oriented programming, data access and
many more.
Over the years, Spring has been growing more and more complex, and the amount of configuration such
application requires can be intimidating. This is where Spring Boot comes in handy — it makes configuring a
Spring application a breeze.
Essentially, while Spring is unopinionated, Spring Boot takes an opinionated view of the platform and
libraries, letting us get started quickly.
Here are two of the most important benefits Spring Boot brings in:
Auto-configure applications based on the artifacts it finds on the classpath
Provide non-functional features common to applications in production, such as security or health
checks
Please check out our other tutorial for a detailed comparison between vanilla Spring and Spring Boot.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0.RELEASE</version>
</parent>
We can go to the Spring Initializr site, choose a dependency management tool (either Maven or Gradle), a
language (Java, Kotlin or Groovy), a packaging scheme (Jar or War), version and dependencies, and
download the project.
This creates a skeleton project for us and saves setup time so that we can concentrate on adding business
logic.
Even when we use our IDE's (such as STS or Eclipse with STS plugin) new project wizard to create a Spring
Boot project, it uses Spring Initializr under the hood.
// other annotations
@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
public class MyConfiguration { }
// other annotations
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class MyConfiguration { }
We can also disable an auto-configuration with the spring.autoconfigure.exclude environment property. This
Start Here Courses Guides About
setting in the application.properties file does the same thing as before:
▼ ▼ ▼
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.baeldung.autoconfigure.CustomAutoConfigurati
on
If we build a project with Maven, that file should be placed in the resources/META-INF directory, which will
end up in the mentioned location during the package phase.
@Configuration
public class CustomConfiguration {
@Bean
@ConditionalOnMissingBean
public CustomService service() { ... }
}
Q9. How to Deploy Spring Boot Web Applications as Jar and War Files?
Traditionally, we package a web application as a WAR file and then deploy it into an external server. Doing
this allows us to arrange multiple applications on the same server. When CPU and memory were scarce,
this was a great way to save resources.
But things have changed. Computer hardware is fairly cheap now, and the attention has turned to server
configuration. A small mistake in configuring the server during deployment may lead to catastrophic
consequences.
Spring tackles this problem by providing a plugin, namely spring-boot-maven-plugin, to package a web
application as an executable JAR.
To include this plugin, just add a plugin element to pom.xml:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
With this plugin in place, we'll get a fat JAR after executing the package phase. This JAR contains all the
necessary dependencies, including an embedded server. So, we no longer need to worry about configuring
an external server.
We can then run the application just like we would an ordinary executable JAR.
Notice that the packaging element in the pom.xml file must be set to jar to build a JAR file:
<packaging>jar</packaging> Start Here Courses ▼ Guides ▼ About
▼
<packaging>war</packaging>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
After executing the Maven package phase, we'll have a deployable WAR file.
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class);
// other statements
}
}
The SpringApplication class then fires up a Spring container and auto-configures beans.
Notice we must pass a configuration class to the run method to work as the primary configuration source.
By convention, this argument is the entry class itself.
After calling the run method, we can execute other statements as in a regular program.
Relaxed binding in Spring Boot is applicable to the type-safe binding of configuration properties.
With relaxed binding, the key of a 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.
<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. So, 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.
For further information on this topic, please see Overview of Spring Boot DevTools.
Essentially, Actuator brings Spring Boot applications to life by enabling production-ready features. These
features allow us to monitor and manage applications when they're running in production.
Integrating Spring Boot Actuator into a project is very simple. All we need to do is include the spring-boot-
starter-actuator starter in the pom.xml file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Spring Boot Actuator can expose operational information using either HTTP or JMX endpoints. But most
applications go for HTTP, where the identity of an endpoint and the /actuator prefix form a URL path.
Here are some of the most common built-in endpoints Actuator provides:
env exposes environment properties
health shows application health information
httptrace displays HTTP trace information
info displays arbitrary application information
metrics shows metrics information
loggers shows and modifies the configuration of loggers in the application
mappings displays a list of all @RequestMapping paths
Please refer to our Spring Boot Actuator tutorial for a detailed rundown.
property server.port.
Programmatically – In our main @SpringBootApplication class, we can set the server.port on the
SpringApplication instance.
Using the command line – When running the application as a jar file, we can set the server.port as a
java command argument:
Q19. Which Embedded Servers Does Spring Boot Support, and How to Change
the Default?
As of date, Spring MVC supports Tomcat, Jetty and Undertow. Tomcat is the default application server
supported by Spring Boot's web starter.
Spring WebFlux supports Reactor Netty, Tomcat, Jetty and Undertow with Reactor Netty as default.
In Spring MVC, to change the default, let's say to Jetty, we need to exclude Tomcat and include Jetty in the
dependencies:
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
Similarly, to change the default in WebFlux to UnderTow, we need to exclude Reactor Netty and include
UnderTow in the dependencies.
Comparing Embedded Servlet Containers in Spring Boot has more details on the different embedded
servers we can use with Spring MVC.
3. Conclusion
This article went over some of the most critical questions on Spring Boot that can come up during a
technical interview.
We hope they help land that dream job!
Start Here Courses
▼ Guides ▼ About ▼
Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:
>> CHECK OUT THE COURSE
with Spring?
Download the E-book
TERMS OF SERVICE
PRIVACY POLICY
COMPANY INFO
CONTACT