Spring Boot Interview Questions List in 2020

Today, we will go through our comprehensive list of Spring Boot interview questions, which are typical to be asked in an interview.

Current article is part of our series on Spring Interview Questions.

Keep in mind that Interviewer is not just interested in your knowledge on Sprint Boot but also on how you can use other Spring projects with a Spring Boot project.

So, once you are gone through the Spring Boot Interview Questions, it’s better to go through questions on other Spring projects too, which suits your profile.

Now, let’s start with the subject of our current article and that is:

spring boot interview questions and answers

Spring Boot Interview Questions And Answers

1. What is the difference between Spring and Spring Boot?

While Spring is the common name used to represent a number of projects…

Spring boot is one of those projects.

spring vs spring boot

2. What is Spring Boot?

Spring boot is one of the Spring projects which help developers in creating the Java enterprise applications, in easier and faster way (Rapid Application Development).

With the help of Spring Boot, we can create a complete skeleton of production-ready Java application, within minutes.

3. What are some of the Spring Boot advantages?

Well…

  1. One thing is, it helps in Rapid Application Development (RAD).
  2. Secondly, it makes the developer’s life easy by providing different pre-built features which helps in minimizing configuration.
  3. Thirdly, it provides us monitoring, health checks and security features to make it a production-ready application.

4. What are Spring Boot disadvantages?

  1. As a developer, we have to go through a bit of learning curve.
  2. For an organisation, it will increase the project cost as it might require finding developers who knows Spring Boot or up-skill existing resources.
  3. Our project’s configuration need might be in frequent conflict with the opinionated configuration which Spring Boot provides.

5. Which Spring Boot version you used in your project?

Say any version like 2.3.4. BUT, you should know the minimum requirements and minimal dependencies for the same.

6. Is there any minimum requirement for using this version of Spring Boot?

YES. We require:

  1. JDK 8+ version.
  2. Spring framework’s 5.2.9 + version.

7. Which build systems are well supported by Spring Boot?

Both Maven and Gradle are well supported but use of Ant is not recommended by the Spring Boot team.

8. What is Bills of Materials (BOM)? Ever heard about it?

Bills of Materials is the well curated list of Spring Boot’s dependencies with which it works great.

With this, we do not need to check if one particular version of a library works with our version of Spring Boot or not.

Also, all the transitive dependencies and conflict are taken care of.

9. What are different Spring Boot features which makes it special?

There is the list of major features of Spring Boot:

  1. Spring Boot Starter Projects
  2. Auto-Configuration
  3. Profiling
  4. Auto configured Logging
  5. Auto configured i18n
  6. Spring Actuator
  7. Spring Initializer
  8. Spring CLI

10. What are Spring Boot Starter Projects?

In a big and complex projects, as the time goes, it gets very difficult to manage the dependencies.

Following major problem might arise:

  1. Compatibility tracking: We need to have complete track of: which jar version works with which jar version.
  2. Dependency tracking: If we want to use a jar, we also need the information of all other jar it is dependent on.
  3. Conflict Issues: Many a times, A jar requires X version of a jar, while B jar requires Y version of same jar. If not tracked properly, it will create issues while resolving the dependencies. Transitive dependencies might also create issues.

Here comes the role of Spring Starter Projects.

For example: if we are creating a web project, we will declare the spring-boot-starter-web in our pom.xml and that’s it.

Now, it’s Spring Boot’s starter project’s ressponsibility:

  1. To resolve all the dependencies it requires.
  2. To manage different version of jars of it’s dependencies.
  3. Because of the above, conflict issues does not arise automatically. A positive side-effect.

11. List some Spring Boot Starter Projects.

Here are some of the popular starter projects:

  1. spring-boot-starter-web
  2. spring-boot-starter-security
  3. spring-boot-starter-actuator
  4. spring-boot-starter-aop
  5. spring-boot-starter-batch
  6. spring-boot-starter-data-jdbc
  7. spring-boot-starter-data-jpa
  8. spring-boot-starter-logging

For a complete list of starter project visit this link: Spring Boot Starter Projects.

12. Can we create our custom Spring Boot starter project?

YES, we can. In-fact, big companies does that to extend the benefits of Spring starter projects.

We just need to take care of the recommended way of naming them.

For example: Custom starter projects should not start with spring-boot-starter. Instead, it should start with our project name and then followed by spring-boot-starter like this. ourproject-spring-boot-starter

Also, the artifact group name should be different.

13. What is Spring Boot auto-configuration feature?

Note: This is one of the most important feature of Spring Boot and hence one of the favorite Spring Boot Interview Questions.

Spring Boot’s auto-configuration feature helps in:

Minimizing the configuration needed in the project.

How does Spring Boot knows which configuration to bring into the project?

Well…

It attempts to add the configuration on a best guess basis, according to the list of jars it finds in our project dependencies.

14. How can we disable a specific auto-configuration?

We can do so:

  1. Either by exclude attribute of @SpringBootApplication annotation.
  2. or by exclude attribute of @EnableAutoConfiguration annotation.
  3. or adding the entry of spring.autoconfigure.exclude in application.properties.

Here are the code snippets for above:

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

We can also use excludeName attribute if the class file is not present in our classpath.

15. How to enable a custom auto configuration?

We can add the entry of our configuration class in spring.factories file with key EnableAutoConfiguration

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.baeldung.autoconfigure.CustomAutoConfiguration

The file can be found under META-INF folder.

16. What if we want Spring to apply custom configuration, if it exists, instead of auto configuration?

We can do this with the help of @ConditionalOnMissingBean annotation.

@Configuration
public class CustomConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public AppComponent appComponent() {
        //
    }
}

17. What are different ways to create a Spring Boot application?

Note: All the Spring Boot Interview Questions on application creations are straightforward. A candidate should be well versed in it.

  1. By Spring Initializer tool
  2. By Spring Boot CLI
  3. With the help of Spring Starter project Wizard
  4. As a Simple Maven project

18. How to create a Spring Boot application with Spring Initializer tool?

It is very easy to create a Spring Boot application with the help of Spring Initializer tool.

This tool is provided by Spring community and is available at https://start.spring.io/.

creating spring boot project with spring initializer tool

As you can see, we just need to provide the project requirements and this tool will deliver the project with those configuration and dependencies added.

19. What is Spring Boot CLI tool?

CLI stands for Command Line Interface.

Spring Boot CLI is a tool which helps developer through different phases of an application: From creation, to packaging, running etc.

For example, to initialize the project, we just need to trigger the following command on CLI:

$ spring init --dependencies=web,data-jpa my-project

Behind the scenes, for initializing a Spring Boot application, CLI uses Spring’s initializer’s web service.

This tool is provided by Spring community. Learn more about this tool from Spring CLI docs.

20. How to create Spring Boot application with Spring starter project wizard?

Eclipse IDE and it’s variations provide us Spring starter project wizard to create Spring Boot application.

So, with New >> Spring Starter Project, IDE opens a wizard where we need to provide the configuration information, like we do in Spring Initialization tool, and that’s it.

IDE will create the project ready with the mentioned configurations.

21. How to create a Spring Boot application as a Maven project?

A typical way in which we can configuring a Maven POM for a Spring Boot project is:

  1. Declare spring-boot-starter-parent project as parent project.
  2. Add starter dependencies according to your project requirements.
  3. Addition of project properties and dependencies of non-Spring boot jars.

This is how it might look:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>your.project.groupId</groupId>
    <artifactId>yourProjectArtifactId</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
    </parent>

    <properties>
        <java.version>12</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security.oauth.boot</groupId>
            <artifactId>spring-security-oauth2-autoconfigure</artifactId>
            <version>2.1.8.RELEASE</version>
        </dependency>
    </dependencies>

</project>

22. What is the recommended way to write a Spring Boot app?

Although Spring Boot does not force any project structure on us to follow BUT certain opinions are well recommended.

For example:

  1. Use Maven or Gradle as your build system.
  2. Do not put any code in the “default” package.
  3. Put the MainApplication in the root package.
  4. Give priority to Java Configuration over XML.
  5. Do not create one big configuration java class. Instead, create small configuration files and then assemble them.

23. How does Spring Boot helps achieve Rapid Application Development?

To create an application we require typically :

  1. Configuration metadata for our application objects.
  2. Configuration of third-party framework like Hibernate.
  3. Initialization of container to consume this metadata.
  4. Setting of web.xml for initial configuration.
  5. Enabling and setting of Spring MVC.
  6. Configuration related to Spring MVC and so on…

So many configuration just to start with. What if we get a basic structure automatically from the framework?

This is what Spring Boot does.

It creates an application in an opinionated way, that is: taking configuration decision on its own, on our behalf.

Then, we can customize the configuration if required.

This interview question on Spring Boot comes as a cross question when you say that Spring Boot is used for RAD. So, be ready with this answer.

24. How can we change the port of embedded Tomcat in Spring Boot?

We can provide the port number in application.properties file with key server.port

For example:

server.port=8085

One of the most frequently asked Spring Boot Interview Questions.

25. How to enable support for HTTP/2 in a Spring Boot application?

It can be done by adding an entry in application.properties file like this:

server.http2.enabled=true

26. What are Spring and Java requirements for Spring Boot 2.0?

We require a minimum of Java 8 and Spring 5 for Spring Boot 2.0

27. Can we create a non-web application in Spring Boot?

We require a minimum of Java 8 and Spring 5 for Spring Boot 2.0

28. Can we use any other embedded server than Tomact? If yes, then how?

By default, Spring Boot gives us Tomcat as embedded server.

We can change it by changing the dependency configuration of web-starter project as:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

After reading this POM, server will be changed to Jetty.

29. Where can I find the complete list of properties that can be configured in application.properties?

Refer Spring official page for complete set of properties.

30. What is Actuator in Spring Boot?

When we say that Spring Boot provides us a production-ready application, Actuator is one of those features which makes an application production-ready.

Actuator module provides support for monitoring and management of an application.

With the help of Actuator, we can gather different health check metrics of an application.

Actuator is used quite regularly in Spring-Boot projects. So, interviewer expects from you to know this. Which makes this and other questions related to Actuator, very important Spring Boot interview questions.

31. How to enable Spring Boot Actuator?

To enable it, we just need to make an entry of actuator starter project in pom dependency list like this:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

32. What are Spring Boot Actuator Endpoints?

An Endpoint is a component by which we collect all the matrices cum data, related to an application.

It lets you interact with the application to fetch the data and thus, helps in monitoring.

These endpoints are exposed via. HTTP or JMX, to fetch the data from remote.

33. What are some of the most commonly used Actuator Endpoints?

EndPoint IDDescription
/threaddumpCreates a thread dump.
/healthShows the data related to an application’s overall health.
/cachesShows available caches.
/beansDisplay complete list of all the beans in your application.
/metricsShows common metrics of an application.

For a complete list of endpoints, visit Actuator Endpoints by default.

34. What is application availability?

Application availability is the application feature by which an application sends signals related to it’s availability, to the outside world.

These signals are used by the deployment infrastructure like Docker Swarm or Kubernetes to take the decision whether to restart the application or not.

35. How does Spring boot supports application availability?

Spring boot sends two states related to it:

  1. Liveness State: It tells if internal state of an application is OK, or recovering or not recoverable.
  2. Readiness State: If tells is application is able to handle the traffic or not.

36. List out some of the common Spring Boot annotations?

  1. @SpringBootApplication
  2. @EnableAutoConfiguration
  3. Conditional: @Conditional, @ConditionalOnBean, @ConditionalOnClass, @ConditionalOnWebApplication
  4. @AutoConfigureBefore, @AutoConfigureAfter, @AutoConfigureOrder

37. How to package a Spring Boot application as either executable jar or deployable war file?

We can take the help of spring-boot-maven-plugin to achieve both the tasks.

For executable jar:

First, we will add the entry for this plugin in pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

and then declare the packaging type as jar in pom.xml

<packaging>jar</packaging>

jar is the default packaging type of this plugin. So, if we do not declare any, plugin will package it as jar.

For war:

First, we will change the packaging type to be war, instead of jar.

<packaging>war</packaging>

Secondly, we will set the scope of embedded server as provided.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

38. Explain relaxed binding in Spring Boot?

Before this, we had to define the keys in specific format. For example:

server.port=8085

We can not write port in other ways like PORT, Port

Relaxed Binding is the ability of the application to interpret the keys in the property files, in different formats namely camelCase, UPPER-CASE, kebab-case and under_score.

Examples:

some.key-format=keyValue //kebab-case: existing format
some.keyFormat=keyValue //camelCase
some.KEY-FORMAT=keyValue //UPPER-CASE
some.key_format=keyValue //under_score

39. How to enable HTTPS/SSL support in Spring Boot?

It can be done in two steps:

  1. Add the SSL specific entries in the application.properties file.
  2. Get and put the certifications on the paths declared in application.properties file.
# Spring Security entries
security.require-ssl=true

# SSL entries
server.port=443
server.ssl.key-store=classpath:cert.p12 (or classpath:cert.jks)
server.ssl.key-store-password=certPrivateKey
server.ssl.keyStoreType=PKCS12(or JKS)

Certification can be generated by either JDK’s keytool tool or any thirdparty tool.

40. What happens when a Spring Boot application is executed as “Run as Java Application”?

By seeing a web application, it

  1. First, it automatically scans the web server (tomcat or jetty or undertow) in pom.xml
  2. Secondly, it launches the web server.
  3. Thirdly, it the application with it. Because of which, application will be accessible through browser.

41. What is @SpringBootApplication annotation?

It is a meta annotation combining @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan.

42. What is @SpringBootConfiguration annotation?

Right now, it is same as @Configuration annotation which is used to support Java based container configuration.

43. What is @EnableAutoConfiguration annotation?

@EnableAutoConfiguration tells Spring Boot to enable it’s support for autoconfiguration feature.

With this, it will add necessary configuration to your application in an opinionated way.

The decision of which all configurations to add is done by scanning the dependencies on your classpath.

Questions on auto-configuration are very important Spring Boot interview questions as it is Spring Boot’s main feature.

44. What is the configuration needed to enable Spring Boot AOP?

  1. First, we need to add “spring-boot-starter-aop” starter project in our dependency list.
  2. Secondly, we need to enable or disable AopAutoConfiguration key in application.properties file. For example: spring.aop.auto=false.

45. How to run your Spring Boot Jar file?

We do so by java -jar command like this:

java -jar build/libs/myapplication.jar

46. What is Spring Boot hot swapping?

Hot swapping is a generic feature where our code changes is reflected on-the fly.

In the context of Spring Boot, spring-boot-devtools does that by restarting the application or by live reloads.

47. What is JRebel? Ever heard about it?

Note: You can say NO here or say I know it but haven’t used it in my project(s).

JRebel is a third-party development tool. It is superior to spring-boot-devtools, in terms of performance and other things.

Spring boot team does recommend it, in case Java application development process is complex.

48. How to debug Spring boot startup failures?

  1. Most of the startup failures are displayed by Spring boot logs out of the box.
  2. If still something is not working and not getting displayed: Execute the jar with –debug switch. For example: java -jar myApp.jar –debug

49. Are Spring boot beans eager or lazy initialized?

Be default, spring boot beans are eager initialized. Although, it increases app start-up time, but we can be sure that all the dependencies are well initialized and injected.

50. How to enable lazy initialization if we want to?

Add property and mark it true.

spring.main.lazy-initialization=true

With the java configuration: call setLazyInitialization of SpringApplication.

51. But these are global settings. How to mark a particular bean as lazy initialized?

Annotate the bean class with @Lazy(false).

52. What is Spring boot’s fluent builder API?

Fluent builder API of spring boot is based upon fluent builder pattern.

In this, we build an object by chaining different methods calling on each other.

For example:

new SomeClass()
        .method1(methodargs)
        .method2(methodargs)
        .method3(methodargs)
        .build();

53. What are various Spring boot lifecycle events?

Spring boot provides us some supporting events. At these points, we can execute some logic accordingly.

For example:

  1. ApplicationStartingEvent
  2. ApplicationEnvironmentPreparedEvent
  3. ApplicationContextInitializedEvents
  4. ApplicationPreparedEvent
  5. ApplicationStartedEvent
  6. AvailabilityChangeEvent
  7. ApplicationReadyEvent
  8. ApplicationFailedEvent

54. How to get application arguments inside a bean, without passing in method?

We can do so by injecting the ApplicationArguments instance into the bean. This class has all the methods to read the arguments.

@Component
public class MyBean {

    @Autowired
    public MyBean(ApplicationArguments args) {
        boolean debug = args.containsOption("key");
        List<String> files = args.getNonOptionArgs();
    }
}

55. What is CommandLineRunner?

CommandLineRunner is used to execute some logic before SpringApplication.run() finishes.

@Component
public class MyBean implements CommandLineRunner {

    public void run(String... args) {
       //CommandLineRunner logic
    }

Our main Spring boot application’s arguments are passed as run method arguments.

56. What is ApplicationRunner?

ApplicationRunner is same as CommandLineRunner. The only difference is that in ApplicationRunner, the main application’s arguments are injected via. ApplicationArguments instance.

@Component
public class MyBean implements CommandLineRunner {

    public void run(String... args) {
       //CommandLineRunner logic
    }

Our main Spring boot application’s arguments are passed as run method arguments.

57. How to enable Spring Boot admin features?

We can do that by adding the property spring.application.admin.enabled.

spring.application.admin.enabled=true

What to read next?

Recommended:

OR Checkout our series on Spring Interview Questions to pick any topic on Spring, which suits your CV.

Best of Luck!!

Leave a Comment