Spring Boot Interview Questions
Last updated on Nov 21, 2023
Spring Boot is a spring module that provides the Rapid Application Development features for spring framework. It is used in creating the standard alone spring-based application which you can run with little spring configuration.
In this article, you can go through the set of spring boot interview questions most frequently asked in the interview panel. This will help you crack the interview as the topmost industry experts curate these at HKR training.
Let us have a quick review of the spring boot interview questions.
Most Frequently Asked Spring Boot Interview Questions
- What are the advantages of Spring Boot
- What are the Spring Boot starters and what are available starters
- What is the need for Spring Boot Dev Tools
- What are the steps to connect an external database like MySQL or Oracle
- How is Hibernate chosen as the default implementation for JPA without any configuration
- What is the difference between WAR and embedded containers
- What is meant by Spring Boot
- Define IOC Container
- How to validate an Actuator within the Spring Boot application
- Name the layer at which the Transaction boundary begins in Spring Boot
What are the advantages of Spring Boot?
The advantages are.
- Creating the standard alone applications which can be started using java -jar.
- The ability to embed Tomcat, Jetty or Undertow directly without any need for deployment of WAR files.
- It provides opinionated 'starter' POMs to simplify your Maven configuration.
- It configures the spring automatically whenever it is possible.
Name the Spring Boot features.
The features of Spring Boot are.
- Web development.
- Spring Application.
- Application events and listeners.
- Admin features.
What is Spring Boot dependency management?
The dependency management of Spring Boot automatically manages the configurations and dependencies. It is not required to specify a version for any of these dependencies. By upgrading the Spring Boot the dependencies of Spring Boot also upgrades automatically.
Become a Spring Boot Certified professional by learning this HKR Spring Boot Training
What is the difference between Spring vs Spring Boot?
- Spring is a web application framework based on java.
- The customized web applications are created using the tools and libraries that are provided by the Spring framework.
- Spring is more complex than Spring Boot.
- It takes opinionated view.
Spring Boot:
- Spring Boot is a module of a spring.
- Spring Boot is used in creating a Spring application project which can just run/execute.
- Spring Boot is less complex than the Spring framework.
- It takes an opinionated view of a platform.
Become a Expert in Spring Boot with HKR. Join now Spring Boot Microservices Training!
What are the Spring Boot starters and what are available starters?
The Spring Boot Starters are the set of convenient dependency management providers which is used in the application for enabling the dependencies. These starters make the easy and rapid deployment. All the available starters will come under the “org.springframework.boot” group. Few of the popular starters are as follows:
- spring-boot-starter: It is the core starter which includes logging, auto-configuration support, and YAML.
- spring-boot-starter-jdbc: This starter is used for HikariCP connection pool with JDBC.
- spring-boot-starter-web: This starter is used for building web applications. It also includes RESTful and applications that use Spring MVC.
- spring-boot-starter-data-jpa: It Is the starter that is used for Spring Data JPA with Hibernate
- spring-boot-starter-security: It is the starter used for Spring Security.
- spring-boot-starter-aop: This starter is used for aspect-oriented programming with AspectJ and Spring AOP.
- spring-boot-starter-test: It is the starter used in testing Spring Boot applications.
Explain how to create a Spring Boot application using Maven.
There are various approaches to create a Spring Boot project using Maven. These are the few approaches that are followed in creating a Spring Boot application using Maven.
- Spring Boot CLI.
- Spring Starter Project Wizard.
- Spring Initializr.
- Spring Maven Project.
Can you explain what happens in the background when a spring boot application is “Run as Java Application”?
The tomcat server is launched automatically when the spring boot is executed as “Run as Java Application” while developing a web application.
Mention the steps to create a Spring Boot project using Spring Initializr.
Spring Initializr is a web tool of Spring which helps in creating Spring Boot projects by providing the project details. The creating steps are as follows.
- Choose the maven project and its dependency requirements. Then, fill the details like Group, Artifact and then click on Generate Project.
- Extract the project once it is downloaded in your system.
- With the “import” option try importing the project under “Spring Tool Suite IDE”. Then choose the project type as “Maven” and source project file that contains pom.xml file.
With these steps, you can see the Spring Boot project is created with all required dependencies.
What is thymeleaf and how is it used?
Thymeleaf is a server-side java template engine used for web applications. The main purpose of thymeleaf is to bring natural templates for web applications which integrate well with Spring Framework and HTML5 java web applications. For using thymeleaf the following code must be added in the pom.xml file.
. What is the need for Spring Boot Dev Tools?
Spring Boot Dev Tools are the set of tools which make the application development easier. When the application runs in the production than this module is disabled automatically, repacking of archives are also excluded by default. The Spring Bot developer tools apply the properties to the specific development environments. Add the following dependency into the pom.xml file for including DevTools.
org.springframework.boot
spring-boot-devtools
. What is the Spring Boot Actuator?
Spring Boot provides the actuator to monitor and manage our application. Actuator contains the HTTP endpoints. When the application is pushed to production then by using HTTP endpoints you can choose to manage and monitor your application.
. How do you create a custom endpoint in Spring Boot Actuator?
In Spring Boot 2.x, the custom endpoint is created using “@Endpoint” annotation. The Spring Boot also exposes the endpoints using @WebEndpointor, @WebEndpointExtension annotations over HTTP with the help of Spring MVC, Jersey, etc.
. Can you change the port of the embedded Tomcat server in Spring Boot?
Yes, the port of the embedded Tomcat server can be changed by using the application properties file. You have to add the property “server.port” in the application properties file and assign it to any port which you want. After assigning the port number, the Spring Boot automatically loads the application properties file and the required configurations will be applied on the application.
[ Related Article: microservices training ]
. Explain how to deploy a different server with a Spring Boot?
Apply the below steps to deploy a different server with Spring Boot.
- Generate a WAR file from the project.
- Then, deploy the WAR file onto your favourite server.
The steps of deploying the WAR file onto a server is dependant on the server which you choose.
. What are the steps to connect an external database like MySQL or Oracle?
Apply the below steps for connecting an external database.
- Start adding a dependency for MySQL Connector to pom.xml.
- Then, in the pom.xml file remove the H2 dependency.
- Now set up your MYSQL and configure your connection to MySQL database.
- Restart your project.
. What is the difference between RequestMapping and GetMapping?
The @GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET). The @RequestMapping and @GetMapping annotations both of them will support the consume options which are.
consumes = “text/plain”
consumes = {“text/plain”, “application/*”}
. What are the steps to add a custom JS code with Spring Boot?
The following are the steps for adding a custom JS code using Spring Boot.
- In the resources folder, create a folder and name it as static.
- Now keep all the static content in this folder.
If in case a browser throws an unauthorised error, then disable the security or search for a password in the log file and eventually it will be passed in the request header.
. What are the differences between @SpringBootApplication and @EnableAutoConfiguration annotation?
@SpringBootApplication:
- The @SpringBootApplication annotation is used in the main class or bootstrap class.
- It is a combination of @Configuration, @ComponentScan and @EnableAutoConfiguration annotations.
@EnableAutoConfiguration:
- The @EnableAutoConfiguration annotation is used to enable auto-configuration and component scanning in your project.
- It is a combination of @Configuration and @ComponentScan annotations
. Can you create a non-web application in SpringBoot?
A non-web application can be created by removing the web dependencies from the classpath along with changing the way Spring Boot creates the application context.
. How is Hibernate chosen as the default implementation for JPA without any configuration?
By using the Spring Boot Auto Configuration the spring-boot-starter-data-jpa dependency is automatically added to pom.xml file. As this dependency has a transitive dependency on JPA and Hibernate, whenever spring boot sees Hibernate in the classpath, it auto-configures Hibernate as the default implementation for JPA.
Spring Boot Training
- Master Your Craft
- Lifetime LMS & Faculty Access
- 24/7 online expert support
- Real-world & Project Based Learning
. What do you think of using jetty instead of tomcat in spring-boot-starter-web?
You can use jetty instead of tomcat in spring-boot-starter-web, by removing the existing dependency and including the following:
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
org.springframework.boot
spring-boot-starter-jetty
. Mention the dependencies needed to start up a JPA Application and connect to in-memory database H2 with Spring Boot?
The following are the dependencies required to start up a JPA Application and connecting in-memory database H2 with Spring Boot.
- web starter
- h2
- data JPA starter
You must refer to the following code for including the dependencies.
org.springframework.boot
spring-boot-starter-web
com.h2database
h2
runtime
org.springframework.boot
spring-boot-starter-data-jpa
. Why is Spring Data REST not recommended in real-world applications?
As the database entities are exposed directly as REST Services, it is not recommended in real-world applications. The domain model and the consumers are the two main things which are considered while designing the RESTful services. But, none of these parameters are considered while using Spring Data REST. The entities will be exposed directly. The Spring Data REST can be used for the initial evolution of the project.
. How to instruct an auto-configuration to back off when a bean exists?
The @ConditionalOnMissingBean annotation is used for instructing an auto-configuration class to back off when a bean exists. The annotation attributes are.
value: It stores the type of beans to be checked.
name: It stores the name of beans to be checked.
. What is the difference between WAR and embedded containers?
WAR is benefited for a considerable measure from Spring Boot while the embedded containers use only one component of Spring Boot for utilization during the improvements.
. What are the steps to deploy Spring Boot web applications as JAR and WAR files?
1.Add the following plugin in the pom.xml for deploying the Spring Boot application.
2.With the use of the above plugin, execute its package which generates a JAR file. This JAR contains all the required libraries and dependencies. This also contains an embedded server which can run the application as an ordinary JAR file.
To build a jar file, the packaging element in the pom.xml file must be set to jar as below.
To build a war file, you must mention
. What is the best way to expose the custom application configuration with Spring Boot?
By using the “@value” annotation you are able to expose the custom application configuration in Spring Boot but the configuration values are distributed through the application. If this is one way than another way is to use a centralized approach instead of using @value annotation.
With a centralized approach, the configuration component is defined using the “@ConfigurationProperties” as follows:
@Component
@ConfigurationProperties("example")
public class SampleConfiguration {
private int number;
private boolean value;
private String message;
With the above snippet, the configured values of application properties will be as follows.
example.number: 100
example.value: true
example.message: Dynamic Message
. What is the difference between JPA and Hibernate?
JPA is a Data Access Abstraction used in reducing the amount of boilerplate code while Hibernate is an implementation of the Java Persistence API that offers the loose coupling benefits.
. What are the @RequestMapping and @RestController annotation in Spring Boot used for?
@RequestMapping:
- This annotation provides the routing information and tells to Spring that any HTTP request must be mapped to the respective method
- The “@RequestMapping” annotation is used by importing.
org.springframework.web.
bind.annotation.RequestMapping;
@RestController:
- This annotation adds the “@ResponseBody” and “@Controller” annotation to the class.
- The “@RestController” annotation is used by importing.
org.springframework.web.
bind.annotation.RestController;
Example: Consider there is a method example() which should map with /example URL.
package com.hkrtrainings;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SampleController {
@RequestMapping("/example")
public String example(){
return"Welcome To HKRTrainings";
. Mention the advantages of the YAML file than Properties file and the different ways to load YAML file in Spring boot.
- The advantage of the YAML file than Properties file is that the data is stored in a hierarchical format which makes it easy for developers to debug the issues. Whenever you use the SnakeYAML library on your classpath, the YAML file is supported as an alternative to properties by the SpringApplication class.
- The different ways to load a YAML file in Spring Boot is as follows:
For loading YAML as a Map, the “YamlMapFactoryBean” is used.
For loading YAML as Properties, the “YamlPropertiesFactoryBean” is used.
. What is meant by Spring Boot?
Ans. Spring Boot is a Java-based open-source spring module useful for Rapid Application Development (RAD). It helps to develop many stand-alone spring-based apps which we can run with a little configuration. Also, it simplifies the usage of the Spring framework for the development of Java.
Subscribe to our YouTube channel to get new updates..!
. Define Spring Boot Annotations?
Ans. The Spring Boot Annotations represent metadata that offers data regarding a program. It provides additional data about a program that doesn’t include the part of an application we develop. Moreover, it doesn’t directly impact the operation of the code they explain.
. What do you mean by Spring Initializr?
Ans. In Spring Boot, Spring Initailizr is a web tool provided by Spring that helps produce the Spring Boot project structure. It supports us while we start a new project from scratch by resolving issues of framework setting. Thus, there is no need to set up the project structure manually.
. Define Spring Boot CLI?
Ans. Spring Boot CLI refers to the command-line interface which Spring Boot Framework supports. It enables you to build Spring-based Java apps and also helps to execute and test Spring Boot apps using the command prompt.
. Explain Spring Boot Properties?
Ans. Spring Boot offers multiple properties where the file application.properties helps write the application-based property into that file. Moreover, these property files hold various configurations that require the app to run in a distinct environment. The significant Spring Boot application properties' types include command line properties, YAML files, properties files, and externalized properties.
. Define Spring Data in Spring Boot?
Ans. Spring Data makes it easier for developers to use different types of databases like relational and non-relational, cloud data services, etc. Thus, it makes data access more accessible and still retains the underlying data.
. What is meant by Dependency Injection?
Ans. In Spring Boot, dependency injection is a design pattern that Spring container uses to insert objects to other dependencies. Also, it helps to eliminate the dependency on the various programs.
. Why use Spring Boot rather than Spring?
Ans. Spring Boot has vital features that Spring doesn’t have. Key features like auto-configuration save much time for developers from drafting long-form codes. It also makes the application development process much more manageable. Further, some other vital features that Spring Boot offers include Starter POM, Actuators, Scanning Components, Version Management, InMemory database, etc.
. What is Relaxed Binding in Spring Boot, and how does it support it?
Ans. Spring Boot uses some relaxed binding rules for binding various Environment Properties to map with the bean property name. It maps with the property name even if there is no exact match. Relaxed binding is highly relevant for the type-safe binding of configuration properties within Spring Boot.
. Define IOC Container?
Ans. IOC container is a robust Spring framework useful for applying automatic dependency injection. It helps in managing the creation of objects and their lifetime. Also, it helps to insert dependencies into the class. The IoC container inserts the dependency objects using a constructor, a method, or a property at the runtime and ejects the same at a relevant time.
. How database connection information automatically connects to H2 in Spring Boot?
Ans. The database connection information in Spring Boot is specified and automatically connected to H2 using the auto-configuration property. This property is a crucial feature that configures all the application dependencies. By leveraging this auto-configuration feature, developers are relieved from manually specifying the database connection information in their code.
The auto-configuration property in Spring Boot simplifies the process by automatically handling the database connection details and seamlessly connecting to the H2 database. This eliminates the need for developers to manually configure the connection properties, saving time and effort.
. What is the default H2 database that Spring Boot configures?
Ans. The default H2 database that Spring Boot configures is called 'testdb.' This is the name of the database configured in the Spring Boot configuration file. It is important to note that if you are using an in-memory H2 database, the name 'testdb' is specifically set up by Spring Boot for your convenience. This default configuration allows you to quickly and easily access and interact with the H2 database in your Spring Boot application.
. Why are Profiles used in Spring Boot?
Ans. Profiles are an essential feature in Spring Boot that allows us to configure different aspects of our application based on the environment. They help us separate various portions of our application configuration and make it convenient to adapt to different environments. With profiles, we can specify the active profiles for a particular environment. This means we can have different configuration settings for development, production, or any other environment we may have. Spring Boot provides us with the flexibility to define multiple profiles and their corresponding configurations. To use profiles, we need to create separate configuration files for each profile. For example, if we have a "dev" profile, we can create a file named "application-dev.properties" to override and customize the properties specific to the development environment. Similarly, for a "prod" profile, we can create a file named "application-prod.properties" to configure the production environment.
. How can we disable the default web server within the Spring Boot application?
Ans. Spring Boot mostly prefers to use the tomcat server, but we can prevent Spring Boot from starting a server automatically. For this, we can avoid including the web server starter within our dependencies.
. How to validate an Actuator within the Spring Boot application?
Ans. To validate or authorize an Actuator in the Spring Boot app, we have to insert the spring-boot-starter-actuator dependency within pom.xml.
. Define the way to check the environment properties within the Spring Boot Application?
Ans. To check all the environment properties in the Spring Boot application, we need to enable the Actuator endpoint “/env.” It returns all the properties that run the Spring Boot app.
. What are the sources of External Configuration in Spring Boot?
Ans. Spring Boot enables developers to execute similar applications in multiple environments. Here, the external configuration supports them to do so. The below sources are the possible ones for external config in Spring Boot.-
- Command-line Properties
- Application Properties
- Profile-specific Property files
. Define the process of custom auto-configuration registration in Spring Boot?
Ans. The @EnableAutoConfiguration annotation is essential for registering a custom auto-configuration class in Spring Boot. By adding this annotation along with the META-INF/Spring.factory file key, developers can enable automatic configuration of various aspects of their application based on the dependencies present in the classpath. This annotation works in conjunction with other annotations such as @Configuration and @ComponentScan.
On the other hand, the @SpringBootApplication annotation is a convenient combination of multiple annotations. It not only allows for auto-configuration but also enables component scanning and specifies the main configuration class for the application. By using @SpringBootApplication, developers can streamline the setup process by consolidating the functionalities of @Configuration, @ComponentScan, and @EnableAutoConfiguration into a single annotation.
. Name the most useful Spring Boot CLI commands?
Ans. The following are the most commonly used Spring Boot CLI Commands.:-
-run, -jar, -install, --init, -shell, -help, -test, -grap, -uninstall, -war, etc.
Related Article : Spring Data ElasticSearch
. Name the layer at which the Transaction boundary begins in Spring Boot?
Ans. In Spring Boot, the boundary or border of the transaction begins from the Service Layer. This means that the Service Layer is responsible for defining the transaction boundaries and ensuring the proper management of transactions. It is in this layer that the logic for the business transactions resides, making it a crucial component in the overall transaction management process. By starting the transaction boundary from the Service Layer, developers can encapsulate the necessary business logic within a single layer, promoting modularity and separation of concerns. This approach allows for easier maintenance and scalability of the codebase, as well as providing a clear separation between the transactional and non-transactional parts of the application.
. Define the way of configuring Logging using Log4j?
Ans. To configure Log4j for logging in a Spring Boot project, it is recommended to include Log4j2 as it is supported by Spring Boot. This can be achieved by adding the necessary dependencies for Log4j2 in the project's configuration. By including Log4j2, the logging configuration can be customized according to the project's requirements.
Upcoming Spring Boot Training Online classes
Batch starts on 21st Nov 2024 |
|
||
Batch starts on 25th Nov 2024 |
|
||
Batch starts on 29th Nov 2024 |
|