Top 40+ Spring Boot Interview Questions & Answers 2021
Are you going to appear for a java developer job interview? If yes, then you might get some questions on spring boot fired on you in the interview. Thus you should sharpen your knowledge about Spring boot before you encounter the interviewer.
This article is for you. In this article, I have formulated many spring boot questions and answers that would help you ace your next interview.
All you need to do is have a look at these questions and their answers, and you will be all set to appear for the interview. So without further ado, let’s start with the very basic question on Spring Boot.
Spring Boot Interview Questions with Answers 2021
Here is the most asked Spring Boot Interview Questions with Answers list of 2021.
1. What is the Spring Boot?
Spring Boot is a Spring Module that helps to create a stand-alone spring application project. This project could only be executed—the objective behind using Spring to facilitate Java Development by simplifying Spring Framework. Spring boot framework provides the developer with a variety of tools like embedded HTTP servers management endpoints, auto-configuration, auto-dependency resolution, Spring Boot CLI, etc. Basically, Spring Boot helps in rapid Java or application development.
2. What is Spring?
Spring is a web application framework that is based on Java. This framework provides developers with various tools and libraries to facilitate the web application development process. It is easy to use Spring.
3. Why should anyone use Spring Boot?
Spring boot provides developers with a lot of reasons to use it, some of the major advantages of using Spring Boot are as follows-
- Helps in creating stand-alone applications with a wide variety of non-functional features. These non-functional features are common to large classes of projects.
- Spring Boot’s auto-configuration helps in loading a set of default configurations. This helps in starting the applications quickly.
- CLI to test and develop applications.
- No need to deploy WAR files.
- Your Maven configurations are simplified by opinionated ‘starter’ POMs.
4. What are the ways to create a Spring Boot application with Maven?
A spring Boot application could be created in a variety of ways. Some of them are listed below-
- Spring Starter Project Wizard
- Spring Initializr
- Spring Maven Project
- Spring Boot CLI
5. What would happen in the background if you run a spring boot application as “Run as Java Application”?
Tomcat server is executed when you are developing a web application and executing the spring boot application as “Run as Java Application”.
6. Define Spring Boot starters?
To enable dependencies in the application, you could make use of spring boot starters that are a set of convenient dependency management provider. Some of the starters in Spring boot are listed below –
- spring-boot-starter
- spring-boot-starter-aop
- spring-boot-starter-test
- spring-boot-starter-web
- spring-boot-starter-JDBC
- spring-boot-starter-security
- spring-boot-starter-data-JPA
7. What is the spring actuator?
Spring actuator in Spring Boot allows us to see what happens inside a running application.
8. Why is the spring actuator used?
Spring actuator can be used for a variety of purposes. With this feature in Spring Boot, you can analyze logs while debugging, identifying beans, CPU usage, etc. It helps in accessing the production-ready REST points.
9. What is the spring boot starter?
Out of many starters in spring boot, spring boot starter is the principal starter that incorporates logging, auto-configuration support, and YAML.
10. Why is spring-boot-starter-JDBC used?
This yet another one of the starters in spring boot utilized for the HikariCP connection pool with JDBC.
11. Why is spring-boot-starter-web used?
This starter helps in developing a web application using Spring MVC. It can also be used to build a RESTful application using spring MVC.
12. Why we use spring-boot-starter-data-JPA?
This starter in spring boot allows us to utilize Spring Data JPA with Hibernate.
13. What is Spring CLI?
Spring boot CLI can be used to build spring boot applications and avoid boilerplate code.
14. What is a starter dependency feature in spring boot?
All the common dependencies are assembled together to improve productivity. This could be achieved by the starter dependency feature in the spring boot.
15. What is spring’sSpring’s initializer?
Spring initializer is a web application capable of generating internal project structure. When you use this feature, you don’t need to set up the internal project structure by yourself.
16. What is an auto-configuration in Spring boot?
Usage of not required WAR files can be avoided by loading the default configurations based on the project you are working on. This is what auto-configuration in Spring Boot does.
17. How to change the port of the embedded Tomcat server?
This could be achieved easily by making use of the application properties file. All you need to do is add a property of “server.port” and then assign the property to any port.
18. What is the way to enable HTTP/2 support in Spring Boot?
In order to enable HTTP/2 support in Spring Boot, we can set the following:
server.http2.enabled=true
19. What is Hibernate?
The advantage of loose coupling cam is used by Hibernate which is an implementation of the Java Persistence API.
20. What is JPA?
A Data Access Abstraction that could decrease the amount of boiler code.
21. What is the Spring data?
Spring data enables developers to access data. It enables us using relational and non-relational databases, cloud-based data services, etc.
22. How to the disable auto-configuration in Spring boot?
The auto-configuration feature can be disabled by excluding the attribute of @EnableAutoConfiguration. Spring.autoconfigure.exclude property can also be used to exclude the list of auto-configuration classes.
23. How can we deploy Spring Boot web applications as JAR and WAR files?
To deploy Spring boot web application as JAR and WAR files, you need to add this plugin in pom.xml file:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
This plugin will get you a JAR containing all libraries and dependencies to execute necessary for executing package phase.
For WAR file, use the following-
<packaging>war</packaging>
24. How can you deploy to a distinct server with Spring boot?
For doing so, you can generate a WAR file from your project and then deploy it onto your chosen server.
25. What is relax binding support in Spring Boot?
When the property name is not required to match the key of the environment property, it is called relax binding. Spring boot supports relax binding.
26. Explain Spring boot CLI and its advantages?
It is a command-line interface allowing developers to create Spring applications with groovy.
27. Where are the properties in Spring Boot applications defined?
The properties in Spring boot applications are defined in an application.properties file. Spring initializer can create this file or alternatively, you can create it manually.
28. What error will you see if H2 is not in the classpath?
The following error will be shown in the case, h2 is not in the classpath:
Cannot determine embedded database driver class for database type NONE
29. What is a profile in Spring Boot?
A profile is simply a key used to identify an environment.
30. Differentiate between the embedded container and a WAR.
You don’t need a webserver to execute Spring boot application as a JAR file, and you can do this from the command prompt. In order to run a WAR file, you would need to connect with a tomcat server and deploy the WAR file there.
31. List the embedded containers that Spring boot support?
All of the embedded containers that Spring Boot support are-
- Tomcat
- Jetty
- Undertow
Thus Spring boot supports am aggregate of 3 embedded containers.
32. List some common Spring Boot annotations.
Some of the common Spring Boot annotations are-
- @SpringBootApplication
- @SpringBootConfiguration
- @EnableAutoConfiguration
- @SpringBootTest.
33. How would you control logging with Spring Boot?
It is possible to control logging with Spring Boot. To accomplish that, we would need to specify the log levels of the application.properties file.
34. What dependencies are needed in order to start up a JPA Application and connect to in-memory database H2 with Spring Boot?
The dependencies require are as follows:
- web starter
- h2
- data JPA starter
35. How will you notify an auto-configuration to back off when a bean exists?
By using @ConditionalOnMissingBean annotation, we can notify auto-configuration to back off when a beam exists.
36. What are the attributes of @ConditionalOnMissingBean annotation?
The attributes of @ConditionalOnMissingBean annotation are value and name.
37. How will you add a custom JS code with Spring Boot?
This could be achieved by following the below-given steps:
- Create a new folder with the name static under the resources folder.
- Include the static content in this folder.
38. The boundary of a transaction should start with which layer, according to you?
The boundary of a transaction should start with Service Layer as Spring Layer includes the logic for business transactions.
39. Explain Spring Data REST?
To expose RESTful resources around Spring Data repositories, Spring data REST will be utilized. The post method could be used to expose the REST service.
40. Why is the YAML file better than the properties file?
In the YAML file, the data is presented in an ordered fashion or hierarchal manner, which makes debugging easier when developers encounter errors.
41. Is creating a non-web application in Spring Boot possible?
Yes, it is possible to create non-web applications in Spring Boot. For doing so, we would need to remove web dependencies from the classpath and change the way Spring boot creates an application context.
42. Have you ever heard of thymeleaf? If yes, then elaborate.
Thymeleaf is nothing but a server-side Java template engine that aims to provide natural template for your web applications. The following code can be used to integrate it with spring framework:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Conclusion
These were some frequently asked and popular Spring Boot interview questions and their answers. Spring boot is a decent framework for application development is widely used. As a java developer, you should have a basic understanding of Spring boot, its features, and related processes.
If you don’t have that required knowledge, I hope reading this article profoundly would help. These questions contain a lot of Spring boot concepts, so revising them on the go would further help.
Once you finish reading these questions and their answers, you will have a better understanding of the spring Boot framework and you would be ready to ace your next job interview. Thanks for reading and I hope it helps.