Top 10 Spring Boot Interview Questions and Answers
Prepare for your next Spring Boot interview with these common interview questions.
Join the DZone community and get the full member experience.
Join For FreeIn this article, we will discuss some top 10 interview questions in Spring Boot. These questions are a bit tricky and trending heavily, nowadays, in the job market.
1) What does the @SpringBootApplication annotation do internally?
As per the Spring Boot doc, the @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes. Spring Boot enables the developer to use a single annotation instead of using multiple. But, as we know, Spring provided loosely coupled features that we can use for each individual annotation as per our project needs.
2) How to exclude any package without using the basePackages
filter?
There are different ways you can filter any package. But Spring Boot provides a trickier option for achieving this without touching the component scan. You can use the exclude
attribute while using the annotation @SpringBootApplication
. See the following code snippet:
@SpringBootApplication(exclude= {Employee.class})
public class FooAppConfiguration {}
3) How to disable a specific auto-configuration class?
You can use the exclude
attribute of@EnableAutoConfiguration
, if you find any specific auto-configuration classes that you do not want are being applied.
//By using "exclude"
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
On the other foot, if the class is not on the classpath, you can use the excludeName
attribute of the annotation and specify the fully qualified name instead.
//By using "excludeName"
@EnableAutoConfiguration(excludeName={Foo.class})
Also, Spring Boot provides the facility to control the list of auto-configuration classes to exclude by using the spring.autoconfigure.exclude
property. You can add into the application.properties. And you can add multiple classes with comma separated.
//By using property file
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
4) What is Spring Actuator? What are its advantages?
This is one of the most common interview questions in Spring Boot. As per the Spring doc:
"An actuator is a manufacturing term that refers to a mechanical device for moving or controlling something. Actuators can generate a large amount of motion from a small change."
As we know, Spring Boot provides lots of auto-configuration features that help developers quickly develop production components. But if you think about debugging and how to debug, if something goes wrong, we always need to analyze the logs and dig through the data flow of our application to check to see what's going on. So, the Spring Actuator provides easy access to those kinds of features. It provides many features, i.e. what beans are created, the mapping in the controller, the CPU usage, etc. Automatically gathering and auditing health and metrics can then be applied to your application.
It provides a very easy way to access the few production-ready REST endpoints and fetch all kinds of information from the web. But by using these endpoints, you can do many things to see here the endpoint docs. There is no need to worry about security; if Spring Security is present, then these endpoints are secured by default using Spring Security’s content-negotiation strategy. Or else, we can configure custom security by the help of RequestMatcher
.
5) How to enable/disable the Actuator?
Enabling/disabling the actuator is easy; the simplest way is to enable features to add the dependency (Maven/Gradle) to the spring-boot-starter-actuator, i.e. Starter. If you don't want the actuator to be enabled, then don't add the dependency.
Maven dependency:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
6) What is the Spring Initializer?
This may not be a difficult question, but the interviewer always checks the subject knowledge of the candidate. It's often that you can't always expect questions that you have prepared. However, this is a very common question asked almost all of the time.
The Spring Initializer is a web application that generates a Spring Boot project with everything you need to start it quickly. As always, we need a good skeleton of the project; it helps you to create a project structure/skeleton properly. You can learn more about the Initializer here.
7) What is a shutdown in the actuator?
Shutdown is an endpoint that allows the application to be gracefully shutdown. This feature is not enabled by default. You can enable this by using management.endpoint.shutdown.enabled=true in your application.properties file. But be careful about this if you are using this.
8) Is this possible to change the port of Embedded Tomcat server in Spring boot?
Yes, it's possible to change the port. You can use the application.properties file to change the port. But you need to mention "server.port" (i.e. server.port=8081). Make sure you have application.properties in your project classpath; REST Spring framework will take care of the rest. If you mention server.port=0 , then it will automatically assign any available port.
9) Can we override or replace the Embedded Tomcat server in Spring Boot?
Yes, we can replace the Embedded Tomcat with any other servers by using the Starter dependencies. You can use spring-boot-starter-jetty
or spring-boot-starter-undertow
as a dependency for each project as you need.
10) Can we disable the default web server in the Spring Boot application?
The major strong point in Spring is to provide flexibility to build your application loosely coupled. Spring provides features to disable the web server in a quick configuration. Yes, we can use the application.properties to configure the web application type, i.e. spring.main.web-application-type=none
.
All the best!
Published at DZone with permission of Manoj Kumar Bardhan. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments