012-SpringFramework 2024517
012-SpringFramework 2024517
FRAMEWORK
Software Construction and
Development
▪ Influential and rapidly growing Java framework.
▪ Spring turned 22 years old on April 1st, 2024.
▪ A framework is a collection of jars, decoupled with different purposes for
creating a more complex application.
▪ Spring is open source, which means that many talented developers have
contributed to it.
▪ Spring is currently the VIP of Java frameworks.
▪ The Spring Framework provides an easy way to create, initialize, and connect
software components into practical, decoupled, easy-to-test, enterprise-ready
applications.
OVERVIEW
2
▪ Spring enables developers to develop enterprise-class
applications using POJOs.
▪ Spring is organized in a modular fashion.
▪ Spring does not reinvent the wheel instead; it truly makes use of
some of the existing technologies.
▪ Spring provides a consistent transaction management.
OVERVIEW
3
▪ POJO based
▪ Modular
▪ Integration with existing framework
▪ Testability
▪ Web MVC
▪ Central Exception Handling
▪ Lightweight
▪ Transaction Management
BENEFITS
4
SPRING
FRAMEWORK
ARCHITECTURE
5
▪ Core
▪ Provides fundamentals including IoC and Dependency injection.
▪ Beans
▪ Module provide BeanFactory.
▪ A sophisticated implementation of factory pattern.
▪ Context
▪ It is a medium to access any objects defined and configured
▪ SpEL
▪ A powerful expression language for querying and manipulating an object
graph at runtime
6
▪ JBDC
▪ An abstraction layer that removed tedious JDBC related config.
▪ ORM
▪ Provides integration layer for popular object-relational mapping API’s including JPA, JDO, hibernate.
▪ OXM
▪ Abstraction layer to support object/XML mappings implementations for JAXB, Castor, XMLBeans, JiBX
and Xstream.
▪ JMS
▪ Module that have features for producing and consuming messages.
▪ Transaction
▪ module supports programmatic and declarative transaction management for classes that implement
special interfaces and for all your POJOs
▪ Web-MVC
▪ This module contains Spring's Model-View-Controller (MVC) implementation for
web applications.
▪ Web-Socket
▪ This module provides support for WebSocket-based, two-way communication
between the client and the server in web applications.
▪ Web-Portlet
▪ This module provides the MVC implementation to be used in a portlet environment
and mirrors the functionality of Web-Servlet module.
WEB COMPONENTS
8
▪ AOP
▪ This module provides an aspect-oriented programming implementation allowing to define method-interceptors and
pointcuts to cleanly decouple code that implements functionality that should be separated.
▪ Aspects
▪ The module provides integration with AspectJ, which is again a powerful and mature AOP framework.
▪ Instrumentation
▪ This module provides class instrumentation support and class loader implementations to be used in certain application
servers.
▪ Messaging
▪ This module provides support for STOMP as the WebSocket sub-protocol to use in applications.
▪ Test
▪ This module supports the testing of Spring components with JUnit or TestNG frameworks.
ADDITIONAL COMPONENTS
9
▪ The Spring container is at
the core of the Spring
Framework.
▪ The container will create
the objects, wire them
together, configure them,
and manage their complete
life cycle from creation till
destruction.
▪ The Spring container uses DI
to manage the components
that make up an application.
These objects are called
SPRING – IOC CONTAINERS Spring Beans.
10
SPRING
CONTAINER TYPES
The ApplicationContext container
includes all functionality of the
BeanFactorycontainer, so it is
generally recommended over
BeanFactory. BeanFactory can still
be used for lightweight applications
like mobile devices or applet-based
applications where data volume
and speed is significant.
11
▪ The objects that form the backbone of your application and that
are managed by the Spring IoC container are called beans.
▪ A bean is an object that is instantiated, assembled, and otherwise
managed by a Spring IoC container.
▪ Bean definition contains the information called configuration
metadata, which is needed for the container to know the following
−
• How to create a bean
• Bean's lifecycle details
• Bean's dependencies
SPRING BEANS
12
Property Description
class This attribute is mandatory and specifies the bean class to be used to create the bean.
name This attribute specifies the bean identifier uniquely. In XMLbased configuration metadata, you use the id and/or name attributes to
specify the bean identifier(s).
scope This attribute specifies the scope of the objects created from a particular bean definition
constructor-arg This is used to inject the dependencies
properties This is used to inject the dependencies
lazy-initialization A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at the startup.
mode
initialization method A callback to be called just after all necessary properties on the bean have been set by the container.
destruction method A callback to be used when the container containing the bean is destroyed.
prototype This scopes a single bean definition to have any number of object instances.
request This scopes a bean definition to an HTTP request. Only valid in the context of a web-
aware Spring ApplicationContext.
session This scopes a bean definition to an HTTP session. Only valid in the context of a web-
aware Spring ApplicationContext.
global-session This scopes a bean definition to a global HTTP session. Only valid in the context of a
web-aware Spring ApplicationContext.
15
▪ It is a principle in software engineering which transfers the control of objects or portions of a
program to a container or framework.
▪ IoC enables a framework to take control of the flow of a program and make calls to our custom
code
• Inversion of Control can be achieved via various mechanisms such as: Strategy design pattern,
Service Locator pattern, Factory pattern, and Dependency Injection (DI).
16
▪ Dependency injection is a pattern we can use to implement IoC,
where the control being inverted is setting an object's
dependencies.
▪Dependency Injection (or sometime called wiring) helps in gluing
classes together and at the same time keeping them independent.
DEPENDENCY INJECTION
17
DEPENDENCY INJECTION (DI)
Consider you have an application which has a text editor
component, and you want to provide a spell check. Your
standard code would look something like this −
18
DI BY CONSTRUCTOR
▪ The container will invoke a constructor with
arguments each representing a dependency we
want to set.
▪ The @Configuration annotation indicates that
the class is a source of bean definitions.
▪We use the @Bean annotation on a method to
define a bean. For a bean with the default
singleton scope,
19
DI BY SETTERS
▪ For setter-based DI, the container will call setter
methods of our class after invoking a no-argument
constructor or no-argument static factory method to
instantiate the bean.
20
▪ In case of Field-Based DI, we can inject the dependencies by
marking them with an @Autowired annotation.
21
▪ Wiring allows the Spring container to
automatically resolve dependencies between
collaborating beans by inspecting the beans that
have been defined.
22
SPRING
FRAMEWORK
Software Construction and
Development
MVC ARCHITECTURE
24
SPRING MVC
▪ Modules of Spring Framework on the
Web layer.
▪Web module provides basic web-oriented integration
features and the initialization of the IoC container using
servlet listeners and a web application context.
25
A web framework built on the Servlet
API.
26
MODEL-VIEW-CONTROLLER (MVC)
▪ An architectural pattern commonly used
for developing user interfaces.
▪An application is divided into 3
interconnected parts:
▪ Model - Responsible for managing data of the
application.
▪ View - Responsible for displaying the model
data to user.
▪ Controller - Responsible for processing user
requests and building an appropriate model
and passes it to the view for rendering.
27
REQUEST
PROCESSING
WORKFLOW
28
DISPATCHED
SERVLET
▪Spring MVC is designed around a
central servlet named
DispatcherServlet.
▪DispatcherServlet acts as a central
entry point to the Spring MVC
application.
▪Every request is handled by
DispatcherServlet.
▪DispatcherServlet is an expression
of the Front Controller pattern.
29
CONTROLLERS
▪The Front Controller’s job is to
determine a suitable handler
capable of performing the actual
processing.
▪Handlers are Spring MVC
Controllers.
▪The selected Controller interacts
with the service layer; the relevant
data are collected in a model.
▪When the Controller has finished
processing, the Front Controller
determines which view to render.
30
VIEW
▪ When the Controller has
finished processing, the Front
Controller determines which
view to render.
▪The Front Controller passes the
model to the view which is
finally which is finally rendered
on the browser.
31
REQUEST PROCESSING WORKFLOW
IMPORTANT ANNOTATIONS
@RequestMapping
• @GetMapping, @PostMapping, @PutMapping and @DeleteMapping
@RequestParam, @PathVariable
@RequestBody
@ResponseBody
Ref: https://www.javadevjournal.com/spring-mvc/spring-mvc-annotations/
33