CH5 SpringFramework
CH5 SpringFramework
It was developed by Rod Johnson in 2003. Spring framework makes the easy development of
JavaEEapplications.
Spring Framework
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.
The framework can be defined as a structure where we find solution of the various
technical problems.
The Spring framework comprises several modules such as IOC, AOP, DAO, Context,
ORM, WEB MVC etc.
Inversion Of Control (IOC) and Dependency Injection
These are the design patterns that are used to remove dependency from the programming
code.
They make the code easier to test and maintain.
1.
class Employee{
Address address;
Employee(){
address=new Address();
}
}
In such case, there is dependency between the Employee and Address (tight coupling). In
the Inversion of Control scenario, we do this something like this:
class Employee{
Address address;
Employee(Address address){
this.address=address;
}
}
Thus, IOC makes the code loosely coupled. In such case, there is no need to modify the
code if our logic is moved to new environment.
In Spring framework, IOC container is responsible to inject the dependency. We provide
metadata to the IOC container either by XML file or annotation.
Advantages of Dependency Injection
o makes the code loosely coupled so easy to maintain
o makes the code easy to test
Spring Modules
The Spring framework comprises of many modules such as core, beans, context,
expression language, AOP, Aspects, Instrumentation, JDBC, ORM, OXM, JMS,
Transaction, Web, Servlet, Struts etc.
These modules are grouped into Test, Core Container, AOP, Aspects, Instrumentation,
Data Access / Integration, Web (MVC / Remoting) as displayed in the following
diagram.
.
Test
o This layer provides support of testing with JUnit and TestNG.
Spring Core Container
o The Spring Core container contains core, beans, context and expression language
(EL) modules.
Core and Beans
o These modules provide IOC and Dependency Injection features.
Context
o This module supports internationalization (I18N), EJB, JMS, Basic Remoting.
Expression Language
o It is an extension to the EL defined in JSP. It provides support to setting and
getting property values, method invocation, accessing collections and indexers,
named variables, logical and arithmetic operators, retrieval of objects by name
etc.
AOP, Aspects and Instrumentation
o These modules support aspect oriented programming implementation where you
can use Advices, Pointcuts etc. to decouple the code.
o The aspects module provides support to integration with AspectJ.
o The instrumentation module provides support to class instrumentation and
classloader implementations.
Data Access / Integration
o This group comprises of JDBC, ORM, OXM, JMS and Transaction modules.
These modules basically provide support to interact with the database.
Web
o This group comprises of Web, Web-Servlet, Web-Struts and Web-Portlet. These
modules provide support to create web application.
Spring MVC
A Spring MVC is a Java framework which is used to build web applications. It follows
the Model-View-Controller design pattern. It implements all the basic features of a core
spring framework like Inversion of Control, Dependency Injection.
A Spring MVC provides an elegant solution to use MVC in spring framework by the help
of DispatcherServlet. Here, DispatcherServlet is a class that receives the incoming
request and maps it to the right resource such as controllers, models, and views.
o Model - A model contains the data of the application. A data can be a single object or a
collection of objects.
o Controller - A controller contains the business logic of an application. Here, the
@Controller annotation is used to mark the class as the controller.
o View - A view represents the provided information in a particular format. Generally,
JSP+JSTL is used to create a view page. Although spring also supports other view
technologies such as Apache Velocity, Thymeleaf and FreeMarker.
o Front Controller - In Spring Web MVC, the DispatcherServlet class works as the front
controller. It is responsible to manage the flow of the Spring MVC application .
Understanding the flow of Spring Web MVC
o As displayed in the figure, all the incoming request is intercepted by the
DispatcherServlet that works as the front controller.
o The DispatcherServlet gets an entry of handler mapping from the XML file and forwards
the request to the controller.
o The controller returns an object of ModelAndView.
o The DispatcherServlet checks the entry of view resolver in the XML file and invokes the
specified view component.
Advantages of Spring MVC Framework
The advantages of Spring MVC Framework:-
o Separate roles - The Spring MVC separates each role, where the model object,
controller, command object, view resolver, DispatcherServlet, validator, etc. can be
fulfilled by a specialized object.
o Light-weight - It uses light-weight servlet container to develop and deploy your
application.
o Powerful Configuration - It provides a robust configuration for both framework and
application classes that includes easy referencing across contexts, such as from web
controllers to business objects and validators.
o Rapid development - The Spring MVC facilitates fast and parallel development.
o Reusable business code - Instead of creating new objects, it allows us to use the existing
business objects.
o Easy to test - In Spring, generally we create JavaBeans classes that enable you to inject
test data using the setter methods.
o Flexible Mapping - It provides the specific annotations that easily redirect the page.
---------xxxxxxxxxx-----------