Introduction to Spring FrameworkFundamentally, what is Spring? We think of it as a Platform for your
Java code. It provides comprehensive infrastructural support for developing
Java applications. Spring deals with the plumbing so you can focus on
solving the domain problemSpring as a platform allows applications to be built from “plain old
Java objects” (POJOs). This is true for the Java SE programming model as
well as within a number of other environments including full and partial
Java EE. Spring allows enterprise services to be applied to POJOs in a
non-invasive wayExamples of Spring as a platform:Make a Java method execute in a database transaction; without the
implementer dealing with transaction APIsMake a local Java method a remote-procedure; without the
implementer dealing with remoting APIsMake a local Java method a management operation; without the
implementer dealing with JMX APIsMake a local Java method a message handler; without the
implementer dealing with JMS APIsDependency InjectionBackgroundIn early 2004, Martin Fowler asked the readers of his site: when
talking about Inversion of Control: the question is,
what aspect of control are [they] inverting?. Fowler
then suggested renaming the principle (or at least giving it a more
self-explanatory name), and started to use the term
Dependency Injection. His article then continued
to explain the ideas underpinning the Inversion of Control
(IoC) and Dependency Injection
(DI) principle.If you need a decent insight into IoC and DI, please do refer to
said article: http://martinfowler.com/articles/injection.html.Java applications (a loose term which runs the gamut from
constrained applets to full-fledged n-tier server-side enterprise
applications) typically are composed of a number of objects that
collaborate with one another to form the application proper. The objects
in an application can thus be said to have
dependencies between themselves.The Java language and platform provides a wealth of functionality
for architecting and building applications, ranging all the way from the
very basic building blocks of primitive types and classes (and the means
to define new classes), to rich full-featured application servers and web
frameworks. One area that is decidedly conspicuous by its absence is any
means of taking the basic building blocks and composing them into a
coherent whole; this area has typically been left to the purvey of the
architects and developers tasked with building an application (or
applications). Now to be fair, there are a number of design patterns
devoted to the business of composing the various classes and object
instances that makeup an all-singing, all-dancing application. Design
patterns such as Factory, Abstract
Factory, Builder,
Decorator, and Service
Locator (to name but a few) have widespread recognition and
acceptance within the software development industry (presumably that is
why these patterns have been formalized as patterns in the first place).
This is all very well, but these patterns are just that: best practices
given a name, typically together with a description of what the pattern
does, where the pattern is typically best applied, the problems that the
application of the pattern addresses, and so forth. Notice that the last
paragraph used the phrase ... a description of
what the pattern does...; pattern books and wikis are typically
listings of such formalized best practice that you can certainly take
away, mull over, and then implement yourself in your
application.The IoC component of the Spring Framework addresses the enterprise
concern of taking the classes, objects, and services that are to compose
an application, by providing a formalized means of composing these various
disparate components into a fully working application ready for use. The
Spring Framework takes best practices that have been proven over the years
in numerous applications and formalized as design patterns, and actually
codifies these patterns as first class objects that you as an architect
and developer can take away and integrate into your own application(s).
This is a Very Good Thing Indeed as attested to by
the numerous organizations and institutions that have used the Spring
Framework to engineer robust, maintainable
applications.ModulesThe Spring Framework contains a lot of features, which are
well-organized in about twenty modules. These modules can be grouped
together based on their primary features into Core Container, Data
Access/Integration, Web, AOP (Aspect Oriented Programming),
Instrumentation and Test. These groups are shown in the diagram
below.
Overview of the Spring Framework
Core ContainerThe Core
Container consists of the Core, Beans, Context and
Expression modules.The Core and
Beans modules provide the most fundamental parts of
the framework and provides the IoC and Dependency Injection features.
The basic concept here is the BeanFactory which
provides a sophisticated implementation of the factory pattern. It
removes the need for programmatic singletons and allows you to decouple
the configuration and specification of dependencies from your actual
program logic.The Context
module build on the solid base provided by the Core and Beans
modules: it provides a way to access objects in a framework-style manner
in a fashion somewhat reminiscent of a JNDI-registry. The Context module
inherits its features from the Beans module and adds support for
internationalization (I18N) (using for example resource bundles),
event-propagation, resource-loading, and the transparent creation of
contexts by, for example, a servlet container. The Context module also
contains support for some Java EE features like EJB, JMX and basic
remoting support. The ApplicationContext
interface is the focal point of the Context module that provides these
features.The Expression Language module provides a
powerful expression language for querying and manipulating an object
graph at runtime. It can be seen as an extension of the unified
expression language (unified EL) as specified in the JSP 2.1
specification. The language supports setting and getting of property
values, property assignment, method invocation, accessing the context of
arrays, collections and indexers, logical and arithmetic operators,
named variables, and retrieval of objects by name from Spring's IoC
container. It also supports list projection and selection, as well as
common list aggregators.Data Access/IntegrationThe 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 to do tedious JDBC coding
and parsing of database-vendor specific error codes.The ORM module
provides integration layers for popular object-relational mapping APIs,
including JPA, JDO, Hibernate, and iBatis. Using the ORM package you can use
all those O/R-mappers in combination with all the other features Spring
offers, such as the simple declarative transaction management feature
mentioned previously.The OXM module provides an abstraction
layer for using a number of Object/XML mapping implementations.
Supported technologies include JAXB, Castor, XMLBeans, JiBX and
XStream.The JMS module provides Spring's
support for the Java Messaging Service. It contains features for both
producing and consuming messages.The Transaction module provides
a way to do programmatic as well as declarative transaction management,
not only for classes implementing special interfaces, but for
all your POJOs (plain old Java objects).WebThe Web layer consists of the Web,
Web-Servlet and Web-Portlet modules.Spring's Web module provides basic
web-oriented integration features, such as multipart file-upload
functionality, the initialization of the IoC container using servlet
listeners and a web-oriented application context. It also contains the
web related parts of Spring's remoting support.The Web-Servlet module provides Spring's
Model-View-Controller (MVC)
implementation for web-applications. Spring's MVC framework is not just
any old implementation; it provides a clean
separation between domain model code and web forms, and allows you to
use all the other features of the Spring Framework.The Web-Portlet module provides the MVC
implementation to be used in a portlet environment and mirrors what is
provided in the Web-Servlet module.AOP and InstrumentationSpring's AOP module
provides an AOP Alliance-compliant aspect-oriented
programming implementation allowing you to define, for example,
method-interceptors and pointcuts to cleanly decouple code implementing
functionality that should logically speaking be separated. Using
source-level metadata functionality you can also incorporate all kinds
of behavioral information into your code, in a manner similar to that of
.NET attributes.There is also a separate Aspects module that
provides integration with AspectJ.The Instrumentation module provides class
instrumentation support and classloader implementations to be used in
certain application servers.TestThe Test module contains the Test Framework
that supports testing Spring components using JUnit or TestNG. It
provides consistent loading of Spring ApplicationContexts and caching of
those contexts. It also contains a number of Mock objects that are usful
in many testing scenarios to test your code in isolation.Usage scenariosWith the building blocks described above you can use Spring in all
sorts of scenarios, from applets up to fully-fledged enterprise
applications using Spring's transaction management functionality and web
framework integration.
Typical full-fledged Spring web
application
By using Spring's declarative transaction management
features the web application is fully transactional, just as it
would be when using container managed transactions as provided by
Enterprise JavaBeans. All your custom business logic can be implemented
using simple POJOs, managed by Spring's IoC container. Additional services
include support for sending email, and validation that is independent of
the web layer enabling you to choose where to execute validation rules.
Spring's ORM support is integrated with JPA, Hibernate, JDO and iBatis;
for example, when using Hibernate, you can continue to use your existing
mapping files and standard Hibernate
SessionFactory configuration. Form
controllers seamlessly integrate the web-layer with the domain model,
removing the need for ActionForms or other classes
that transform HTTP parameters to values for your domain model.
Spring middle-tier using a third-party web
framework
Sometimes the current circumstances do not allow you to completely
switch to a different framework. The Spring Framework does
not force you to use everything within it; it is not
an all-or-nothing solution. Existing front-ends built
using WebWork, Struts, Tapestry, or other UI frameworks can be integrated
perfectly well with a Spring-based middle-tier, allowing you to use the
transaction features that Spring offers. The only thing you need to do is
wire up your business logic using an
ApplicationContext and integrate your web layer
using a WebApplicationContext.
Remoting usage scenario
When you need to access existing code via web services, you can use
Spring's Hessian-, Burlap-,
Rmi- or JaxRpcProxyFactory
classes. Enabling remote access to existing applications suddenly is not
that hard anymore.
EJBs - Wrapping existing POJOs
The Spring Framework also provides an access-
and abstraction- layer for Enterprise JavaBeans, enabling you to
reuse your existing POJOs and wrap them in Stateless Session Beans, for
use in scalable, failsafe web applications that might need declarative
security.