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โ€™.

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?

Commonly, 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. They include a set of commonly used libraries, configurations, and dependencies related to a specific task. Starters help developers get started quickly with minimal configuration. 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.
Spring Boot Profiles allow us to define different sets of configurations for different environments such as development, testing, production. We can use profiles to customize properties, data sources, and other settings based on the target environment, making it simpler to manage application configurations across various deployment setups.

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.

Q.15) What is GraalVM Native Image Support in Spring Boot?

This is the new feature introduced in Spring Boot 3.0 release. GraalVM Native Images provide a new way to deploy and run Java applications. Compared to the Java Virtual Machine, native images can run with a smaller memory footprint and with much faster startup times.

GraalVMย is a high-performance JDK intended to speed up the execution of applications written in Java and other JVM languages while also offering runtimes for JavaScript, Python, and a number of other popular languages. GraalVM offers two ways to run Java applications: on the HotSpot JVM with Graal just-in-time (JIT) compiler or as an ahead-of-time (AOT) compiled native executable.

GraalVM Native Imagesย are standalone executables that can be generated by processing compiled Java applications ahead-of-time. Native Images generally have a smaller memory footprint and start faster than their JVM counterparts. They are well suitable for applications that are deployed using container images. A GraalVM Native Image is a complete, platform-specific executable. We do not need to ship a Java Virtual Machine in order to run a native image.

If you want to know more about it and experiment with GraalVM you can go ahead with the official documentation on โ€œGraalVM Native Image Supportโ€.

Q.16) What are the new features of Spring Boot 3.0?

Spring Boot 3.0 is officially released in November, 2022. The major features in Spring Boot 3.0 are:

1) A Java 17 baseline

2) Support for Jakarta EE in place of Java EE

3) Native images support using GraalVM.

4) Improved observability with Micrometer and Micrometer Tracing

A separate article โ€˜New Features in Spring Boot 3.0 and Spring 6โ€˜ is available for the complete new features of Spring Boot 3.0.

Q.17) What is Spring Bootโ€™s auto-configuration feature, and how does it work?

Spring Bootโ€™s auto-configuration automatically configures application components based on classpath dependencies. It scans the classpath for specific libraries and provides default configurations for them. However, we can override or customize these configurations if needed. It focuses on minimizing the need for explicit configuration and reducing boilerplate code, encouraging developers to get started quickly with Spring Boot projects.

Q.18) How can you secure a Spring Boot application?

Spring Boot comes with very basic level inbuilt security features if we include spring-boot-security starter project into it. However, we can expand advanced level security features in Spring boot using different techniques. Some of them are:

  • ย Spring Security: Configuring authentication and authorization.
  • ย OAuth2: Implementing OAuth2-based authentication for APIs.
  • ย JWT (JSON Web Tokens): Securely transmitting authentication data.
  • ย Role-based access control (RBAC): Defining access policies based on user roles using UserDetailsService.
  • ย LDAP Authentication: Control userโ€™s authentication and authorization.

Q.19) How does Spring Boot handle exceptions in a web application?

Spring Boot comes with a powerful inbuilt exception handling. To explain this, we will consider the most commonly used flows which are Spring Boot MVC and Spring Boot REST. Both flows work based on a Controller, either it is a normal controller or a RestController. In both the cases, any request first interacts with DispatcherServlet before sending the response to the client, whether it is returning a successful result or failure after raising any exception. If it returns any failure result, DispatcherServlet calls a predefined functional interface ErrorController. In this case BasicErrorController (an implementation class of interface ErrorController) normally handles the request.

Furthermore, we can create our custom exception handling mechanism by extending existing APs easily. Spring Boot provides centralized exception handling through the `@ControllerAdvice` annotation. We can create a global exception handler that captures and processes exceptions thrown by controllers. This approach promotes consistent error responses and enhances maintainability.

Q.20) What are the key differences between Spring Boot and Spring Framework when it comes to configuration?

  • Spring Boot: Spring Boot follows a convention-over-configuration approach. It provides reasonable defaults and auto-configuration, eliminating the need for extensive configuration files or Java code. Developers can get started quickly with minimal setup.
  • Traditional Spring Framework: The traditional Spring Framework requires manual configuration using XML or Java-based configuration classes. Developers must define beans, wiring, and various settings explicitly.

โ™ฅ If you are looking for interview questions on Spring Core, kindly visit a separate article on Spring Core Interview Questions and Answers.

ย 

Leave a Reply


Top