You are on page 1of 10

Spring Boot –

Architecture
The Spring Boot is built on top of the core Spring framework. It is a simplified and
automated version of the spring framework. The spring boot follows a layered
architecture in which each layer communicates to other layers.

Spring Boot makes it easy to create stand-alone, production-grade Spring based


application that you can “Just Run”

The main aim of spring boot is to remove the XML and annotations-based
configuration settings from the application.

Spring Boot Layers


The spring boot consists of the following four layers:

1. Presentation Layer – Authentication & Json Translation


2. Business Layer – Business Logic, Validation & Authorization
3. Persistence Layer – Storage Logic
4. Database Layer – Actual Database
1. Presentation Layer

The presentation layer is the top layer of the spring boot architecture. It consists of
Views. i.e., the front-end part of the application. It handles the HTTP requests and
performs authentication. It is responsible for converting the JSON field’s
parameter to Java Objects and vice-versa. Once it performs the authentication of
the request it passes it to the next layer. i.e., the business layer.

2. Business Layer

The business layer contains all the business logic. It consists of services classes. It
is responsible for validation and authorization.

3. Persistence Layer

The persistence layer contains all the database storage logic. It is responsible for
converting business objects to the database row and vice-versa.

4. Database Layer

The database layer contains all the databases such as MySql, MongoDB, etc. This
layer can contain multiple databases. It is responsible for performing the CRUD
operations.
Spring Boot Flow Architecture

Explanation:

• The Client makes an HTTP request(GET, PUT, POST, etc.)


• The HTTP request is forwarded to the Controller. The controller maps the
request. It processes the handles and calls the server logic.
• The business logic is performed in the Service layer. The spring boot
performs all the logic over the data of the database which is mapped to the
spring boot model class through Java Persistence Library(JPA).
• The JSP page is returned as Response from the controller.

JPA - Java Persistence API

-----

The java persistence API provides a specification for persisting, reading, and
managing data from your java object to your relational tables in the database.
Dependency->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

application.properties file:

spring.datasource.url=jdbc:mysql://localhost:3306/emp
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update

Modal layer:

Create a simple POJO(Plain old java class) with some JPA annotation.

@Entity: This annotation defines that a class can be mapped to a table

@Id: This annotation specifies the primary key of the entity.

@GeneratedValue: This annotation is used to specify the primary key generation


strategy to use. i.e. Instructs database to generate a value for this field
automatically. If the strategy is not specified by default AUTO will be used.

// @ID This annotation specifies

// the primary key of the entity.


@Id

// @GeneratedValue This annotation

// is used to specify the primary

// key generation strategy to use

@GeneratedValue(strategy = GenerationType.AUTO)

private long id;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

DAO(Data access object) layer:

• @Repository: The @Repository annotation is a marker for any


class that fulfills the role or stereotype of a repository
(also known as Data Access Object or DAO).
• JpaRepository<Employee, Long> JpaRepository is a JPA-specific
extension of the Repository. It contains the full API of
CrudRepository and PagingAndSortingRepository. So it contains
API for basic CRUD operations and also API for pagination and
sorting. Here we enable database operations for Employees.

import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.stereotype.Repository;

import com.example.demo.modal.Employee;

// @Repository is a Spring annotation that

// indicates that the decorated class is a repository.

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long>{

ArrayList<Employee> findAllEmployee();

Service layer:

@Service: This annotation is used with classes that provide some business
functionalities. Spring context will autodetect these classes when
annotation-based configuration and classpath scanning is used. Here JPA
repository has lots of predefined generic methods to perform the database
operation some are used in the below code.

import java.util.ArrayList;

import java.util.Iterator;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.example.demo.modal.Employee;

import com.example.demo.repository.EmployeeRepository;

// @Service marks a Java class that performs some service,

// such as executing business logic, performing

// calculations, and calling external APIs.

@Service

public class EmpServiceImpl implements EmpService {

@Autowired
EmployeeRepository employeeRepository;

@Override

public ArrayList<Employee> findAllEmployee() {

return (ArrayList<Employee>) employeeRepository.findAll();

@Override

public Employee findAllEmployeeByID(long id) {

Optional<Employee> opt = employeeRepository.findById(id);

if (opt.isPresent())

return opt.get();

else

return null;

@Override

public void addEmployee() {

ArrayList<Employee> emp = new ArrayList<Employee>();

emp.add(new Employee("Lucknow", "Shubham"));

emp.add(new Employee("Delhi", "Puneet"));

emp.add(new Employee("Pune", "Abhay"));

emp.add(new Employee("Noida", "Anurag"));

for (Employee employee : emp) {

employeeRepository.save(employee);
}

@Override

public void deleteAllData() {

employeeRepository.deleteAll();

Controller layer:

• @RestController: This is a Spring annotation that is used to build


REST API in a declarative way. RestController annotation is applied to
a class to mark it as a request handler, and Spring will do the
building and provide the RESTful web service at runtime.
• @Autowired: This annotation can be used to autowire bean on
the setter method just like @Required annotation, constructor,
a property, or methods with arbitrary names and/or multiple
arguments.
• @PostMapping: This annotation maps HTTP POST requests onto specific
handler methods. It is a composed annotation that acts as a shortcut
for @RequestMapping(method = RequestMethod. POST)
• @GetMapping: This annotation is a specialized version of
@RequestMapping annotation that acts as a shortcut for
@RequestMapping(method = RequestMethod. GET). The @GetMapping annotated
methods in the @Controller annotated classes handle the HTTP GET
requests matched with the given URI expression.
• @DeleteMapping: This annotation maps HTTP DELETE requests onto
specific handler methods. It is a composed annotation that acts as a
shortcut for @RequestMapping(method = RequestMethod. DELETE

package com.example.demo.controller;

import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.DeleteMapping;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RestController;

import com.example.demo.modal.Employee;

import com.example.demo.service.EmpServiceImpl;

@RestController

public class EmpController {

@Autowired

EmpServiceImpl empServiceImpl;

@PostMapping("/")

public void add() {

empServiceImpl.addEmployee();

@GetMapping("/findall")

public ArrayList<Employee> getAllEmployee() {

return empServiceImpl.findAllEmployee();

@GetMapping("/findbyid/{id}")

public Employee getEmployeeUsingId(@PathVariable long id) {

return empServiceImpl.findAllEmployeeByID(id);

}
@DeleteMapping("/delete")

public void delete() {

empServiceImpl.deleteAllData();

You might also like