Spring Introduction
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
- dist - Contains spring framework.jar
- docs - Contains documents.
- lib - Contains all dependent libraries.
- src - Contains source code.
Spring Hello World Program
Let's write a simple Hello world program using the spring way:
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 which has a method print message
public class HelloWorld {
public void printMessage(){
System.out.println("Hello World");
}
}Write the Spring configuration file. We will 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>
Write the main program
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.
HelloWorldMessage:
public class HelloWorldMessage {
public String getMessage(){
return "HelloWorld";
}
}And our HelloWorld class would look like
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.
Please follow the video to how to make the application:
These are basic premises of Inversion of Control(IOC) or DI(Dependency Injection). We will not get into theoretical debate here. But 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.
Spring Framework Index Java Home Home
Sidebar
Sidebar
Random Pages
- Comprehensive Overview of Java Technologies
- SOAP
- Pune Hill Forts For Trekking
- Windows Antivirus
- Source Code Control Systems
- JavaFX Data Binding and Trigger
- Introduction to Seam
- CSS - Handling Browser Moods
- Parsers Introduction
- Spring Annotation driven Aspects
- JAXB Validation using Schema
- Spring POJO Aspects
- What markets work on?
- Why projects fail?
- Bharat Band - Jai ho
- The concept of Nation
- Don't hide complexity if it cannot be handled in a robustway
Last blog post comments
-
Bharat Band - Jai ho: How do we protest?
Wed 18 of Aug., 2010 13:13 IST
-
Divided by Destiny: Contact
Fri 23 of July, 2010 16:02 IST
-
Future of Java: thesis writing
Sat 17 of July, 2010 01:50 IST
-
Hang till Death Mr. Kasab: some change
Mon 28 of June, 2010 16:03 IST
-
God Religion : Why we are confused?: Re: Is GOD Necessary?
Tue 15 of June, 2010 17:29 IST
-
God Religion : Why we are confused?: Is GOD Necessary?
Tue 15 of June, 2010 13:06 IST
-
The reason in religion: good
Wed 10 of Mar., 2010 18:30 IST
-
The confusion of Design Patterns: I think at macro level you are right...
Tue 23 of Feb., 2010 03:31 IST
-
The Indian Municipality: Comment
Fri 22 of Jan., 2010 13:20 IST
-
What Government should do?: Re: Review of the Indian Law and Order and Justice Dispensation regime.
Fri 22 of Jan., 2010 13:16 IST
Last wiki comments