Spring enables POJO-based programming model, allowing
developers to concentrate more on business logic, thus, reducing EJB
complexities
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.
|
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
pattern
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.
Patient.java: This is a peculiar POJO-bean which follows
standard JavaBeans naming conventions and provides accessor and mutator methods.
RegisterAPatient.java: This is the business interface or
the service that declares following two methods.
List getPatients();
void admitAPatient(Patient particulars);
PatientRegistrar.java: 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.
PatientRecordInserter.java: This is the client class. The
client has a property that accepts a service as shown below.
public class
PatientRecordInserter{
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 Servlet.java: 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.
ClassPathXmlApplicationContext
ctx = new ClassPathXmlApplicationContext("Patient-Config.xml");
PatientRecordInserter pri = (PatientRecordInserter)ctx.getBean("recordInserter");
pri.insertRecords(newcomer);
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
page.
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
particulars.
|
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.
.....
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.
Deployment
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.
Conclusion
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