FSD
Week 7
What is Spring?
The Spring Framework (Spring) is an open-source application framework that provides
infrastructure support for developing Java applications.
One of the most popular Java Enterprise Edition (Java EE) frameworks, Spring helps
developers create high performing applications using plain old Java objects (POJOs).
Spring is a lightweight framework. It can be thought of as a framework of
frameworks because it provides support to various frameworks such as Struts, Hibernate,
Tapestry, EJB, JSF, etc.
Why Spring?
1. Easy, Simple, and Lightweight
Spring is easy to learn and implement which is composed of modules where we can
write spring applications normally with interfaces and abstract classes just like java
applications.
Spring helps in coupling and wiring the components which make working with Spring
easier and simpler.
It is lightweight as we can inject dependencies as per our need
2. Builds Secure Web Applications
Spring provides security if Spring Security is on the classpath and we can customize the
security settings further for basic authentication and prevent vulnerabilities in our
project.
3. MVC Pattern
Model View Controller which helps in separating implementation and business logic
The view is where requests are received first which are taken by the controller to the
corresponding model for results which are then taken to view to show at the front side
of the application.
4. Easy Communication with Databases
Dept. of CSE S.M.P SAGAR Page 1
FSD
With the support of DAO(Data Access Object) in Spring, data access related
technologies such as Hibernate, JDBC, and JPA make it easier to communicate with
databases.
5. Modular Design
It is divided into components such as core container, data access / integration, web, and
test which work together but independently.
Implementation can also be divided in Spring according to the MVC pattern
6. Can be Integrated with Other Frameworks
It can be used with other frameworks such as Struts, and Hibernate.
7. Dependency Injection
Dependency Injection helps in decreasing coupling and dependency between classes
in Spring projects so that the program becomes maintainable and reusable.
Modules of one project can be used effectively in another project like a login page, and
registration page which not only saves them time but also promotes code reusability
8. Follows Aspect-Oriented Programming
Aspect-Oriented Programming allows us to think differently about the structure of the
program by enabling the modularization of concerns.
It helps in breaking down the logic into parts known as concerns and the concerns help
in dividing the business logic of an application and in increasing the modularity.
9. Testing becomes easy
classes can be tested independently without having to depend on one another
10. Handle external resources easily
Resource and ResourceLoader are the interfaces present in Spring to handle external
resources
How Spring works
A web application (layered architecture) commonly includes three layers:
Dept. of CSE S.M.P SAGAR Page 2
FSD
1. Presentation/view layer (UI) - This is the outermost layer which handles the presentation of
content and interaction with the user.
2. Business logic layer - The central layer that deals with the logic of a program.
3. Data access layer - The deep layer that deals with data retrieval from sources.
Important terms
Autowiring - The process by which Spring identifies dependencies and matches and populates
them.
Bean - A Spring bean is an object that is instantiated, created, and managed by the IoC container.
Beans are the backbone of an application.
Dependency injection - A programming design pattern that makes code loosely coupled,
meaning that any change in the application of one, will not affect the other.
Inversion of control (IoC) - Taking control away from the class and giving it to the Spring
Framework.
Inversion of control container - This is the core of the Spring Framework where objects are
created, wired together, configured, and managed throughout their life cycle.
Spring Framework Architecture
Dept. of CSE S.M.P SAGAR Page 3
FSD
Core Container
The Core Container consists of the Core, Beans, Context, and Expression Language modules
The Core module provides the fundamental parts of the framework, including the IoC
and Dependency Injection features.
The Bean module provides BeanFactory, which is a sophisticated implementation of the
factory pattern.
The Context module builds on the solid base provided by the Core and Beans modules
and it is a medium to access any objects defined and configured. The ApplicationContext
interface is the focal point of the Context module.
The SpEL module provides a powerful expression language for querying and
manipulating an object graph at runtime.
Data Access/Integration
Dept. of CSE S.M.P SAGAR Page 4
FSD
The Data Access/Integration layer consists of the JDBC, ORM, OXM, JMS and Transaction
modules
The JDBC module provides a JDBC-abstraction layer that removes the need for tedious
JDBC related coding.
The ORM module provides integration layers for popular object-relational mapping
APIs, including JPA, JDO, Hibernate, and iBatis.
The OXM module provides an abstraction layer that supports Object/XML mapping
implementations for JAXB, Castor, XMLBeans, JiBX and XStream.
The Java Messaging Service JMS module contains features for producing and consuming
messages.
The Transaction module supports programmatic and declarative transaction
management for classes that implement special interfaces and for all your POJOs.
Web
The Web layer consists of the Web, Web-MVC, Web-Socket, and Web-Portlet modules
The Web module provides basic web-oriented integration features such as multipart file-
upload functionality and the initialization of the IoC container using servlet listeners and a
web-oriented application context.
The Web-MVC module contains Spring's Model-View-Controller (MVC)
implementation for web applications.
The Web-Socket module provides support for WebSocket-based, two-way
communication between the client and the server in web applications.
The Web-Portlet module provides the MVC implementation to be used in a portlet
environment and mirrors the functionality of Web-Servlet module.
Key components of Spring Framework
Spring Core
Spring AOP
Spring Web MVC
Spring DAO
Spring ORM
Spring context
Spring Web flow
Dept. of CSE S.M.P SAGAR Page 5
FSD
SpringBoot
Spring Boot is an open source Java-based framework used to create a micro Service.
Spring Boot provides a good platform for Java developers to develop a stand-alone and
production-grade spring application that you can just run. You can get started with
minimum configurations without the need for an entire Spring configuration setup
Advantages
Spring Boot offers the following advantages to its developers −
Easy to understand and develop spring applications
Increases productivity
Reduces the development time
Why SpringBoot?
It provides a flexible way to configure Java Beans, XML configurations, and Database
Transactions.
It provides a powerful batch processing and manages REST (Representational state
transfer building and interact with web service )endpoints.
In Spring Boot, everything is auto configured; no manual configurations are needed.
It offers annotation-based spring application
Eases dependency management
It includes Embedded Servlet Container
Compare Spring and SpringBoot ******IMP******
Spring Spring Boot
Spring Framework is a widely used Spring Boot Framework is widely used to
Java EE framework for building develop REST APIs.
applications.
It aims to simplify Java EE It aims to shorten the code length and provide the
development that makes developers easiest way to develop Web Applications.
more productive.
Dept. of CSE S.M.P SAGAR Page 6
FSD
The primary feature of the Spring The primary feature of Spring Boot
Framework is dependency injection. is Autoconfiguration. It automatically configures the
classes based on the requirement.
It helps to make things simpler by It helps to create a stand-alone application with less
allowing us to develop loosely configuration.
coupled applications.
The developer writes a lot of code It reduces boilerplate code.
(boilerplate code) to do the minimal
task.
To test the Spring project, we need to Spring Boot offers embedded server such
set up the sever explicitly. as Jetty and Tomcat, etc.
It does not provide support for an in- It offers several plugins for working with an
memory database. embedded and in-memory database such as H2.
Developers manually define Spring Boot comes with the concept of starter in
dependencies for the Spring project [Link] file that internally takes care of downloading
in [Link]. the dependencies JARs based on Spring Boot
Requirement.
Under standing the spring initializer interface
Spring Initializer is a web-based tool. With the help of Spring Initializer, we can easily generate
the structure of the Spring Boot Project. It offers extensible API for creating JVM-based projects.
Annotations ******IMP-*******
Annotations are used to provide supplemental information about a program.
Annotations start with ‘@’.
Annotations help to associate metadata (information) to the program elements i.e. instance
variables, constructors, methods, classes, etc.
Dept. of CSE S.M.P SAGAR Page 7
FSD
Annotations basically are used to provide additional information, so could be an alternative
to XML and Java marker interfaces.
Spring Annotations List
@Service
Annotate all your service classes with @Service. All your business logic should be in Service
classes.
@Service
public class CompanyServiceImpl implements CompanyService {
...
}
@Repository
Annotate all your DAO(database access object) classes with @Repository. All your database
access logic should be in DAO classes.
@Repository
public class CompanyDAOImpl implements CompanyDAO {
...
}
@Component
Annotate your other components (for example REST resource classes) with @Component
@Component
public class ContactResource {
...
}
@Component is a generic stereotype for any Spring-managed component. @Repository,
@Service, and @Controller are specializations of @Component for more specific use cases, for
example, in the persistence, service, and presentation layers, respectively.
Dept. of CSE S.M.P SAGAR Page 8
FSD
@Autowired
Let Spring auto-wire other beans into your classes using @Autowired annotation.
@Service
public class CompanyServiceImpl implements CompanyService {
@Autowired
private CompanyDAO companyDAO;
...
}
@Transactional
Configure your transactions with @Transactional spring annotation.
@Service
public class CompanyServiceImpl implements CompanyService {
@Autowired
private CompanyDAO companyDAO;
@Transactional
public Company findByName(String name) {
Company company = [Link](name);
return company;
}
...
}
@Scope
To change default behavior, use @Scope spring annotation.
@Component
Dept. of CSE S.M.P SAGAR Page 9
FSD
@Scope("request")
public class ContactResource {
...
}
What is inversion of control?
Spring IoC (Inversion of Control) Container is the core of Spring Framework. It creates
the objects, configures and assembles their dependencies, manages their entire life cycle.
Inversion of Control (IoC) is a design principle that allows classes to be loosely coupled
and, therefore, easier to test and maintain.
IoC refers to transferring the control of objects and their dependencies from the main
program to a container or framework.
What is dependency injection?
Dependency Injection is the main functionality provided by Spring IOC(Inversion of
Control).
The Spring-Core module is responsible for injecting dependencies through either
Constructor or Setter methods.
Types of Spring Dependency Injection.
1. Constructor Injection: In the constructor injection, the injector supplies the service
(dependency) through the client class constructor.
2. Property Injection: In the property injection (aka the Setter Injection), the injector
supplies the dependency through a public property of the client class.
3. Method Injection: In this type of injection, the client class implements an interface
which declares the method(s) to supply the dependency and the injector uses this
interface to supply the dependency to the client class.
Spring IoC container
The IoC container is responsible to instantiate, configure and assemble the objects. The IoC
container gets informations from the XML file and works accordingly. The main tasks performed
by IoC container are:
o to instantiate the application class
o to configure the object
Dept. of CSE S.M.P SAGAR Page 10
FSD
o to assemble the dependencies between the objects
There are two types of IoC containers. They are:
1. BeanFactory
2. ApplicationContext
There are mainly three types of Dependency Injection:
Constructor Injection: In this type of injection, the injector supplies dependency through
the client class constructor.
Setter Injection / Property Injection: In this type of injection, the injector method injects
the dependency to the setter method exposed by the client.
Interface Injection: In this type of injection, the injector uses Interface to provide
dependency to the client class. The clients must implement an interface that will expose a
setter method which accepts the dependency.
Converting monolithic application to microservices architecture ******IMP*******
Step 1: Split the code.
Step 2: Split the database.
Step 3: Define a standalone Delivery Service.
Step 4: Use the standalone Delivery Service.
Step 5: Remove the delivery management functionality from the FTGO(food truck go)
monolith.
Dept. of CSE S.M.P SAGAR Page 11
FSD
Step 1: Split the code
The first step is to split the code and convert delivery management into a separate, loosely
coupled module within the monolith.
Step 2: Split the database
Dept. of CSE S.M.P SAGAR Page 12
FSD
The second step is split the database and define a separate database schema for the ftgo-delivery-
service module.
Step 3: Define a standalone Delivery Service
The third step of the refactoring process is to define a standalone Delivery Service and deploy it.
The service does not, however, handle production traffic. Instead, it can, for example, be tested
in production.
Step 4: Use the standalone Delivery Service
The fourth step of the refactoring process is to use the standalone Delivery Service.
Dept. of CSE S.M.P SAGAR Page 13
FSD
Step 5: Remove the delivery management functionality from the FTGO monolith
The fifth step of the refactoring process is to remove the now obsolete delivery management
logic from the monolith.
Dept. of CSE S.M.P SAGAR Page 14