Advertisment

Java EE Apps Development with Spring

author-image
CIOL Bureau
Updated On
New Update

Kunal Jaggi

Advertisment

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.

It also 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.

Advertisment
Direct Hit!
Applies to: Java developers
USP: Develop lightweight enterprise applications without using EJB
Primary Link: http://springframework.org/
Google keywords: Spring, Spring framework, Dependency Injection, IoC
On PCQXtreme DVD : system\labs

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:

Advertisment

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.

publive-image

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.

Advertisment

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.

Advertisment

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.

Advertisment

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.

Advertisment

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.

       publive-image
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.

                        sun.jdbc.odbc.JdbcOdbcDriver                                     jdbc:odbc: patientAdmitter            

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.

Source: PCQuest

tech-news