Spring Boot Basic Interview Q&A

 

Topics to be covered

  1. What is Spring Framework

  2. Advantages of Spring Framework

  3. What is Spring Boot?

  4. Advantages of Spring Boot?

  5. Limitations of Spring Boot?

  6. Spring vs Spring Boot

  7. Spring Boot vs Spring MVC

  8. Explain Spring MVC Flow

  9. Explain Spring Boot Architecture

  10. Explain Spring Boot Flow

  11. Dependency Injection

  12. Inversion of Control

 

What is Spring Framework?

  • Spring is a lightweight framework. In other words, it can be called as framework of frameworks

  • It provides support to the various frameworks such as Hibernate, JSF, EJB 

  • Java Spring Framework is a popular, open source, enterprise-level framework for creating standalone, production grade applications that run on the JVM

 

Advantages of Spring Framework?

Loose Coupling

     Spring applications are loosely coupled using the concept called Dependency Injection

      

Predefined Templates :  

    Spring Frameworks provide templates for Hibernate, JDBC, JPA, etc technologies. Spring Framework handles the basic steps of these technologies. So, there is no need to code much. 

    For Example, Incase JDBC template, we don't need to write commits, exceptions handling, creating and closing connection


What is Spring Boot?

  • Spring Boot is a project that is built on the top of the Spring Framework.

  • It provides an easier and faster way to setup, configure and run both simple and web based applications.


  • Spring Boot is a combination of Spring framework and Embedded servers.

  • In Spring Boot, there is no need of XML configuration (deployment descriptor).

  • It uses convention over configuration software design paradigm that means it decreases the effort of the developer.

  • We can use Spring STS IDE or Spring Initializer to develop Spring Boot Java applications. 

 
Advantages of Spring Boot?
  • The Dependency Injection approach is used in Spring Boot

  • It creates stand-alone spring applications that can be started using Java -jar

  • It tests web applications easily with the help of different Embedded HTTP servers such as Tomcat, Jetty, etc. We don't need to deploy war files

  • It provides opinionated starter POMs to simplify our Maven configuration

  • There is no requirement for XML configuration

  • It offers CLI tool for developing and testing the Spring Boot applications

  • It offers number of plugins 

  • Minimizes the boilerplate codes (the code that has to be included in many places with little or no alteration), XML configuration and annotations 

  • Increases the productivity and reduces the development time

Goals
  • The main Goal of Spring Boot is to reduce development, unit test and integration test time

  • Provides Opinionated Development approach

  • Avoids defining more Annotation Configuration

  • Avoids writing lots of import statements

  • Avoids XML Configuration.

 
 Limitations of Spring Boot?
  • Spring Boot can use dependencies that are not going to be used in the application. These dependencies increase the size of the application.

 
 Spring vs Spring Boot?
 

Spring

Spring Boot

Spring Framework is a widely used Java EE framework for building applications.

Spring Boot Framework is widely used to develop REST APIs.

It aims to simplify Java EE development that makes developers more productive.

It aims to shorten the code length and provide the easiest way to develop Web Applications.

The primary feature of the Spring Framework is dependency injection.

The primary feature of Spring Boot is Autoconfiguration. It automatically configures the classes based on the requirement.

It helps to make things simpler by allowing us to develop loosely coupled applications.

It helps to create a stand-alone application with less configuration.

The developer writes a lot of code (boilerplate code) to do the minimal task.

It reduces boilerplate code.

To test the Spring project, we need to set up the sever explicitly.

Spring Boot offers embedded server such as Jetty and Tomcat, etc.

It does not provide support for an in-memory database.

It offers several plugins for working with an embedded and in-memory database such as H2.

Developers manually define dependencies for the Spring project in pom.xml.

Spring Boot comes with the concept of starter in pom.xml file that internally takes care of downloading the dependencies JARs based on Spring Boot Requirement.

 

Spring Boot vs Spring MVC
 

Spring Boot

Spring MVC

Spring Boot is a module of Spring for packaging the Spring-based application with sensible defaults.

Spring MVC is a model view controller-based web framework under the Spring framework.

It provides default configurations to build Spring-powered framework.

It provides ready to use features for building a web application.

There is no need to build configuration manually.

It requires build configuration manually.

There is no requirement for a deployment descriptor.

A Deployment descriptor is required.

It avoids boilerplate code and wraps dependencies together in a single unit.

It specifies each dependency separately.

It reduces development time and increases productivity.

It takes more time to achieve the same.



Explain SPRING MVC Flow
 
 
 
  1. Whenever we hit any url in the browser, first it will come to Dispatcher Servelet (Front controller)

  2. By using Handler Mapper, Dispatcher servelet will know the corresponding controller to process the request. Here, Handler mapper means method annotated with @RequestMapping("customers")]

  3. The Conroller method is responsible for processing the request. After processing, it will return Model and View object

  4. The View resolver (By default InternalResourceViewResolver) will find the views (prefix, suffix), view means JSP or Thymeleaf

  5. Then View Engine merges the Views and model data and produce a html response to the Dispatcher Servelet

  6. The dispatcher servelet will send the html response to the client.


Dispatcher servelet is front controller. Its configured in WEB.xml

In folder, src/main/webApp/WEB-INF/View will have views pages

In folder, src/main/webApp/WEB-INF/Resources will have Application Properties file.where we can mention prefix, suffix for ModelandView object

 

Explain SPRING Boot Architecture 


Four layers in Spring Boot are,

  1. Presentation Layer
  2. Business Layer
  3. Persistence Layer
  4. Database Layer
 
Presentation Layer: The presentation layer handles the HTTP requests, translates the JSON parameter to object, and authenticates the request and transfer it to the business layer. In short, it consists of views i.e., frontend part.

Business Layer: The business layer handles all the business logic. It consists of service classes and uses services provided by data access layers. It also performs authorization and validation.

Persistence Layer: The persistence layer contains all the storage logic and translates business objects from and to database rows.

Database Layer: In the database layer, CRUD (create, retrieve, update, delete) operations are performed.

 
 
Explain SPRING Boot Flow

 
 
  • Now, we have validator classes, view classes and Utility classes

  • Spring Boot uses all the modules of Spring-like Spring MVC, Spring Data, etc.

  • The architecture of SPRING BOOT is same as the architecture of SPRING MVC, except one thing : there is no need for DAO and DAOImpl classes in Spring Boot

  • Creates a Data Access Layer and performs CRUD Operation.

  • The Client makes the HTTP requests (PUT or GET)

  • The Request goes to the controller, and the controller maps that request and handles it. After that, it calls the Service logic if required.

  • In the Service Layer, all the business logics will be performed. Service Layer performs the logic on the data that is mapped to JPA with model classes.

  • A JSP page is returned to the user if no error occured.

     

What is Dependency Injection (DI) and Inversion of Control (IOC)?

Dependency Injection (DI) is based on the principle of Inversion of Control (IOC).

It means instead of code controlling the flow, the control is inverted or shifted to the SPRING BOOT Framework that manages and controls the flow of program.

Spring Boot achieves DI by using the container called "Application Context". The Application Context manages and controls the dependencies in our code by creating and maintaining instances of the different classes that our code needs.

When our code needs to use a particular class, it requests an instance of that class from the application context. Then the application context creates a new instance of the class (If the instance doesn't already exist) and injects into our code where needed.

Goals of Dependency Injection : 

Using DI, Spring Boot can manage and control the dependencies in our code and making it easier to write, test and maintain. 

DI allows more flexibility and extensibility because we can easily change the dependencies used by our code without changing the code itself.

  

 Advantages of Dependency Injection :

  • Decoupling of Components

  • Reusability of Components

  • Testability

  • Flexibility

  • Scalability


What are the ways to create Spring Boot Application

We can create Spring Boot Projects using by either Spring Initializr or STS (Spring Tool Suite)


Creating Spring Boot project using Spring Initializr

  1. Open the Spring Initializr https://start.spring.io

  2. Provide the Group and Artifact name

  3. Now Click on the button Generate

  4. When we click on the button Generate, it starts packaging the project in a .rar file and downloads the project

  5. Extract the RAR file

  6. Import the folder in STS or Eclipse IDE

    File → Import → Existing Maven Project


Creating Spring Boot project using STS (Spring Tool Suite)

  1. Open the STS

  2. File → New → Maven Projects

  3. Select the maven-archetype-quickstart and click Next

  4. Provide Group ID, Artifact ID and FINISH

  5. Now, Open App.java, we can see some default code

  6. Open POM file and add Java version inside the tag properties

    <java.version>1.8</java.version>

  7. In order to make it a Spring Boot Project, we need to add spring-boot-starter-parent and spring-boot-starter-web dependencies in pom.xml