Sunday, June 2, 2024

Top 20 Spring Boot Interview Questions with Answers for Java Developers

Hello Java developers, there is no doubt that the Spring Boot is now the standard way to develop Java application using Spring Framework, and that's why Spring Boot Questions are increasingly becoming popular on Java interviews. In the past, I have shared many Spring MVC interview questions in this blog like the @RestController vs. @Controller, but I haven't got a chance to share any questions on Spring Boot, Spring Cloud, and Microservices. After a couple of personal requests from my readers, I thought to write about, and here comes my list of top 20 Spring Boot interview questions for Java developers.

In this list, you will tackle all-important Spring Boot concepts from an interview point of view like auto-configuration, starter dependencies, Spring Boot CLI, and Actuator. Once you have gone through this list, you should have sufficient knowledge to tackle any Spring Boot question on the real interviews.

Arguably, Spring Boot is the most significant and exciting thing that happens in Java development for a long time, I guess, after the release of Java 5. The main advantage of using Spring Boot is that it greatly simplifies the use of the Spring framework for Java development.

It addresses most of the concerns a new developer or a team has about using a framework like configurations, dependencies, setup hurdles, etc. It's the kind of application development framework Java programmers have been looking for a long time, and that's why it's very important from Java and Spring point of view.

Btw, If you are preparing for Spring interview you can take a look at them and if you still need more questions you can also take help from books like Grokking the Spring Boot Interview and this 200+ Spring interview questions course on Udemy which is probably the biggest list of Spring interview questions I have come across.





20 Spring Boot Interview Questions with Answer

In this article, I am going to share some of the frequently asked questions from the Spring Boot framework, covering essential concepts like starter dependencies, auto-configuration, command-line interfaces, and Actuator in detail.

This is the third list of Spring interview questions on Javarevisited. Earlier I have shared general Spring Framework interview questions, and more recently, I have written about some advanced Spring MVC and REST interview questions for Java developers.

The article is divided into 5 sections, the first section includes general Spring Boot interview questions like Comparision with Core Spring and Spring MVC.

The second section focuses on the auto-configuration feature of Spring boot which removes most of the configuration required to work with the Spring framework.

The third section focuses on Spring Boot's starter dependencies which consolidate common dependencies into one for ease of management.

The fourth section focuses on interview questions about Spring Boot's Actuator, which provides insights about running Spring boot application, a very useful tool in production.

Fifth, and the last section deals with Spring Boot CLI interview questions, which offers an attractive option for developing Spring Boot application using Groovy.


1. Spring Boot Common Interview Questions

The main purpose of Spring Boot is to simplify the use of the Spring framework for Java development. It introduces a suite of features that address the pain associated with using the Spring framework for Java development as a lot of configuration, several JAR files, and a difficult startup curve. 

Now, let's see common Spring Boot questions from this topic:


1. What is the Spring Boot?
Spring Boot is nothing but a Java framework that aims to simplify the use of the Spring framework for Java development. It introduces a slew of features like auto-configuration, embedded servlet container, starter dependencies, Actuator, and Spring Boot CLI to make Java web development with Spring MVC even easier.


2. What is the advantage of using Spring Boot?
There are several advantages of using Spring Boot the most important one is convenience and productivity. Spring Boot application can be run as a normal Java application like inside the main method. It also comes with starter dependency which means instead of explicitly loading all common libraries, you can load one starter and it will automatically import others.

The Spring CLI features make writing Spring Boot application even easier using Groovy. You can even write a working Spring Boot application in less than 140 characters, something which you can tweet. You can further check out Learn Spring Boot in the 100 Steps course on Udemy to learn more about what Spring Boot offers in terms of efficient configuration and development.

Top 20 Spring Boot Interview Questions with Answers for Java Developers



3. What is the difference between Spring Boot and Spring MVC?
Even though both are part of the bigger Spring framework umbrella they are two different frameworks and solve different problems. Spring MVC makes Java web development easier by providing a consistent structure using the Model View Controller design pattern.

On the other hand, Spring Boot aims to simplify the use of Spring Framework including Spring MVC for Java development by adding some painful areas with respect to configuration, dependency management, and running the application.

For example, you need a web-server to run a Spring MVC application but Spring Boot comes with embedded Tomcat or Jetty which can be used to run your Java web application.


4. What is the difference between Core Spring and Spring Boot?
Again, they are two different frameworks but comes under the same umbrella of the Spring framework. Core Spring generally refers to Spring container which provides dependency injection and inversion of control, a key feature of Spring framework which promoted writing Java application in a different way 15 years back. While Spring Boot is now doing the same for the Spring framework, it is promoting a new way to use the Spring framework in the Java project.


2. Spring Boot Auto-Configuration Interview Questions

One of the important features of Spring Boot is the auto-configuration, which makes it possible for the Spring framework to intelligently detect what kind of application you're building and automatically configure the components necessary to support the application's need.

It's built upon the conditional configuration feature introduced in Spring 4 which determines which configuration would be used and which will be ignored at runtime based upon dependencies on application's classpath, environment, and other factors, but with Spring Boot, you don't even need to write the explicit configuration for common scenarios.


5. What is Spring Boot auto-configuration?
Spring Boot auto-configuration is a runtime process that considers several factors to decide what Spring configuration should and should not be applied at the application startup-time. Some of its work may should magical to you.

For example, if Spring's JdbcTemplate is available on classpath and if there is a DataSource bean then it can auto-configure a JdbcTemplateBean.

Similarly, if Spring MVC is on the classpath, Spring's DisapatcherServlet will be configured and Spring MVC will be enabled.


6. How does auto-configuration work? How does it know what to configure?
As explains in the previous example, Spring Boot auto-configuration checks classpath and makes decisions like if a Thymeleaf is present on the classpath then it can configure a Thymelead template resolver, view resolver, and a template engine automatically.

If Spring Data JPA is on the classpath then it can automatically create repository implementations from repository interfaces.  Similarly, if Spring Security is present on the classpath then it can configure a very basic web security setup.

It is seriously powerful as it takes more than 200 such decisions ever time an application starts up. If you need more examples, you can check out Spring Boot in Action book from Craig Walls, the best book to learn Spring Boot framework.

Best book to learn Spring boot for beginners



7. What are some common Spring Boot annotations? (answer)
Some of the common Spring Boot annotations are @EnableAutoConfiguration, which enables auto-configuration and @SpringBootApplication which combines @Configuration, @ComponentScan, and @EnableAutoConfiguration into one to provide Java configuration, component scanning and enable Spring Boot's auto-configuration features. Both annotations are usually placed in the main class.


8. What does @EnableAutoConfiguration annotation do? (answer)
The @EnableAutoConfiguration annotation enables auto-configuration in the Spring Boot project. This one line of code literally saves you from writing pages of configurations that would be required otherwise.


3. Spring Boot Starter Dependency Interview Questions

This is another interesting feature of Spring Boot which makes it easier to select which build and runtime dependencies your application will need by combining commonly needed dependencies. With Spring Boot starter dependency you can just include one dependency and rest assured that it will other common dependencies without worrying about the compatible version.


9. What is starter dependency in Spring Boot? What is the advantage of it?
The starter dependency feature of Spring Boot solves the problem of dependency management in your project. For example, if you want to build a Java web application using JPA and Thymeleaf template what are the dependencies you will need?

You not only need to figure out a list of dependencies but also their compatible versions. This can be painful and risky if you don't have much experience. Spring Boot solves this problem by introducing "Starter".

Instead of manually adding a library or specifying in Maven or Gradle build file you specify Starter. Each Starter aggregates common and related dependencies like to develop a Spring MVC based application instead of adding all JAR files you can just specify spring-boot-starter-web starter in your Maven or Gradle build file and it will import Spring MVC Jar and commonly used library like Jackson.

Internally, Starters are nothing but Maven POM that defines transitive dependencies on other libraries that work together to provide some common functionality.

Btw, If you want to learn more about starter dependency then I suggest you check out Spring Boot Fundamentals by Kesha Williams on Pluralsight.

Spring Boot Starter Dependency Interview Questions



10. How do you define properties in Spring Boot? Where?
You can define properties in the application.properties file. Spring Boot automatically loads this file and its properties can be used to configure both application and Spring boot. 

For example, if you want to change the default port of embedded tomcat server in Spring Boot then you can add server.port = 9000 in this file as shown in this example. Also, when you use Spring Boot Initializer to create your project structure, it also creates this file but empty


11. What does @SpringBootApplication annotation do?
This is the main Spring Boot annotation which enables Spring's component-scanning and auto-configuration feature of Spring Boot. Every Spring Boot application which is not using Spring CLI needs this annotation. Internally, @SpringBootApplication is a combination of three annotations:

Spring's @Configuration which enables Spring's Java-based configuration.

Spring's ComponentScan which enables component scanning so that web controller classes and other components you write are automatically discovered and registers as spring beans in ApplicaitonContext.

Spring Boot's @EnableAutoConfiguration which enables the auto-configuration feature of Spring Boot.

The @SpringBootApplication annotation was introduced in Spring Boot 1. 2, which means in older versions you need to annotate your main class with all of these three annotations.

If you want to learn more about this annotation I suggest you check Master Microservices with Spring Boot and Spring Cloud course by Ranga Karnam on Udemy.

Spring Boot Auto-Configuration Interview Questions



12. What things affect what Spring Boot sets up?
Spring Boot's magic depends upon multiple things e.g. annotations, application's classpath, environment, etc. Spring CLI can automatically add packages to Groovy's default package if they are present in the application classpath. Similarly, you can enable auto-configuration by using @SpringBootApplication or @EnableAutoConfiguration annotations.


4. Spring Boot Actuator Interview Questions

This is probably the most important feature of a Spring Java application running on production. It provides insights into the inner working of a running Java application which is really important given so much automation. If you really need to know which beans are configured and how Spring MVC controllers are mapped to request path then this tool can help you.


13. What is the Spring Boot Actuator?
It's part of the Spring Boot application that provides deep insights about a running Spring boot application like beans in the Spring application context, auto-configuration decisions, Spring MVC mappings, thread activity, and various applications health metrics.


14. What are the different ways Actuator provides to gain insight into a Spring Boot application?
Spring Boot Actuator provides a couple of ways to retrieve insights and other metrics like it provides REST endpoints, which means you can access metrics over HTTP. It also provides a remote shell to execute a command inside the Spring Boot application and expose much of the same data as the Actuator's endpoints. 

Also, all of the Actuator's endpoints are exposed as JMX MBeans, which means you can monitor and manage them in runtime using a JMX client like the jConsole.


15. Why you need to secure Spring Boot Actuator's endpoints?
You need to secure the Spring Boot Actuator's endpoints because it exposes a lot of information from running the Spring Boot application which can be considered sensitive. Some of Spring Boot endpoints like /shutdown are also dangerous as a rogue user can shut down the production instance resulting in loss of money and reputation.


16. How do you secure the Spring Boot Actuator's endpoint to restrict access?
Just like Spring MVC application, you can secure the Spring Boot Actuator's endpoints using Spring Security. You can add a Security starter as a build dependency and then security auto-configuration will take care of locking the application, including the Actuator endpoints.

Though, you also need to do a bit of customization like setting up the right roles, username, password, etc. Btw, if you are not familiar with Spring Security then you can take a look at the Learn Spring Security Masterclass by Eugen Paraschiv to learn more about it.

Spring Boot Interview Questions for Beginners




5. Spring Boot CLI Interview Questions

Spring Boot command line really makes it easy to develop Java web applications using Groovy. It takes advantage of Groovy's feature to remove the ceremony associated with writing Java code. With The Spring CLI, there is no need for accessor methods, access modifiers such as public, private, semicolons, or return keyword. In some cases, you can even get rid of import statements.


17. What is Spring Boot CLI? (answer)
It's a command-line interface provided by Spring Boot which makes it easy to write Spring Boot application using Groovy. It leverages the simplicity and convenience offered by auto-configuration and starter dependency and makes it even easier to write Spring boot application by taking Groovy's power to remove a lot of boiler-plate code from Java. In order to use Spring Boot CLI, you need to install it on your machine.


18. Can you write Spring application with Groovy?
Yes, Spring Boot CLI makes it possible to write Spring Boot application in Groovy. It also cut down a lot of noise which comes with writing code in Java.


19. What are the main advantages of the Spring Boot command-line interface (CLI)?
Apart from the convenience of writing Spring application with Groovy, Spring Boot CLI offers the following advantages:

It can leverage Spring Boot auto-configuration and starter dependencies.
It can detect when certain types are in use and automatically resolve the appropriate dependencies to support those types, though, not all.

CLI has information about which packages some common types belong like JdbcTempalte and common Spring annotations like @Controller. If those types are used, it can automatically add those packages to Groovy's default packages at compile time. You can further see


20. What does @Grab annotation do? When to use this?
The @Grab annotation is used to import types which Spring boot CLI cannot resolve automatically. It actually extends @Grab annotation from Gradle, a JAR dependency manager into Groovy, so that, for many frequently used library you can import them by just adding module ID like you can just write @Grab("h2") instead of @Grab("com.h2database:h2:1.4.185) to import H2 in-memory database and Spring CLI will take care of version, etc.


21. What is Spring Initializer?
This is a bonus Spring Boot interview questions as we have already covered 20 questions. Well, the Spring Initializer is a web application that can generate Spring Boot project structure for you. Sometimes creating a project starter is the most difficult thing, especially if you are new to Spring development and Spring Initializer takes care of that. It doesn't generate any code but gives you basic project structure with Maven or Gradle build specification to build your code.


22. What is the difference between @SpringBootApplication and @EnableAutoConfiguration in Spring Boot?  (answer)
The main difference is that @SpringBootApplicaiton annotation is relatively newer and only available from the Spring Boot 1.2 version while @EnableAutoConfiguration is present from the start. Also, @EnableAutoConfiguration just enables the auto-configuration feature of the Spring Boot application while @SpringBootApplication does three things.

It not only enables auto-configuration but also enables Component scanning and allows you to run your application inside embedded tomcat. You can see the answer article for a more detailed explanation.


That's all about some frequently asked Spring Boot interview Questions from Java web developer interviews. As I said, Spring Boot simplifies working with the Spring framework by its suite of features and a good knowledge of Spring Boot is essential for Java developer. It will not only help you to land a job but also become more productive in your current job.

Almost all new Java development that is using the Spring framework is using Spring Boot. If you want to learn Spring Boot better you can check out the following resources

Further Learning
Learn Spring Boot Rapidly
Master Java Web Services with Spring Boot
Spring Boot: Efficient Development, Configuration, and Deployment


Other Java and Spring Articles you may like
  • 15 Spring Boot Interview Questions for Java Developers (questions)
  • Top 5 Courses to Learn and Master Spring Cloud (courses)
  • 5 Free Courses to Learn Spring Framework in depth (free courses)
  • 5 Courses to Learn Spring Security in depth (courses)
  • Top 5 Spring Boot Annotations Java Developers should know (read)
  • @SpringBootApplication vs @EnableAutoConfiguration? (answer)
  • 5 Spring Books Experienced Java Developer Should Read in depth (books)
  • Top 5 Frameworks Java Developer Should Know (frameworks)
  • 7 Best Spring Courses for Beginners and Experienced (courses)
  • 10 Spring MVC annotations Java developer should learn (annotations)
  • Top 5 Spring Cloud annotations Java programmer should learn (cloud)
  • 5 Courses to learn Spring Cloud in depth (courses)
  • 10 Advanced Spring Boot Courses for Java developers (courses)
  • Top 7 Courses to learn Microservices in Java (courses)
  • 10 Free Courses to learn Spring Framework for Beginners (free courses)
  • 10 Free Courses to learn Spring Boot in-depth (free courses)

Thanks for reading this article so far. If you like these Spring Boot interview questions then please share with your friends and colleagues. If you have any doubts or feedback then please drop a note.

P. S. - If you want to learn Spring 5 and Spring Boot 2 from scratch, in a guided, code-focused way then I suggest you take a look at the Learn Spring: The Master Class course by Eugen Paraschive of Baeldung.

4 comments :

geoaxis said...

Thank you for article, it has few spelling mistakes, It's not Spring Boto (but Spring Boot)

javin paul said...

Hello geoaxis, thanks for point out, fixed now.

Daniel Lopez said...

Very helpful post :)

Anonymous said...

Thank you for sharing these Spring boot questions, can you please also share Spring boot questions for experienced developers? like 5 to 10 years of experience with Spring boot?

Post a Comment