You are here
Home > Interview >

Spring Boot Interview Questions & Answers

Spring Boot Interview Questions & AnswersIf we talk about a job interview in Java technology, Spring Boot becomes the most popular topic. Even if you don’t mention Spring Boot in your resume, you will reduce the chances of getting shortlisted. It happens because now-a-days Spring Boot is the most widely used framework for developing a Java based Application. Apart from that, it also used to develop microservices based projects in Java, which again is a trending architecture in the industry. Hence, Spring Boot becomes a ‘Must Know’ skill for a Java developer. Therefore, our topic for discussion is ‘Spring Boot Interview Questions & Answers’.

In this article, we will talk about the most important interview questions on Spring Boot. If you are looking for detailed tutorials on Spring Boot, kindly visit our Spring Boot Tutorial section. Let’s discuss out topic ‘Spring Boot Interview Questions & Answers’.

Q.1) What is Spring Boot?

Spring Boot is a combination of two words “Spring” and “Boot”. Here, the word ‘Spring’ represents a Spring Application. The word ‘Boot’ represents bootstrap. Hence, Spring Boot is an open-source java-based framework that helps in bootstrapping the Spring Application.

Here is the official definition of spring.io website : “Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can just run”.

In simple words, we can also say that Spring Boot is an extension of the Spring framework that simplifies the process of creating a Spring based Application. Now what are the characteristics of the Spring based Application that Spring Boot simplifies? These are: application is stand alone, production ready and just runnable.

Spring Boot can create a stand-alone spring application with minimal configuration needed. It automates approximately 70-80% repeated tasks that we do in Spring based application.

Spring Boot provides production-ready features such as metrics, health checks, and externalized configuration.

It has embedded web servers in the form of jar files that internally handle the process of running the application without manually configuring a web server. Moreover, we don’t have to worry about making the application’s jar/war and deploying it into any web server. These tasks and configurations are automated and done by Spring Boot internally that makes an application just runnable.

Q.2) Why Spring Boot became so popular?

Spring Boot became very popular in a very short duration as compared to other frameworks in the Java programming language. For example, below are some points that make Spring Boot a popular framework to develop a production-ready application.

1) Spring Boot provides ready-made functionalities that are the most common and applicable to any type of project. Hence, it eliminates boiler plate code.

2) It provides an easier and faster way to configure, set up & run both web-based as well as simple applications.

3) Spring Boot reduces XML configurations and promotes annotations. Hence, it reduces development effort significantly.

4) It creates a stand-alone application that can run using jar file. It’s not mandatory to create a war file, even to run a web application.

5) It offers production-ready features like health checks, metrics and externalized configurations.

6) Spring Boot provides an easy integration process with many third party tools.

7) Spring Boot provides a great support to develop Java Microservices.

8) It offers developers ready-made starter projects to quickly work on functionalities rather than wasting time in setting up initial configurations.

9) Spring Boot supports most commonly used build tools like Maven & Gradle.

Q.3) How does a Spring Boot Application start?

The Spring Boot application has a main method like a simple Java program. This method serves as an entry point, which invokes the run() method to start the application. Below code demonstrates the main class with the @SpringBootApplication annotation that has the main() method which internally calls the run() method.

@SpringBootApplication
public class MySpringBootApplication { 
   public static void main(String[] args) {
      SpringApplication.run(MySpringBootApplication.class, args); 
   } 
}

Q.4) What are the disadvantages of Spring Boot?

1) One of the disadvantage that many developers observed at the time of development is that it includes many additional dependencies which are not required. It increases the deployment file size. However, if we compare with the list of advantages, this disadvantage becomes negligible. If the size criteria are our priority, then this becomes really a big disadvantage.

2) Another disadvantage is that there is a challenge when we have a requirement of migration of legacy Spring based projects. However, we can rectify this problem by using Spring Boot CLI (Command Line Interface). It will help us to convert legacy code.

Q.5) What are the ways to create a Spring Boot Application?

Generally, there are three ways to create a Spring Boot Application as shown below:

1) Using Spring Boot Command Line Interface (CLI Tool)
2) Using Spring Tool Suite (STS IDE)
3) Using Spring Initializr Website

Out of them, the most commonly used way of developing a Spring Boot project in the industry is using STS (Spring Tool Suite) IDE. It is the most user friendly and has various features. It is known to save our development time. If you are new to STS, you can visit the separate article on ‘How to create a SpringBoot Project using STS?’

Q.6) What are the starter dependencies a Spring Boot Application?

In Spring Boot, we highly work with starter dependencies. The Starters are the ready made small projects that we can include in our application. Officially they are called dependency descriptors. Technically, the starters contain a lot of the dependencies that we need to get a project up and running quickly and with a consistent, supported set of managed transitive dependencies. We don’t have to manually search for required jars to run a project.

For example, if you want to develop a Spring Web Application, include the spring-boot-starter-web dependency in your project. However, if you are using STS (Spring Tool Suite) as an IDE, you need to just search for ‘Web’ and select ‘Spring Web’. The STS will automatically add the ‘spring-boot-starter-web’ dependency in your pom.xml.

All official starters follow a similar naming convention; spring-boot-starter-*, where * stands for a particular type of application. This naming structure is intended to help when you need to find a starter. As aforementioned, the Maven integration with many IDEs like STS lets you search dependencies by name. You can find the list of some commonly used starters from the link ‘Common Starters in Spring Boot‘.

Q.7) Which web sever does Spring Boot provide by default?

Spring Boot provides Tomcat as a default embedded server. Additionally, it also provides two more embedded servers: Jetty and Undertow.

Q.8) How can we switch to different web servers in Spring Boot?

We can switch to different servers by adding the Starter dependency in the pom.xml file. Like we can use spring-boot-starter-jetty as a dependency for using a jetty server in our project.

Q.9) How can we disable auto-configuration to a specific class in Spring Boot?

We can disable auto-configuration to a specific class by using exclude attribute of the @EnableAutoConfiguration annotation as below:

@EnableAutoConfiguration(exclude={className})

For example, below code snippet disables the DataSourceAutoConfiguration:

@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)

Moreover, We can also disable an auto-configuration with the ‘spring.autoconfigure.exclude’ environment property. Existance of this setting in the application.properties file does the same thing as before:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

Q.10) What is the concept of Profiles in Spring Boot?

During any project development, we have multiple environments such as Dev, Test, QA, Prod. Each environment requires a different configuration. For example, we might be using an embedded H2 database for dev, but we might have SQL Server, Oracle or DB2 for Prod. Even if we have the same database across all the environments, the URL will be different. To make this easy and clean, Spring has the provision of Profiles to keep the separate configuration of environments.

Q.11) What is the difference between Spring and Spring Boot?

1) Spring is a framework that requires some effort to develop enterprise level applications, whereas Spring Boot is an extension of Spring Framework that provides rapid application development (RAD) to develop an application faster.

2) Spring is popular for dependency injection, whereas Spring Boot is popular for auto-configuration.

3) Spring framework requires developers to setup the web server manually. Spring Boot has built-in servers like Tomcat, Jetty & Undertow.

4) Spring framework requires a deployment descriptor (web.xml) to run an application. Spring Boot doesn’t require a deployment descriptor to run an application.

5) Spring requires many manual configurations, whereas Spring Boot promotes auto-configuration.

6) Spring Framework requires a number of dependencies to create a web app. Spring Boot requires only one dependency to work on web app which is  ‘spring-boot-starter-web’.

7) Spring framework requires XML Configurations, whereas Spring Boot doesn’t require XML configuration.

8) Spring Boot provides CLI (Command Line Interface) tool to develop & test the application, whereas Spring framework doesn’t provide such kind of tool.

9) Spring framework requires manually defining dependencies for the Spring project in the pom.xml. Spring Boot offers the concept of starter in pom.xml file that internally takes care of downloading the dependent JARs automatically.

10) Spring Boot offers production-ready features such as metrics, health check and other reports. Spring does not offer such kind of features.

11) Spring framework doesn’t support in-memory databases. Spring Boot includes support for in-memory databases like H2.

Q.12) What Is Spring Boot Actuator? What is its purpose?

Actuator enables production ready features in a Spring Boot application. These features allow us to monitor and manage applications when they are running in production. Some of the features are health, metrics, info, dump, env, etc. It uses HTTP endpoints or JMX beans to enable us to interact with it. In order to enable Spring Boot Actuator, we just need to add the spring-boot-actuator dependency to our pom.xml as shown below.

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

Below are some of the most commonly used built-in endpoints that an Actuator provides:

  • env: represents environmental properties
  • health: displays application’s health condition
  • httptrace: shows HTTP trace information
  • info: displays application’s basic information
  • metrics: displays application’s metrics information
  • mappings: shows a list of all @RequestMapping paths in the application
  • beans: displays a complete list of all the Spring beans in the application
  • shutdown: allows the application to be gracefully shutdown

Q.13) What is the use of DevTools in Spring Boot?

Spring Boot DevTools is a feature provided by Spring Boot, where it restarts the application whenever any changes are being made in the code. Generally, we need to right-click on the project and run the application again and again. Spring Boot DevTools does this for us if there is any change in the code. The primary goal of this module is to save the time in development & testing. ‘spring-boot-devtools’ is the dependency that we need to add in order to take the benefit of this feature.

Q.14) What are the advantages of using yml file than properties file?

YAML(.yml) is a convenient format for specifying hierarchical configuration data. It is considered as more readable than its property file alternative since it does not contain repeated prefixes. Moreover, it also provides several unique and useful feature for us.

1) If we are using spring profiles, we can have multiple profiles in one single .yml file. On the other hand, each profile needs one separate .properties file.

2) .yml file has a hierarchical structure, whereas .properties doesn’t have a hierarchical structure.

3) While retrieving the values from .yml file we get the value as whatever the respective type (int, string etc.) is in the configuration. In case of the .properties files we get strings regardless of what the actual value type is in the configuration.

close

Leave a Reply

Top