Reading Time: 5 minutes

Most Important Spring Boot Interview Questions

Spring Boot Interview Questions: Spring has become undoubtedly the most famous framework among Java developers. Spring comes with a lot of configurations and to solve this issue, Spring boot came into the picture. Nowadays most developers opt for spring boot while creating a spring application.

Today, we have compiled a list of Most Important Spring Boot Interview Questions. This list will cover questions from most basic to advance level.

Most Asked Spring Boot Interview Questions

What is Spring Boot?

Spring Boot is a Spring framework module that provides Rapid Application Development (RAD) to the Spring framework with the help of starter templates and auto-configuration features. It aims to simplify the use of the Spring framework for Java development.

It is used to create stand-alone Spring-based applications that you can just run. So, it basically removes a lot of configurations and dependencies.

What is the need for Spring Boot?

Spring Boot takes an opinionated view of the Spring Framework and third-party libraries imported into the project and configures the behavior for us. It means that as soon as we include any dependency on the application, spring boot assumes its general purpose and automatically configures the most used classes of the library as spring beans with sensible defaults.

For example, if we are creating a web MVC application then including a maven dependency; spring-boot-starter-web brings all jars/libraries used for building a web application, including RESTful applications using Spring webMVC.

It also provides a range of non-functional features such as embedded servers, security, metrics, health checks, and externalized configuration out of the box without extra configurations.

How is Spring Boot different from Spring Framework?

Spring Boot is basically an extension of the Spring framework which eliminates the boilerplate configurations required for setting up a working production-ready application.

All Spring Annotations ASAP Cheat Sheet [Core+Boot][2019]

What are the advantages of Spring Boot?

  • It provides simplified & version conflict-free dependency management through the starter POMs and opinionated auto-configuration of the most commonly used libraries and behaviors.
  • The embedded jar enables us to package the web applications as jar files which can be run anywhere.
  • Its actuator module provides HTTP endpoints to access application internals like detailed metrics, application inner working, health status, etc.
  • It comes with embedded tomcat, servlet, and container jetty to avoid the usage of WAR files.
  • It gives CLI (Command Line Interface) tools to create and test Spring Boot (Java or Groovy) applications from commanding prompt very easily and rapidly.
  • It integrates with Spring Ecosystem like Spring JDBC, Spring ORM, Spring Data, Spring Security easily by avoiding boilerplate code.

What are the disadvantages of Spring Boot?

  • Many developers may see the transitive dependencies included with starter poms as a burden to deployment packaging.
  • During auto-configuration, it might enable many such features that might never get used in the application lifecycle and thus unnecessarily utilize resources.
  • It is difficult to convert the existing Spring project into Spring boot.

How Spring Boot supports “Run as Java Application” feature?

When a Spring Boot application is executed as “Run as Java application”, then it automatically launches up the tomcat server as soon as it detects deployment of a web application.

What are the Spring Boot starters?

Spring Boot starters are a set of convenient dependency management providers that can be used in the application to enable transitive dependencies. These starters make development easy and rapid.

For example, if we want to create a Spring WebMVC application then in a traditional setup, we would have included all required dependencies ourselves. It leaves the chances of a version conflict which ultimately results in more runtime exceptions.

With Spring Boot, to create a WebMVC application, all we need to import is spring-boot-starter-web dependency. Transitively, it brings in all other required dependencies to build a web application e.g. spring-webmvc, spring-web, hibernate-validator, tomcat-embed-core, tomcat-embed-el, tomcat-embed-websocket, jackson-databind, jackson-datatype-jdk8, jackson-datatype-jsr310, and jackson-module-parameter-names.

What are available the Spring Boot Starters?

All the available starters come under the org.springframework.boot group.

  • spring-boot-starter: – This is the core starter and includes logging, auto-configuration support, and YAML.
  • spring-boot-starter-web – Is the starter for building web applications, including RESTful, applications using Spring MVC.
  • spring-boot-starter-jdbc – This starter is used for HikariCP connection pool with JDBC.
  • spring-boot-starter-data-jpa – Is the starter to use Spring Data JPA with Hibernate
  • spring-boot-starter-security – 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: Is the starter for testing Spring Boot applications

What is Spring Boot dependency management?

Spring Boot dependency management is basically used to manage transitive dependencies from spring boot starters and automatically configuring them without specifying the version for any of the dependencies.

What is the Transitive Dependency Resolution Management used in Spring Boot?

Transitive Dependency Resolution Management means fetching the dependencies that are required to resolve the dependency in the build script (like POM).
For example, if we define an “A” dependency in the build scripts, “A” is dependent on “B” and “B” is dependent on “C”, That means “A” is also dependent on “C”.
Then Build Tools (like Maven) will download and add all Three Jar files “A”, “B” and “C” to our application classpath automatically.

Explain Spring Actuator and its advantages.

Spring boot’s actuator module allows us to monitor and manage application usages in a production environment, without coding and configuration for any of them. These monitoring and management information are exposed via REST-like endpoint URLs.
Spring Boot includes a number of built-in endpoints and lets us add our own. Further, each individual endpoint can be enabled or disabled as well.

What are some of the common Spring Actuator’s Endpoint?

ENDPOINT USAGE
/env Returns list of properties in the current environment
/health Returns application health information.
/auditevents Returns all auto-configuration candidates and the reason why they ‘were’ or ‘were not’ applied.
/beans Returns a complete list of all the Spring beans in your application.
/trace Returns trace logs (by default the last 100 HTTP requests).
/dump It performs a thread dump.
/metrics It shows several useful metrics information like JVM memory used, system CPU usage, open files, and much more.

How can we change the port of the embedded Tomcat server in Spring boot?

We can change the port of the embedded tomcat server by using the application properties file. We have to add a property of “server.port” and assign it to any port you wish to.

For example, if you want to assign it to 8081, then you have to mention server.port=8081.

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

Yes, we can create a non-web application by removing the web dependencies from the classpath along with changing the way Spring Boot creates the application context.

What Is Spring Boot Configuration File Called?

The main configuration file in spring boot is application.properties. This file must be present in the resources folder. application.properties file is used to override any of spring boot’s default configuration such as changing tomcat server port, disabling auto-configuration of a bean or database configuration.

What is auto-configuration in Spring Boot?

Auto-configuration is used to automatically configure the required configuration for the application. For example, if you have a data source bean present in the classpath of the application, then it automatically configures the JDBC template.

With the help of auto-configuration, you can create a Java application in an easy way, as it automatically configures the required beans, controllers, etc.

How to disable the auto-configuration?

To disable the auto-configuration property, you have to exclude the attribute of @EnableAutoConfiguration, in the scenario where you do not want it to be applied.

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
If the class is not on the classpath, then to exclude the auto-configuration, you have to mention the following code:

@EnableAutoConfiguration(excludeName={Sample.class})

Apart from this, Spring Boot also provides the facility to exclude a list of auto-configuration classes by using the spring.autoconfigure.exclude property. You can go forward and add it either in the application.properties or add multiple classes with comma-separated.

Where do we write static content in Spring Boot?

Spring boot will automatically map /src/main/resources/static as static resource folder. To add css or js, we can create subdirectories like /src/main/resources/static/css/ and /src/main/resources/static/js.

What is Spring Boot’s DevTools used for Auto-reload?

Spring Boot comes with a tool that aims to ease development. DevTools auto-reload/LiveReload the spring boot application whenever any change is saved in any of the source files. This saves developer efforts to build and restart the server to see the changes.
To enable DevTools, add spring-boot-devtools dependency in the pom.xml file.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true
</dependency>

 

We will keep adding more Spring Boot Interview Questions.

References

https://www.edureka.co/blog/interview-questions/spring-boot-interview-questions/

https://howtodoinjava.com/interview-questions/spring-boot-interview-questions/

https://www.onlineinterviewquestions.com/spring-boot-interview-questions/