Top Spring Boot Interview Questions (2023)
Top Spring Boot Interview Questions (2023)
Open In App
Spring Boot Interview Questions
In this article, we’ve covered the top 50 spring boot interview questions and answers,
which are categorized as basic to advanced interview questions. These questions are
designed for both freshers as well as experienced candidates. By preparing with these diverse
interview questions, you can increase your chances of acing your next spring boot interview
round.
Table of Content:
Java developers may use the Spring Boot framework independent, executable spring
applications that are production-grade. Rather than a thorough Spring configuration setup,
you might start out with simple configurations.
To read more, refer to the article – Introduction to Spring Boot
Easy to use: The majority of the boilerplate code required to create a Spring application is
reduced by Spring Boot. This shortens the time it takes to create apps and makes it simple
for developers to get started with Spring Boot.
Rapid Development: Spring Boot’s opinionated approach and auto-configuration enable
developers to quickly develop apps without the need for time-consuming setup, cutting
down on development time.
Scalable: Spring Boot apps are intended to be scalable. This implies they may be simply
scaled up or down to match your application’s needs.
Production-ready: Metrics, health checks, and externalized configuration are just a few of
the features that Spring Boot includes and are designed for use in production environments.
This helps facilitate the implementation of Spring Boot applications to the production
environment and contributes to the dependability of your applications.
To read more, refer to the article – Difference between Spring and Spring Boot
Spring Boot works by scanning your application for annotations. These annotations tell
Spring Boot what kind of application you are building and how to configure it. For example,
if you have an annotation that indicates that your application is a web application, Spring
Boot will automatically configure a servlet container and a web server.
Spring Boot also provides a number of starters. These are dependency descriptors that
make it easy to add common features to your application. For example, if you want to add a
database to your application, you can add the Spring Boot starter for JDBC. This will
automatically add the necessary dependencies to your project and configure the database
connection for you.
Here are the main steps involved in how Spring Boot works:
1. You start by creating a new Spring Boot project.
2. You add the necessary dependencies to your project.
3. You annotate your application with the appropriate annotations.
4. You run your application.
To read more, refer to the article –Best Practices For Structuring Spring Boot
Application
A Spring application gets started by calling the main() method in the SpringApplication
class. This method takes a SpringApplicationBuilder object as a parameter, which is used to
configure the application. The SpringApplicationBuilder object can be used to set the
application’s name, version, and other properties.
Once the SpringApplication object is created, the run() method is called. This method starts
the application by creating an application context and initializing it.
Once the application context is initialized, the run() method starts the application’s
embedded web server. The embedded web server is used to serve the application’s web
pages.
Example:
Java
import [Link];
import [Link];
@SpringBootApplication
public class MyApplication
{
public static void main(String[] args) {
[Link]([Link], args);
}
}
The Spring Boot framework offers a feature called Spring Boot dependency management
that makes it easier to manage dependencies in a Spring Boot project. It makes sure that all
necessary dependencies are appropriate for the current Spring Boot version and are
compatible with it.
For example, if you want to use Spring Boot to create a web application, you can add the
Spring Boot starter web dependency to your application. This dependency will contain all of
the dependencies that you need to use Spring Boot’s web features, such as Spring MVC
and Tomcat.
To read more, refer to the article – Spring Boot – Dependency Management
In Spring Boot, starters are a collection of pre-configured dependencies that make it easier
to develop particular kinds of applications. A curated selection of dependencies relating to a
specific functionality or technical stack is included in the starters.
These starters include all of the dependencies, version control, and configuration needed to
make certain features of a Spring Boot application functional.
To use a Spring Boot Starter, you simply add the dependency to your project’s [Link] file.
For example, to add the Spring Boot starter web dependency, you would add the following
dependency to your [Link] file:
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Spring Boot Actuator is a component of the Spring Boot framework that gives your
applications production-ready operational monitoring and management capabilities. You can
manage and monitor your Spring Boot application while it is running thanks to a set of pre-
built endpoints and metrics that are provided.
To use Spring Boot Actuator, you simply need to add the spring-boot-starter-actuator
dependency to your project. Once you have added the dependency, Spring Boot will
automatically configure your application to expose the actuator endpoints.
To read more, refer to the article – Spring Boot Actuator
Spring Boot CLI is a command-line tool that can be used to create, run, and manage Spring
Boot applications. It is a powerful tool that can help you to get started with Spring Boot
quickly and easily.
It is built on top of the Groovy programming language and leverages Spring Boot’s powerful
features to streamline the development process.
For various environments or application scenarios, you can configure and manage various
sets of bean definitions and configurations using Spring profiles. You can define particular
configurations that are only activated when the corresponding profile is active by using
profiles.
To use Spring Profiles, you simply need to define a set of profiles in your application’s
configuration. You can then use the [Link] property to specify which profile
you want to use.
Spring MVC is a web MVC framework built on top of the Spring Framework. It provides a
comprehensive programming model for building web applications.
The MVC architectural pattern separates the application into three layers:
The model: The model represents the data that is used in the application.
The view: The view is responsible for displaying the data to the user.
The controller: The controller is responsible for handling user requests and interacting
with the model and view.
To read more, refer to the article – Spring MVC – Basic Example using JSTL
Spring Boot can be used to create command-line applications. This is done by excluding
the Spring Boot web starter dependency from the project’s [Link] file. This will prevent
Spring Boot from automatically configuring the application for web applications.
To create a command-line application in Spring Boot, you can use the Spring Boot CLI. The
following command will create a new command-line application called “my-command-line-
project[name]”:
This will create a new project called “my-command-line-project[name]” with the Spring Boot
starter dependency excluded. You can then start developing your command-line application.
To run the command-line application, you can use the following command:
To use a Spring Boot starter dependency, you simply need to add it to your project’s
[Link] file. For example, to add the Spring Boot starter web dependency, you would add
the following dependency to your [Link] file:
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring init: This command initializes a new project using Spring Initializr. You can use this
command to create a new project with a specific set of dependencies.
spring run: This command runs a Spring Boot application from the command line. You can
use this command to quickly start up a Spring Boot application for testing or debugging
purposes.
spring test: Runs tests for a Spring Boot application. This command automatically detects
and runs tests in your project.
spring start: Starts a new interactive Spring Boot shell. The shell provides a command-line
environment for running and managing Spring Boot applications.
spring help: Displays the help documentation for the Spring Boot CLI, listing available
commands and their usage.
Yes, it is possible to change the port of the embedded Tomcat server in a Spring Boot
application.
A simple way is to set the server. port property in your application’s [Link]
file. For example, to set the port to 8081, you would add the following property to your
[Link] file:
[Link]=8081
The default port of the embedded Tomcat server in Spring Boot is 8080.
You can change the default port by setting the server. port property in your application’s
[Link] file.
To read more, refer to the article – Spring Boot – Project Deployment Using Tomcat
22. Can we disable the default web server in the Spring Boot application?
Yes, you can disable the default web server in the Spring Boot application.
To do this, you need to set the [Link] property to -1 in your application’s
[Link] file.
To disable a specific auto-configuration class in a Spring Boot application, you can use the
@EnableAutoConfiguration annotation with the exclude attribute. This allows you to
exclude the auto-configuration class from being applied during the application startup
process.
For example, the following code would exclude the
Java
[Link]
@SpringBootApplication
@EnableAutoConfiguration(exclude = { [Link] })
public class MyApplication {
public static void main(String[] args)
{
[Link]([Link], args);
}
}
24. Describe the flow of HTTPS requests through the Spring Boot
application.
@RequestMapping(value = “/example”,
Example @GetMapping(“/example”)
method = [Link])
27. How to get the list of all the beans in your Spring boot application?
Using the ApplicationContext object in Spring Boot, you can retrieve a list of all the beans in
your application.
The ApplicationContext is responsible for managing the beans and their dependencies.
The Environment object in a Spring Boot application can be used to check the
environment’s properties. Access to the configuration settings for the application, including
those defined in property files, command-line arguments, environment variables, and other
sources, is made possible by the Environment.
You can get the Environment instance by calling the getEnvironment() method on the
SpringApplication class.
A design pattern called Dependency Injection (DI) enables us to produce loosely coupled
components. In DI, an object’s ability to complete a task depends on another object, the
dependency object. However, the dependent object requests that another object (the
injector object) provide the dependency object for it rather than creating the dependency
object itself.
To read more, refer to the article – Spring – Dependency Injection by Setter Method
Constructor injection: This is the most common type of DI in Spring Boot. In constructor
injection, the dependency object is injected into the dependent object’s constructor.
Setter injection: In setter injection, the dependency object is injected into the dependent
object’s setter method.
Field injection: In field injection, the dependency object is injected into the dependent
object’s field.
To read more, refer to the article – Spring Dependency Injection with Example –
GeeksforGeeks
A key component of a spring core framework or runtime environment that controls the
creation, configuration, and dependency injection of objects (often referred to as beans) in
an application is an IoC (Inversion of Control) container, also referred to as a DI
(Dependency Injection) container.
To read more, refer to the article – Spring – IoC Container
An object that is managed by the Spring IoC container is referred to as a spring bean. The
creation, configuration, and dependencies of objects in a Spring application are managed
using this fundamental idea of the Spring Framework.
A Spring bean can be any Java object, but it typically has the following characteristics:
It is annotated with the @Component annotation or one of its derivatives.
In Spring, an Inner Bean refers to a bean that is defined within the scope of another bean’s
definition. It is a way to declare a bean inside the configuration of another bean, without
explicitly giving it a unique identifier.
To define an Inner Bean in Spring, you can declare it as a nested <bean> element within
the configuration of the enclosing bean.
Here’s an example XML configuration:
XML
Inner Beans can also be defined using the Java-based configuration in Spring, using the
appropriate annotations.
To read more, refer to the article – Spring @Bean Annotation with Example
Dependencies can be
Promotes immutability as
Immutability changed dynamically after
dependencies are set at creation.
object creation.
Provides flexibility as
Limited configurability as
dependencies can be
Configurability dependencies are set during object
creation and cannot be changed later. changed dynamically at
runtime.
Bean wiring is a mechanism in Spring that is used to manage the dependencies between
beans. It allows Spring to inject collaborating beans into each other.
There are also two main types of bean wiring:
Autowiring: This is a feature of Spring that allows the Spring container to automatically
wire beans together. Autowiring can be done by name or by type.
Manual wiring: This is the process of manually wiring beans together. This is done by
the developer, who specifies the dependencies of a bean in the bean configuration file.
To read more, refer to the article –Spring @ComponentScan Annotation with Example
40. Mention the steps to connect the Spring Boot application to a database
using JDBC.
Add the spring-boot-starter-jdbc dependency to your project. This dependency will provide
you with the JDBC driver for your database and the Spring Boot auto-configuration for
JDBC.
Create an [Link] file and configure the database connection properties. The
following are some of the properties that you need to configure:
[Link]: The JDBC URL for your database.
Create a JdbcTemplate bean in your application. The JdbcTemplate bean is a Spring bean
that provides you with a simple way to execute SQL queries and statements against your
database.
Use the JdbcTemplate bean to execute SQL queries and statements against your database.
To read more, refer to the articles
Spring JDBC Example
Spring – JDBC Template
Spring Boot – JDBC
URL path
URL parameters
Request headers
43. What are the steps to connect an external database like MySQL or
Oracle?
To read more, refer to the articles –Spring Boot – CRUD Operations using MySQL
Database
44. Mention the advantages of the YAML file over than Properties file and
the different ways to load the YAML file in Spring boot.
Advantages:
Easy to Edit and Modify: YAML files are easier to edit and modify manually, especially
when dealing with large or nested configurations. The hierarchical structure and
indentation make it less error-prone when making changes.
Conciseness: YAML syntax allows for a more concise representation of data compared
to the key-value pairs in properties files. This can lead to shorter and more readable
configuration files.
Complex data types: YAML supports complex data types like lists and maps, allowing
for more flexible and expressive configuration. This is particularly useful when dealing
with nested or multi-valued properties.
To read more, refer to the article – Difference between YAML(.yml) and .properties file in
Java SpringBoot
Spring Data REST is a framework that exposes Spring Data repositories as RESTful web
services. It allows you to expose repositories as REST endpoints with minimal
configuration.
Spring Data REST uses the following technologies:
Spring Data: Spring Data provides a unified abstraction for accessing data from different
data stores.
Spring MVC: Spring MVC is a web framework that provides a way to build RESTful web
services.
Here are the reasons why not to choose Spring Data REST
Performance – Since Spring Data REST generates endpoints dynamically, the
performance may not be optimal for very large-scale applications.
Versioning – It can be difficult to version the REST APIs exposed by Spring Data REST.
Relationships – Handling relationships between entities can be tricky with Spring Data
REST.
Filtering – There are limited options for filtering the results returned by the endpoints.
For more information, Please Refer to this article- Spring – REST Controller
48. What is Data JPA?
Spring Data JPA is a module that provides a high-level abstraction over the Java
Persistence API (JPA). Spring Data JPA makes it easy to develop applications that interact
with relational databases.
JPA is a specification that defines the Java standard for Object-Relational Mapping (ORM).
It provides an API for mapping Java objects to relational database tables and performing
CRUD (Create, Read, Update, Delete) operations using object-oriented concepts.
49. How is Hibernate chosen as the default implementation for JPA without
any configuration?
Spring Boot automatically configures Hibernate as the default JPA implementation when
you add the spring-boot-starter-data-jpa dependency to your project. This dependency
includes the Hibernate JAR file as well as the Spring Boot auto-configuration for JPA.
You can override the default Hibernate configuration by providing your own JPA properties
or Hibernate properties in your [Link] file. But by default, Hibernate is
chosen and configured for you.
For More Details, Refer to These articles
Hibernate Architecture
Java – JPA vs Hibernate
50. Can you explain how to deploy to a different server with Spring Boot?
Here are the steps on how to deploy to a different server with Spring Boot:
Build your Spring Boot application. You can do this by running the mvn clean install
command.
Create a deployment package. The deployment package will depend on the type of
server you are deploying to. For example, if you are deploying to a Tomcat server, you will
need to create a WAR file.
Deploy the deployment package to the server. The specific steps for deploying the
deployment package will depend on the type of server you are deploying to. For example,
if you are deploying to a Tomcat server, you can deploy the WAR file by copying it to the
webapps directory of the Tomcat server.
Start the server. Once the deployment package has been deployed, you can start the
server.
In the interview, candidates with over 5 years of experience are primarily questioned
about these concepts.
There are a few things you can do to prepare for Spring Boot interview questions:
For candidates with up to 2 years of experience, interviews will typically focus on the core
concepts of Spring Boot, such as auto-configuration, starters, actuator, and CLI.
Questions may also be asked about how to create, configure, run, and deploy Spring
Boot applications.-
In the interview, candidates with over 3 years of experience are primarily questioned
about these concepts.
Company
About Us
Legal
Careers
In Media
Contact Us
Advertise with us
Explore
Job-A-Thon Hiring Challenge
Hack-A-Thon
DSA in JAVA/C++
Master CP
GeeksforGeeks Videos
Geeks Community
Languages
Python
Java
C++
PHP
GoLang
SQL
R Language
Android Tutorial
DSA
Data Structures
Algorithms
DSA Roadmap
Competitive Programming
ML Maths
Pandas Tutorial
NumPy Tutorial
NLP Tutorial
Web Technologies
HTML
CSS
CSS
JavaScript
TypeScript
ReactJS
NextJS
NodeJs
Bootstrap
Tailwind CSS
Python
Django Tutorial
Python Projects
Python Tkinter
Web Scraping
Computer Science
GATE CS Notes
Operating Systems
Computer Network
Software Engineering
Engineering Maths
DevOps
Git
AWS
Docker
Kubernetes
Kubernetes
Azure
GCP
DevOps Roadmap
System Design
UML Diagrams
Interview Guide
Design Patterns
OOAD
Interview Questions
School Subjects
Mathematics
Physics
Chemistry
Biology
Social Science
English Grammar
Commerce
Accountancy
Business Studies
Economics
Management
HR Management
Finance
Income Tax
UPSC Study Material
Polity Notes
Geography Notes
History Notes
Economy Notes
Ethics Notes
Preparation Corner
Resume Templates
Aptitude Preparation
Puzzles
Company-Wise Preparation
Companies
Colleges
Competitive Exams
JEE Advanced
UGC NET
SSC CGL
SBI PO
SBI Clerk
IBPS PO
IBPS Clerk
More Tutorials
Software Development
Software Testing
Product Management
Project Management
Linux
Excel
Image Editor
Code Formatters
Code Converters
Currency Converter
Improve an Article
Internships