|
Advertisement |
Spring - The Lightweight Container
Posted On July 2, 2010 by Anish S filed under Java
HTML clipboard
Spring is one of the most popular framework out side of the standard. The basic idea that Spring promotes is inversion of control. Spring was first introduced by Rod Johnson in 2004. Spring framework has put the following principals as their mission statement. These statements do not look path breaking at the moment, as most of the newer generation frameworks has bought these ideas. But it was quite path breaking, when they were introduced first time when the world was struggling with EJB2.1 infinite number of interfaces. Now the mission statements:
- J2EE should be easier to use - Take this statement with the amount of code you have to write in EJB2.1 or doing a statement execution against a database.
- It’s best to program to interface, rather than classes. Spring reduces the complexity cost of using interfaces to zero. - This is a basic programming principal and is actually at a higher level than Spring. It's about loose coupling and building services which are bound with contract and not glued tightly with implementations.
- JavaBeans offer a great way of configuring applications - Your class is a plain POJO and do not depends on the framework. This improves the testability of the program.
- OO design is more important than any implementation technology. - Again this comes basically from EJB2.1. The framework forces you to write so many things as part of implementation that you start loosing grip on your Business logic, which is covered more with OO paradigm.
- Checked exceptions are overused in Java. A framework shouldn’t force you to catch exceptions you’re unlikely to be able to recover from.-Checked exceptions are nuisance most of the time. Think if any time in your life you have done anything useful with your SQLException when dealing with JDBC code.
- Testability is essential, and a framework such as Spring should help make your code easier to test. - This again comes from loose coupling in the pieces of the program so that they can be tested independently.
Before we move further download the Spring framework from http://www.springsource.org/download . The examples are tried on 2.5 but are equally valid for any other version. Download spring-framework*-with-dependencies.zip. This includes all the dependent libraries also. When you unzip it, it has following important directories.
- dist - Contains spring framework jar
- docs - Contains documents.
- lib - Contains all dependent libraries.
- src - Contains source code.
Spring Hello World Program
In the true tradition of programming, let's write a simple Hello world program using the spring way. Make a simple Java application in your favourite IDE.
Bring the following jars in library path:
- dist/spring.jar
- lib/log4j/log4j-1.2.14.jar
- lib/jakarta-commons/commons-logging.jar
Write a HelloWorld bean class, which has a method print message
public class HelloWorld {
public void printMessage(){
System.out.println("Hello World");
}
}
Now we will write the Spring configuration file. Let's name it as context.xml. The name can be anything. Put the file somewhere in the classpath.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="helloWorld" class="HelloWorld"/>
</beans>
Now let's write the main program
public static void main(String[] args){
String[] files = {"context.xml"};
//Start the context of Spring
ApplicationContext appContext = new ClassPathXmlApplicationContext(files);
//Get the Helloworld bean from spring context
HelloWorld helloWorld = (HelloWorld)appContext.getBean("helloWorld");
helloWorld.printMessage();
}
If you look into the main program, than we are not creating the HelloWorld object ourself but we ask Spring factory to return us an instance of HelloWorld object. This is the most important aspect of Spring, which is Ability to Create Objects. Spring essentially is a factory which creates objects for us.
Let's now say that we want to decouple the functionality of providing message to HelloWorld and pass that responsibility to a different class called HelloWorldMessage. The class would look like
public class HelloWorldMessage {
public String getMessage(){
return "HelloWorld";
}
}
And our HelloWorld class would look as follows.
public class HelloWorld {
private HelloWorldMessage message;
public void setMessage(HelloWorldMessage message) {
this.message = message;
}
public void printMessage(){
System.out.println(message.getMessage());
}
}
Now HelloWorld has a dependency relationship and it needs an object of HelloWorldMessage. For that we need to modify the context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="helloWorld" class="HelloWorld">
<property name="message" ref="helloWorldMessage"/>
</bean>
<bean id="helloWorldMessage" class="HelloWorldMessage"/>
</beans>
The main class still remains the same. Here what we did is to use spring to build the relationship. This is another important aspect of Spring. To Wire the relationship.
Again to reiterate, the two important things that Spring bring to table is:
* Ability to create objects.
* Ability to build the relationship between objects.
The corollary is that if you are making instance of objects your self or building relationships yourself than you are not using Spring effectively.
These are basic premises of Inversion of Control(IOC) or DI(Dependency Injection). We will not get into theoretical debate here. The details of this can be checked at http://martinfowler.com/articles/injection.html. The important thing to understand is that the control of building objects and wiring relationships is delegated to the environment. You as an application developer go to the Spring container and ask for your object to do your work. The object is given to you created and all relationships set. A video can be watched on Spring at you tube in lalitbhatt123 channel.
Apart from this Spring provides other capabilities which can help in building enterprise grade applications. The other important feature of Spring is that its modular in nature and one pays for only what one uses. The important elements of Spring framework are:
* Aspect Oriented Programming: Spring supports Aspect oriented Programming which is useful in handling the cross cutting concerns of an application.
* Data Access Integration : Spring has integration point which makes handling of Data access frameworks easier. The prominent among them are JDBC, Hibernate, JPA and JMS.
* MVC/Web : Spring has a full featured MVC framework to handle the front end development. Apart from this Spring also supports integration point to other popular web frameworks.
About Author
Lalit Bhatt is a consultant and trainer in Software application development using Java technologies. He can be reached at http://www.lalitbhatt.com or at lalit.bhatt@gmail.com





