Top Spring Boot Interview Questions (2021) | CodeUsingJava








Top Spring Boot Interview Questions (2021)


  1. What is Spring Boot?
  2. What are the features of spring boot?
  3. Spot the difference between spring and spring boot?
  4. How does spring boot work?
  5. Using Maven, explain the way to create a Spring Boot application ?
  6. What are starter dependencies?
  7. How would you describe the HTTPS request through the Spring Boot application?
  8. Spot the differences between spring boot and spring MVC?
  9. How do you enable debugging log in the spring boot application?
  10. What are the Steps to deploy spring boot web applications as JAR and WAR files?
  11. Can we create non web application in spring boot?
  12. Explain thymeleaf and and the way to use it?
  13. What is dependency Injection?

What is Spring Boot?

  • Spring consists of a number of modules. Previously for each module the dependencies needed to be imported seperately. So for example Spring AOP needs multiple dependencies. All these dependencies needed to be imported individually in the pom or gradle file. With different versions of these dependencies there used to be many conflicts which needed to be resolved and could cause issues. Spring Boot is a project that is built on the top of the Spring Framework. It provides an easier and faster way to set up, configure, and run both simple and web-based applications. With Spring Boot we just need to provide the Spring AOP starter dependency. With this all the dependencies related to AOP with their correct versions are automatically downloaded by spring.


What is the use of @SpringBootApplication annotation?

@SpringBootApplication annotation was introduced in Spring Boot 1.2.0. Using this annotation enables the auto-configuration feature
This annotation enables the following 3 annotation -
  • @Configuration - Registering the beans
  • @ComponentScan - Component scanning to discover the controller and component classes automatically.
  • @EnableAutoConfiguration - Enable the Spring Boot autoconfiguration


What is Spring Actuator? How to make use of it?

Actuator is used to bring production-ready features to our application. It is responsible for monitoring app, gathering metrics, understanding traffic, or the state of the database.
It provides secured endpoints for monitoring and managing your Spring Boot application. By default, all actuator endpoints are secured.
When using Maven Framework include the following dependency
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
When using Gradle Framework include the following dependency
compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'


What is Spring Security? How to enable Spring Security?

Spring Security is a powerful and highly customizable authentication and access-control framework. It is the de-facto standard for securing Spring-based applications.
Spring Security is a framework that focuses on providing both authentication and authorization to Java applications. Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements
If Spring Security is on the classpath, Spring Boot automatically secures all HTTP endpoints with "basic" authentication. However, you can further customize the security settings. The first thing you need to do is add Spring Security to the classpath. When using Maven Framework include the following dependency
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
</dependency>
When using Gradle Framework include the following dependency
compile group: 'org.springframework.boot', name: 'spring-boot-starter-security'

How to start Spring Boot Application on a port other than the default port 8080

By default spring boot starts on port 8080. This can be changed by specifying in the application.properties the port on which spring boot should be started-
server.port = 8081
This will start spring boot on port 8081

What is Spring Boot Dev Tools? How to use it?

Spring Boot 1.3 provides another module called Spring Boot DevTools. DevTools stands for Developer Tool. The aim of the module is to try and improve the development time while working with the Spring Boot application. Spring Boot DevTools pick up the changes and restart the application.
When using Maven Framework include the following dependency
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-devtools</artifactId>
</dependency>
When using Gradle Framework include the following dependency
compile group: 'org.springframework.boot', name: 'spring-boot-starter-devtools'
 

What are the features of Spring Boot Devtools

  • Automatic Restart
  • Property Defaults
  • Live Reload
  • Remote Debug Tunneling
  • Remote Update and Restart
  • Global Configuration

What Is a Spring Boot?

Spring boot is a spring module that provides RAD framework capabilities to the spring framework. It helps us in creating efficient applications which you can run .it removes a lot of configurations and dependencies. It offers auto-configuration and embedded application server functionality, such as tomcat, jetty, and others.

What are the features of spring boot?

  • Creates a stand-alone spring application that requires very little configuration.
  • Provide features like as metrics, health checks, and externalised configuration
  • There isn't any need for any XML configuration.
  • It has spring actuator that helps running spring boots applications


Explain the difference between spring and spring boot?

    Spring
  • It is a web application framework based on java
  • It allows us to design loosely linked applications, which makes things easier.
  • Spring framework has more complexity than Spring Boot
  • There isn't any support for an in-memory database
    Spring boot
  • The Spring Boot Framework is a popular framework for creating REST APIs
  • It helps in creation of stand-alone application.
  • Spring Boot has more complexity than Spring framework
  • It provides several plugin for working with an embedded and in-memory database

How does spring boot work?

Spring Boot uses annotations to automatically configure your application based on the dependencies you've added to the project. The class with the @SpringBootApplication annotation and the main method is the spring boot application's entry point. The @ComponentScan annotation in Spring Boot automatically scans all of the project's components.

Using Maven, explain the way to create a Spring Boot application ?.

There are several options to construct a Spring Boot application using maven, following are the ways to develop a Spring Boot project/ application using maven:
  • Spring Boot CLI
  • Spring Maven Project
  • Spring Starter Project Wizard
  • Spring Initializr


What are starter dependencies?

Spring boot starter is a maven template that contains a set of all the mandatory transitive dependencies that are needed to start a particular functionality. To create a online application, we must import the spring-boot-starter-web dependency.
<dependency>
<groupId> org.springframework.boot</groupId>
<artifactId> spring-boot-starter-web </artifactId>
</dependency>

How would you describe the HTTPS request through the Spring Boot application?

Follow the MVC pattern in a high-level spring boot application, as shown in the flow diagram below.

Spot the difference between spring boot and spring MVC?

Spring boot
  • Spring Boot is a Spring module that gives logical defaults for Spring-based applications.
  • The development time is considerably decreased because all dependencies are wrapped together.
  • It comes with default configurations for creating Spring-powered frameworks./li>
Spring mvc
  • Under the Spring framework, Spring MVC is a model-view-controller-based web framework.
  • In a coupling situation, one must guarantee that every dependencies and versions match, which can lead to a extended development time.
  • It offers ready-to-use functionality for creating web applications.

How to you enable debugging log in the spring boot application?

There are three ways to activate debugging logs:
  • Start the application with --debug switch.
  • In the application.property file, we can set the logging.level.root=debug property.
  • In the logging configuration file, we can set the root logger's logging level to debug.

What are the Steps to deploy spring boot web application as JAR and WAR files?

to deploy a spring boot web application, you have to add following plugin in the pom.xml file in ,just add a plugin element to pom.xml:
 <plugin>
   <groupId >org.springframework.boot</groupId >
    <artifactId > spring-boot-maven-plugin </artifactId >
    </plugin>
The packaging elements in pom.xml file must be set to jar to builda JAR file as below:
    <packaging>jar</packaging>
    <packaging>war</packaging>
   

Can we create a non web application in spring boot?

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

Explain thymeleaf and the way to use it?

Thymeleaf is ne of the server-side template engine used for web applications in Java. It is designed to provide a natural template for your web application and is compatible with Spring Framework and HTML5 Java web applications.
<dependency>    
<groupId>org.springframework.boot</groupId>    
<artifactId>spring-boot-starter-thymeleaf</artifactId>    
</dependency>

What is dependency Injection?

  • Setter Injection: The IOC container will invoke the setter method to inject the dependent bean object into the target bean object.
  • Constructor Injection: By calling the target bean constructor, the IOC container will inject the dependent bean object into the target bean object.
  • Field Injection: Using the Reflection API, the IOC container will inject the dependent bean object into the target bean object.