Top 40+ Spring Boot Interview Questions and Answers – 2020

Table of Contents

This article has latest and most asked Spring Boot Interview questions and answers.

Spring Boot framework or module helps in developing applications easily. We have covered the Spring Boot framework from the basic level in our tutorials.

When you are expecting interviews for Java Professionals , Spring Boot knowledge is necessary and the interviewer can asks several questions on Spring Boot framework to test your knowledge.

The below list of Spring Boot interview question and answer is prepared to help you crack interview questions easily.


Q. What is Spring Boot ?

Spring Boot is a framework of module based on java language.

Spring Boot is developed on top of the Spring framework and this is also developed by the Pivotal team -The Spring Team.

This framework is used to develop the Microservices.

Spring Boot makes the process of developing and bootstrapping the Spring applications easier.

It is a Spring framework but with additional features like Rapid Application Development, Auto Configuration etc. This framework does not require XML configurations like Spring framework.

Spring Boot Tutorial

These simple points are enough making good point on What is Spring Boot ? But you can future read it into detail at below url.

Read More.

Q. Why we need Spring Boot / Goals of Spring Boot ?

Spring Boot was mainly required to make application development easier. Spring framework requires a lot of configuration and that’s why bootstrapping and developing applications takes a lot of time.

On the other hand, Spring Boot has auto configuration feature so the configuration is not needed or very less needed.

Below are some of the goals of Spring Boot :

  • It avoids the XML Configurations hence makes it faster to develop the application.
  • Spring Boot helps developing the ready-to-run applications.
  • Applications development is easier and faster.
  • Unnecessary boilerplate code is avoided.

Q. Features of Spring Boot

Below are some of the important features of Spring Boot :

  • Auto Configuration.
  • Type-safe Configuration
  • Logging
  • Web Development
  • Security
  • SpringApplication class
  • YAML Support
  • Externalized Configuration
  • Properties Files etc.

Q. Advantages of Spring Boot

Spring Boot framework has many advantages over the traditional frameworks. Some are mentioned below :

  • Spring Boot is easy to understand framework.
  • Application development is easier in Spring Boot.
  • The development time is lesser in Spring Boot framework.
  • Boilerplate code and Xml configurations are not required.
  • Spring Boot comes with Embedded servers such as Tomcat etc.
  • It has multiple plugin and tools available for development.
  • It can be easily integrated with Spring Security, Spring JDBC etc.

Q. Disadvantages of Spring Boot

In my view, I only found one limitation in Spring Boot framework, which is :

It is extremely difficult and time consuming to migrate the existing Spring applications to Spring Boot. However, developing a new project or application with Spring Boot is very easy.

Q. Spring vs Spring Boot

Spring Boot Tutorial

Q. Mention the Spring Boot Components

Spring Boot framework has six components , which are :

  1. Spring Boot Starters
  2. Spring Boot AutoConfigurator
  3. Spring Boot CLI
  4. Spring Boot Actuator
  5. Spring Initializr
  6. Spring Boot IDEs
Key Components of Spring Boot

For complete details on above mentioned components, Read More.

Q. What are different ways to create Spring Boot Application?

We can create Spring Boot Application with four different ways . To learn the complete steps on how to create application using any way, click on the specific link.

Q. How to create Spring Boot project using CLI ?

Spring Boot CLI is a tool provided by the Spring team. It is used to run and test the Spring Boot application using command prompt.

You can download the Spring Boot CLI tool from here.

Detailed steps to create Spring Boot project using CLI are available- here.

Q. How to create Spring Boot project using Spring Initializr API ?

Spring Initializr is an API / web tool. It is provided by Spring team .

We can easily generate projects using this API, below are simple steps :

  • Go to URL : https://start.spring.io/
  • Fill out the Project name, package etc.
  • Select the dependency β€œSpring Web” from the Dependencies option.
  • Click on Generate the Project.

Detailed overview with screenshots is available – here.

Q. How to create Spring Boot application using Maven ?

With Maven , we can create Spring Boot application by adding very few dependencies.

Detailed steps with screenshots here.

Q. How to create Simple Spring Boot application using Spring Starter Project Wizard ?

Spring Starter Project Wizard option can be used to create Spring Boot ready to run project in simple steps.

Steps with screenshots – here.

Q. What is Spring Boot Starter ?

Spring Boot Starters are dependency or a component in Spring Boot. It contains multiple jar files.

Spring Boot starter basically groups the related or transitive dependencies into single dependency. These are used by the framework to provide the Auto-Dependency Resolution.

Key Components of Spring Boot

Detailed overview and advantages of Spring Boot Starter are – here.

Q. Name few popular Spring Boot Starters ?

Below are some of the most used Spring Boot Starters :

  • spring-boot-starter – It is the core starter of Spring Boot. This starter includes the dependencies such as auto-configuration support, logging, YAML etc.
  • spring-boot-starter-jdbc – It is the jdbc starter of the Spring Boot. It is used for jdbc connection pool.
  • spring-boot-starter-web – It is the web starter of Spring Boot. It is used for building web applications such as RESTful, Spring MVC etc.
  • spring-boot-starter-data-jpa – It is the starter for application using Spring Data JPA with Hibernate.
  • spring-boot-starter-security – This is the starter used for Spring Security
  • spring-boot-starter-aop – This spring boot starter is used for aspect-oriented programming with AspectJ and  Spring AOP
  • spring-boot-starter-test – This starter is used for testing Spring Boot applications

Q. What is Spring Boot AutoConfigurator ?

Spring Boot AutoConfigurator is a component of Spring Boot. It is used to provide auto configuration feature of Spring Boot.

The main purpose of Spring Boot AutoConfigurator component is to reduce a lot of configuration requirements. A lot of configuration is needed in Spring framework development but Spring Boot does not want that.

The AutoConfigurator component is used for achieving this feature in Spring Boot.

Detailed overview of Spring Boot AutoConfigurator is here.

Spring Boot AutoConfiguration with Example – here.

Q. What is Spring Boot Actuator?

Spring Boot Actuator is a very important component of Spring Boot.

Spring Boot Actuator provides many essential features. some of the main features are mentioned below :

  • It provides the Management EndPoints to the Spring Boot Applications.
  • It has Spring Boot Application Metrics feature.

Spring Boot Actuator Complete Guide – Must read for interviews.

Q. What are some important Spring Boot Actuator Endpoints?

Below are some of the most used and important Spring Boot Actuator endpoints :

Spring Boot Actuator

Q. What is Spring Boot CLI ?

Spring Boot CLI is a new tool developed by Spring team. It is used for developing and testing the Spring Boot applications using the command prompt.

The Spring Command Line interface internally uses Spring Boot Starter and Spring Boot AutoConfigurate component to resolve the dependencies and to execute the application. It is very easy to run the Spring web applications using the Spring Boot CLI.

Below are steps required for Spring Boot CLI component :

  • Install the CLI
  • Setup the CLI
  • Develop the Spring Boot application
  • Testing the application

Develop application using Spring Boot CLI with Examples – here.

Q. What is Spring Boot Initializr ?

Spring Boot Initializr is an API. It is used to quick start new Spring Boot Projects easily.

This API can be downloaded from here.

Develop application using Spring Boot Initializr with examples explained- here.

Q. What are IDEs supported by Spring Boot framework ?

Some important IDEs supported by Spring Boot are –

  • Eclipse
  • IntelliJ IDEA
  • Spring STS etc.

Q. Why we need Spring Boot Initializr?

Spring Boot Initilizr API is used to simplify the development of Spring Applications. It provides initial project structure and build scripts.

Advantages of Spring Boot Initializr:

  • Reduces Development time
  • Increases Productivity

Q. What is Transitively Dependency Resolution Management?

Transitively Dependency Resolution Management can be defined with the below scenario :

Spring Boot Interview Questions

Suppose we have –

  • β€œA” dependency in build scripts
  • β€œA” is dependent on β€œB”
  • β€œB” is dependent on β€œC”

It will imply that “A” is also dependent on β€œC”.

In Spring Boot , build tools will download and add all three jar files β€œA”, β€œB” and β€œC” to the application classpath.

Q. What are the Spring Boot Annotations?

One of the great feature of Spring Boot is that it does not require any xml configurations. There are annotations exist in Spring Boot which helps in developing and bootstrapping spring boot application easily.

Spring Boot Annotations exists in below mentioned packages :

  • org.springframework.boot.autoconfigure
  • org.springframework.boot.autoconfigure.condition

Below are some important annotations :

  • @SpringBootApplication – Java class annotated with @SpringBootApplication annotation is considered as main class and the application starts from this class. Read More with Examples.
  • @EnableAutoConfiguration– This annotation enables the Spring Boot application’s auto-configuration feature. Which means Spring Boot will look for all the auto-configuration beans on its classpath and it automatically applies them. Read More with Examples.
  • @SpringBootConfiguration–  This annotation of Spring boot is used for annotating class as configuration classes. Read More with Examples.
  • @ImportAutoConfiguration- This annotation is Spring Boot is used to import and apply the specified auto-configuration classes. Read More with Examples.

More Annotation with details and Examples is – here.

Q. What is Spring Boot dependency management?

Dependency management in Spring Boot is basically used for automatically managing the dependencies and configuration without even specifying the version for any dependencies.

Spring Boot automatically upgrades all the dependencies when we upgrade the Spring Boot.

Read More @ Official documentation.

Q. What are the Spring Boot properties?

Spring Boot application / project has one property file – application.properties . We can specify the pre-defined properties of Spring Boot inside this file.

All these properties have their default values. We can set values for that properties in application.properties file.

Spring Boot Properties are basically used for setting the values such as – server port number, database connection and configurations etc.

Below are some of the common Spring Boot properties :

PropertyDefault valueDescription
banner.charsetUTF-8This property is used for setting the banner file encoding.
banner.locationclasspath:banner.txt This property is used for setting the banner file location.
logging.fileThis property defines the name of the log.
spring.application.indexThis property is used to set application index.
spring.application.nameThis property is used to set application name.
spring.application.admin.enabledfalseThis property is used to enable the admin features for the application.
spring.config.locationThis defined config locations.
spring.config.nameapplicationThis is used to set the config name.
spring.mail.default-encodingUTF-8This property is used to set the default MimeMessage encoding.
spring.mail.hostThis is used to set the SMTP server host for the application
spring.mail.passwordThis sets the login password of the SMTP server.
spring.mail.portThis sets the SMTP server post.
spring.mail.test-connectionfalseThis property is used to test that the mail server is available on startup.

Q. What happens in the background when we run a Spring Boot Application as β€œRun as Java Application”?

Whenever we execute a Spring Boot application as β€œRun as Java application”, the application itself launches the tomcat server automatically after identifying if your application is a web application.

Q. What are minimum requirements for Spring boot?

The version 2.1.7.RELEASE of Spring Boot requires following :

  • Spring Framework 5.1.9 +
  • Java 8 +

For Explicit build support =

  • Gradle 4.4+
  • Maven 3.3+

For Servlet Container Support

  • Tomcat 9.0 – Servlet Version 4.0
  • Undertow 2.0 – Servlet Version 4.0
  • Jetty 9.4 –  Servlet Version 3.1

Q. What is thymeleaf?

Thymeleaf is a server side Java template engine developed for web applications.

The purpose of thymeleaf is bringing the elegant and natural templates for your web application.

Thymeleaf can also be integrate with Spring Framework. It best works with the HTML5 Java web applications.

Q. How to use thymeleaf?

To use Thymeleaf into our Spring Boot Project, we need to add below dependency in our application’s pom.xml file :

<dependency>    

<groupId>org.springframework.boot</groupId>    

<artifactId>spring-boot-starter-thymeleaf</artifactId>    

</dependency>

Q. How to change the port of the embedded Tomcat server in Spring boot?

It is possible to change the embedded tomcat server port in Spring Boot.

We can change it in our project’s application.properties file.

We need to add below mentioned property in our application.properties file :

server.port

Suppose, You want to change the port of embedded tomcat server to 8081 then below code is required to be added in the application.properties file.

server.port = 8081

As soon as we mention the port number in the file, Spring Boot automatically loads the file again and required configuration will be automatically applied in the application.

Q. How to connect Spring Boot application to database using JDBC?

Spring Boot has starters and libraries available for connecting application with JDBC.

In below Example, We are creating a Spring boot application and establishing connection with Mysql database using JDBC.

Below are the steps to create and setup JDBC with Spring Boot application.

Step 1: Creating a database in MySQL

CREATE DATABASE testdb;

Step 2: Creating a table inside the database in MySQL

CREATE TABLE employees(id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, empname VARCHAR(60));  

Step 3: Create a Spring Boot Project

If you need any help in creating Spring Boot project – Read here.

Step 4: Add the Jdbc, Mysql and web dependencies into project

Step 5: Configure the Database into your project’s application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root  
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=create-drop  

Step 6:  Add below code in main MainApp.java class

package com.tbs;  

import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  

@SpringBootApplication  
public class MainApp{  

public static void main(String[] args) {  
SpringApplication.run(MainApp.class, args);  


} 
 

Step 7:  Creating a Controller to handle Http requests

package com.tbs;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyDBController {

@Autowired
JdbcTemplate jdbc;

@RequestMapping("/insert")
public String index(){
jdbc.execute("insert into employees(name) values('Alex')");
return "Employee Inserted Successful";
}

} 

Step 8: Now, Execute the project as a Java application.

Step 9:  Now test in the url – “localhost:8080/insert”, the output will be “Employee Inserted Successful”.

Q. How to enable HTTP/2 support in Spring Boot?

We can enable the HTTP/2 support in our Spring boot application by adding below property in application.properties file :

server.http2.enabled=true

Q. What is @RestController annotation in Spring Boot?

The @RestController annotation is a stereotype annotation.

This annotation is a class level annotation and the annotated class is defined as a Rest Controller. The annotated class is capable pf serving Rest API requests.

@RestController annotation adds the @Controller annotation and @ResponseBody annotation to the class.

This annotation is available in the package – org.springframework.web.bind.annotation. This package needs to be imported in order to use this annotation.

package com.tbs; 

import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 

@RestController 
public class StudentController { 

@RequestMapping("/name") 
public String studentName(){ 
return"Alex"; 
} 
} 

Complete Example of Rest API with Spring Boot.

Q. What is @RequestMapping annotation in Spring Boot?

The annotation @RequestMapping is used for providing the routing information for rest controllers.

This is a method level annotation. It tells which HTTP request should be mapped to the annotated method.

This annotation is available in the package – org.springframework.web.bind.annotation . This package needs to be imported in order to use this annotation.

package com.tbs; 

import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 

@RestController 
public class StudentController { 

@RequestMapping("/name") 
public String studentName(){ 
return"Alex"; 
} 
} 

Complete Example of Rest API with Spring Boot.

Q. How to connect Spring Boot to the database using JPA?

Spring Boot framework provides the starter dependency for jpa – spring-boot-starter-data-jpa. This starter is used to connect the Spring application with relational database .

This dependency needs to be added in the pom.xml of the project in order to implement it.

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

Below properties are needed in application.properties file :

# Hibernate Properties # The SQL dialect is used to generate the better SQL

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

# Hibernate ddl auto property

spring.jpa.hibernate.ddl-auto = update

Complete CRUD Example of Spring Boot Rest API with MySQL, JPA and Hibernate.

Q. What is the difference between JPA and Hibernate?

JPAHibernate
JPA is basically a Data Access Abstraction. It is used for reducing the amount of boilerplate code needed.

Hibernate is an ORM framework. It is an implementation of Java Persistence API. It offers many benefits including loose coupling etc.

Q. How to create a custom Spring Boot Actuator Endpoint?

We can create custom actuator endpoints in Spring Boot by using below mentioned annotations :

@Endpoint annotation is a class level annotation. It is used to create the custom actuator endpoint.

@ReadOperation, @WriteOperation, @DeleteOperation annotations are used at method level to expose the method as actuator endpoint bean.

@JmxEndpoint and @WebEndpoint annotations are used to create the custom technology specific actuator endpoints.

Example


package com.tbs;

import org.springframework.stereotype.Component;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;


@Endpoint(id="customendpoint")
@Component
public class CustomEndpointExample {

	@ReadOperation
	@Bean
	public String message() {
		return "returning message";
	}
}

Q. What do you understand by auto-configuration in Spring Boot and how to disable the  auto-configuration?

The best feature of Spring Boot is it’s auto configuration property. This feature makes Spring Boot application development faster and efficient.

Spring boot automatically configures the application’s required configurations (beans, controllers etc).

Suppose, your application’s classpath is containing the bean for a data source then the Spring boot will automatically configures the jdbc template.

Sometimes, there may be a requirement where you need to disable some auto configurations of Spring Boot. It is possible.

We can disable the auto-configuration property of Spring Boot by excluding the attribute of @EnableAutoConfiguration for the scenario where auto-configuration is not needed.

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

In case, the class is not in the application’s classpath then below code is needed to exclude the auto-configuration –

@EnableAutoConfiguration(excludeName={Test.class})

We can also disable the auto-configuration property for multiple classes at same place. We need to add the property – spring.autoconfigure.exclude in application.properties file with value as comma separated class names .

spring.autoconfigure.exclude = Test.class, Sample.class

Q. Mention the difference between @SpringBootApplication and @EnableAutoConfiguration annotation?

@SpringBootApplication @EnableAutoConfiguration
This annotation is used with the main class, from where application will be bootstrapped.This annotation is used to enable auto-configuration and the component scanning in your application.
This annotation is combination of three annotations –
@Configuration
@ComponentScan
@EnableAutoConfiguration

This annotation is combination of two annotations –
@Configuration
@ComponentScan

Q. Mention steps to deploy Spring Boot web applications as JAR and WAR files?

spring-boot-maven-plugin is used for deploying the Spring boot web applications.

We need to add the below code in our project’s pom.xml file :

<plugin>

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

With above mentioned plugin, we will get a jar file executing the package phase.

This JAR will have all the required dependencies , libraries and also an embedded server.

This will help is in running Spring Boot web application as an ordinary JAR file.

If we want our application to built as a jar file, we need to set the packaging element to the jar in the pom.xml file :

<packaging>jar</packaging>

Similarly, If we want our application to built as a war file, we need to set the packaging element to the jar in the pom.xml file :

<packaging>war</packaging>

Q. What are the possible sources of external configuration in Spring Boot?

Spring Boot is flexible for developers. Developers can run the spring boot application in different environments.

This is possible in Spring Boot only because it allows external configurations.

We can mention the required external configuration properties by using properties file, environment variables, YAML files , command-line arguments and system properties etc.

To access these properties, @value annotation is used.

Below are some of the mentioned possible sources of external configurations :

  • Application.Properties – Spring boot searches for properties in application.properties or YAML file or config directory to load the properties.
  • Command line properties – command-line arguments are also provided by Spring Boot . It converts these command line arguments to property and then it adds these properties to the set of environment properties.
  • Profile specific properties –  Profile specif properties are loaded from the application-{profile}.properties file or even from its YAML file. This type of file is available in the same location as that of the non-specific property files.

Q. What is the need for Spring Boot DevTools?

Dev Tools in Spring Boot are an elaborated set of tools.

The purpose of these tools is to make development of Spring boot application easier.

When the application is running in the production then automatically this module gets disabled. archives repackaging is also by default excluded.

Developer tools of Spring Boot applies properties to the respective development environments. 

Below code needs to be added in the pom.xml file for spring boot dev tools –

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

Q. How to deploy a different server with Spring Boot?

Below steps are required to be follow for deploying a different server in Spring boot –

  • First, you need to generate a WAR file from the project.
  • after that, deploy the generated WAR file into any server as per your requirement.

Please note that, steps can be different for different servers. These steps are the common steps.

Q. What is the difference between RequestMapping and GetMapping?

The annotation @GetMapping is basically a composed annotation.

It acts as a shortcut for an annotation like – @RequestMapping(method = RequestMethod.GET).

Both of these methods support the consumes and below are some of the consume options –

consumes = β€œtext/plain”
consumes = {β€œtext/plain”, β€œapplication/*”}

That’s all folks.

If you have any question, Please post it into below comments section.

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply