by January 23, 2006 0 comments

Spring enables POJO-based programming model, allowing
developers to concentrate more on business logic, thus, reducing EJB

Spring is the most popular amongst the new breed of
lightweight containers. Spring proposes a new paradigm, a pluggable,
non-intrusive and robust framework. It is a framework that allows plug-in of
other technologies; lets you concentrate on developing the business
functionality of your application rather than the intricacies of technology
specific APIs; and allows software components to be first developed and tested
in isolation, then scaled up for deployment in any environment in a consistent
and transparent fashion.

Direct Hit!
Applies to:
Java developers
Develop lightweight enterprise applications without using EJB
Primary Link:
Google keywords:
Spring, Spring framework, Dependency Injection, IoC
On PCQXtreme DVD :

In this article we are going to develop an application
called MedTracker, based on open-source Java projects, that will provide an
interface for admitting a new patient.  It is based on open-source Java
projects. We’ll use Spring as the application framework,

Tomcat as the Servlet container, Ant for deployment and
MySQL for data persistence. Let’s begin by demonstrating how to develop
loosely coupled components (POJOs) via DI design pattern, and then inject
dependencies through Spring. Although, Spring provides its own version of MVC
architecture, for simplicity we’ll use our own custom designed MVC pattern.

Development environment
We’ll organize the directory structure similar to the Spring sample
applications. These are the directories that we’ll use:

src: This is the home for all
of the source code in the application.
war: The WAR file is the
typical deployable unit for the Web  application. All the Spring’s
configuration files and the Web application DD (Deployment Descriptor), web.xml,
will go here.

Dependency injection
Liberal use of interfaces is
the mantra of DI (Dependency Injection) pattern. This
allows the developer to implement business logic in POJO (Plain
Old Java Objects), resulting in increased productivity. Since
business logic is implemented in POJOs, you no longer need to deploy your object to a
heavyweight container in order to use it in unit tests. DI 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. Now let’s look at the source-code files. This is a peculiar POJO-bean which follows
standard JavaBeans naming conventions and provides accessor and mutator methods. This is the business interface or
the service that declares following two methods.

List getPatients();
void admitAPatient(Patient particulars); This is the implementation class
that encapsulates the business logic. This class is actually responsible for
registering a new patient by firing an SQL query and returning a list of all
patients. This is the client class. The
client has a property that accepts a service as shown below.

public class
private RegisterAPatient rap;

The service is actually wrapped in an interface. The client
can’t see the implementation of the service. Please note that the DI pattern
is not Spring (or any other platform) specific.

Spring in action
With Spring, there’s no need to locate and instantiate POJOs using service
locators or JNDI calls. The Assembler (Spring framework in this case) takes care
of finding these classes based on the information provided in the descriptor and
makes them available to the calling class.

SpringAssembler With DI, a third-party called
Assembler, creates both the client and service, and then sets the value of a
Service to satisfy the dependency. Here we are using a servlet, it instantiates
both the client and the service as shown in the code snippet below.

ctx = new ClassPathXmlApplicationContext("Patient-Config.xml");
PatientRecordInserter pri = (PatientRecordInserter)ctx.getBean("recordInserter");

This is also the controller servlet that accepts HTTP
request from the browser, instantiates the beans which implement the business
logic, saves the result as a request parameter and forwards the request to a JSP

patientsview.jsp: This is a JSP view which extracts list of
the patients as a request parameter and displays all the patient names.

There are other miscellaneous files like the DD web.xml
file, where we have mapped the servlet for request handling and register.html
which provides an interface to the client for entering new patient’s


Building an MedTracker appllication using Ant takes just a single step. Deployment is another single step copy operation

Spring configuration
The configuration file is used to describe the beans. The essence of Spring
is that we don’t directly connect our components and services in code, rather
describe which services are needed by which components through configuration
files. In Spring, injected properties-or references to other beans are
configured via an XML file, making configuration almost entirely trivial. The
configuration file is placed inside the WEB-INF/classes directory.

<bean id="registerPatient" class="com.pcquest.PatientRegistrar"
<bean id="recordInserter" class="com.pcquest.PatientRecordInserter">
<property name="rap"><ref bean="registerPatient"/></property>

Data access through Spring
Spring comes with a family of data access frameworks that integrate well
with a variety of data access technologies such as JDBC, JDO and ORM (Object
Relational Mapping) such as iBatis, Hibernate, etc. Spring comes with a very
lightweight data source implementation class called DriverManagerDataSource that
we’ll configure in the Spring configuration file as shown in the code below.

<bean id="dataSource"

<value>jdbc:odbc: patientAdmitter </value>

Ant built the system in one automated step. The Ant build script is placed
in the root folder of our project and fired by specifying ‘ant’ on the
command prompt.

With Spring you can develop rich enterprise applications and deploy them
without paying for an application server. Spring has now become incumbent in the
lightweight container space and has developed a rich ecosystem around it. Java
can now compete in entry-level Web application architecture with PHP and other
technologies like Ruby.

Having made inroads into the enterprise application
development market, Spring is believed to foster the server side middleware
development in years to come. So, get a smell of the Spring framework this
summer before it’s too late.

Kunal Jaggi

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.

Your data will be safe!Your e-mail address will not be published. Also other data will not be shared with third person.