The Spring Framework
by Benoy Jose
Introduction
Officially the spring season might be over but spring still
seems to be lingering around. If you did not get the smell of
the Spring Framework this summer then you might be missing
something. You might be having your fingers crossed skeptically
about another run of the mill Framework system that we have been
seeing for the last couple of years. Hundreds of open source and
propriety frameworks have sprung all over the place making it
difficult to identify what each framework does, so the
skepticism is understandable, but before we write off spring
like one of those others it might be worth briefly peeking
through it. Spring proposes a new paradigm, a pluggable, non-
intrusive and robust framework.
Summary
Unlike other frameworks and APIs, spring does not impose itself
wholly on to the design of a project. Spring is modular and has
been divided logically into independent packages, which can
function independently. The architects of an application have
the flexibility to implement just a few spring packages and
leave out most of the packages in spring. The "Spring Framework"
would not feel bad with this attitude and on the contrary
encourages users to introduce Spring into existing applications
in a phased manner. So no matter what kind of framework you are
using now Spring will co-exist with it without causing you
nightmares and further more Spring will allow you to choose
specific packages in Spring.
The "Struts" framework is no doubt a good framework to enhance
the ability of the web tier, but the biggest drawback is
the fact that it caters only to the web tier and leaves most of
the Enterprise tier or middle tier to the fancy of the
application architects. The Application architects need to
provide an additional framework to deal with the enterprise tier
and make sure that the new framework integrates well with the
Struts framework. Spring tries to alleviate this problem by
providing a comprehensive framework, which includes an MVC
framework, an AOP integration framework, a JDBC integration
framework, and an EJB integration framework. It also provides
integration modules for major O/R mapping tools like Hibernate
and JDO. Spring provides all these in a modular fashion without
imposing any layer on to the user. Spring is not a take-it-or-
leave-it kind of framework. It tries to seamlessly blend into
the existing framework users have without hindrances. Spring
also provides transaction management support using Java classes,
email support packages using framework classes, web services
support through proxies and many more features like the above.
As mentioned earlier all these packages are optional and spring
does not make any of them mandatory. Spring can seamlessly
integrate with existing applications and provide specific
functionality that you intend to provide with minimal demands
for customization. A user can continue to use Struts for the web
tier and toplink O/R for the database and meanwhile hook spring
to provide e-mail services and web services support. Spring is
based on the Inversion of Control/Dependency Injection pattern
that has been making rounds in message boards all over the
Internet.
Inversion of Control/Dependency Injection
Dependency injection is kind of an Inversion of Control pattern.
It is proposed as an alternative for the Service Locator. Martin
flower discusses the Dependency Injection pattern, Inversion of
Control pattern and the differences it has with the service
locator in an article at his site (See resources below).
Dependency Injection proposes separating the implementation of
an object and the construction of objects that depend on them.
The job of coordinating the implementation and construction is
left to the Assembler code. The object that needs to be
implemented does not need to instantiate the dependent objects
and can rely on the assembler to do the job. The assembler will
gather and instantiate, if necessary, all the dependent objects
and make them available to the implemented object. Since the
assembler does not depend on the code directly changes can be
made to the assembler without any changes to the code. The
assembler gathers the required classes through configuration
files so a change in the assembler only needs changes to the
configuration file. In this case the Assembler code would be
the spring framework.
Since the classes are independent and are integrated through the
assembler, independent testing of each class can be done without
affecting other application codes.
There are three types of Dependency Injections.
Type 1 IOC also called Interface Injection In Type 1 IOC the
injection is done though an interface. The interface will define
the injection method and the implementation class has to
implement this interface and provide concrete implementation for
the injection method.
Type 2 IOC also called Setter Injection In Type 2 IOC the
injection is done via a setter method. Type 2 IOC uses setter
methods to get the dependent classes it needs.
Type 3 IOC also called Constructor Injection. In Type 3 IOC
implementing class defines a constructor to get all its
dependents. The dependent classes are defined in the constructor
arguments.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.