Spring Boot FW Unit -5
Spring Boot FW Unit -5
spend a great deal of time focusing on the core of the framework and all its parts. There will be a
lot of content to go through so please go through it diligently as it will set a solid foundation for
you.
Spring
Spring is one of the most extensively used Java EE Frameworks for building applications. For the
Java platform, the Spring framework provides a detailed programming and configuration model.
It aims to simplify the Java EE development and helps developers be more productive at work. It
can be used at any kind of deployment platform. It tries to cater for todays needs in business to
make it quick an easy to develop applications.
Spring Boot
While the Spring framework focuses on providing flexibility to you, Spring Boot aims to shorten
the code length and provide you with the easiest way to develop a web application. With annotation
configuration and default codes, Spring Boot shortens the time involved in developing an
application. It helps create a stand-alone application with less or almost zero-configuration. IT is
a module of the spring framework and built on top of it.
Spring Boot follows a layered architecture in which each layer communicates with the layer
directly below or above it (hierarchical structure).
Presentation Layer: The presentation layer handles the HTTP requests (your Restful api),
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 using different tools such JDBC and Repository.
Database Layer: In the database layer, CRUD (create, retrieve, update, delete) operations are
performed. The actual scripts which get/insert information to and from the database
@Autowired We can use the @Autowired to mark a dependency which Spring is going to resolve
and inject. We can use this annotation with a constructor, setter, or field injection.
@SpringBootApplication We use this annotation to mark the main class of a Spring Boot
application, it encapsulates @Configuration, @EnableAutoConfiguration, and @ComponentScan
annotations with their default attributes.
Dependency Injection
Before we can understand dependencies inject we have to first understand what inversion of
control(Ioc) is and why we care about it
By contrast with traditional programming, in which our custom code makes calls to a library, IoC
enables a framework to take control of the flow of a program and make calls to our custom code.
Simply put, this allows for loose coupling of components and moves the responsibility of
managing components onto the container.
1. Constructor Injection
Class Animal {
@Autowired
public Dog dog() {
return new Dog();
}
1. Setter Injection
Class Animal {
private Dog dog;
@Autowired
public void setDog(Dog dog) {
this.dog = dog;
}
}
1. Field Injection
Class Animal {
@Autowired
private Dog dog;
}
Class RestController {
@Autowired // still field injection but this is how you can create an abstraction between your
view and service
private AccountService accService;
}
Interface AccountService {
...
}
Spring Actuator
This feature provides a lot of insights of a running Spring boot application. For example, you can
use Actuator to find out which beans are created in Spring’s application context and which request
path are mapped to controllers.