javawaveblogs-20

Monday, December 3, 2007

Spring - Part I

Here is the simple example to start up with spring,

To write a simple spring application we need one interface, one implementation of that interface and a test client to test the implementation. Apart form that we also need a configuration XML file.

One interface --> Hello.java
One implementation --> HelloImpl.java
One Test Client --> HelloClient.java
One Spring XML File --> Hello.xml (Should be in the classpath).

Now let us see the interface Hello.java.


package com.javawave.spring.cli;

/**
* @author dhanago
*/
public interface Hello
{
/**
* This method will return the salutation for the name passed as input
* param to it.
*
* @param name
* @return
*/
public String sayHello( String name );
}


The above interface has only one method to say hello. This method has to be implemented in the implementation class "HelloImpl.java"


package com.javawave.spring.cli;

/**
* @author dhanago
*/
public class HelloImpl implements Hello
{

private String greet;

/**
* zero-arg constructor
*/
public HelloImpl()
{

}

/**
* @param greet
*/
public HelloImpl( String greet )
{
this.greet = greet;
}

/*
* (non-Javadoc)
*
* @see com.javawave.spring.cli.Hello#sayHello(java.lang.String)
*/
public String sayHello( String name )
{

return this.greet + name;
}

/**
* @param greet the greet to set
*/
public void setGreet( String greet )
{
this.greet = greet;
}

}

The Impl class has a property called greet. This also has a setter method "setGreet" to set the value for this property.

Now let us see the spring xml. This XML file will be used for creating the objects in spring framework. This XML is also called wiring xml. Through this XML we will set the greet property of the HelloImpl class.

Hello.XML
~~~~~~~~~


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="hello"
class="com.javawave.spring.cli.HelloImpl">
<property name="greet">
<value>Good Morning!...</value>
</property>
</bean>
</beans>

Here in the XML you can see the tag called bean which will map to the bean what you are using in the application. It has an attribute called "id" and a attribute called "class". The "class" attribute maps to the HelloImpl.java in our case. The property tag inside the bean tag maps to the property inside the bean class. Here in our case it is the "greet" property. The value for the property is given in the value element inside the property element. This is the value passed to the bean's property "greet" . This is how spring does the dependency injection (DI). Here what you are doing is injecting dependency through setter method. This is also called Setter Injection.

Now we will see the client application "HelloClient.java" to test the "HelloImpl.java".

/**
*
*/
package com.javawave.spring.cli;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
* @author dhanago
*/
public class HelloClient
{

/**
* @param args
*/
public static void main( String[] args )
{
try
{
System.out.println( "Inside main of HelloClient.." );
Resource resource = new ClassPathResource(
"com/javawave/spring/cli/Hello.xml" );
BeanFactory factory = new XmlBeanFactory( resource );
Hello hello = (Hello) factory.getBean( "hello" );
String result = hello.sayHello( "Man" );
System.out.println( result );
}
catch (Exception e)
{
System.out.println( "Exception/Error:" + e.toString() );
}
}
}

This client application uses ClassPathResource to load the resource. In our case Hello.xml and it creates the factory via XmlBeanFactory. factory class has getBean method which will return a object. Here we get Hello object. So from that we can call sayHello method as shown in the above code and get the result.

The Jars used for this application is:
spring.jar
commons-logging.jar

The Output is shown below:

Inside main of HelloClient..
Good Morning!...Man
Dec 3, 2007 1:59:01 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/javawave/spring/cli/Hello.xml]

diggthis