Hello Spring
Summary
Spring is an open-source framework, created by Rod Johnson. Put simply, Spring is a lightweight inversion of control and aspect-oriented container framework.
Tools Used
Spring Framework 2.0.6
Eclipse IDE 3.3.0
Java Runtime Environment 1.6.0_02
Assumptions
Latest version of Java Development Kit (JDK) and Eclipse IDE are installed and are working fine.
Initial Setup
1. Download Spring Framework from here.
2. Extract spring-framework-2.0.6.zip
3. Create a new Java Project in Eclipse named HelloSpring
4. Open the project Properties window for the current project and go to Java Build Path.
5. Add spring.jar which is located in the spring local folder/dist to your build path.

Example
Create a package under Hello Spring called, for example, com.tw.hellospring

Problem Statement
Developing a simple Greeting Service which greets users and also includes a Translator, which helps is providing the proper salutation taking the language of preference into consideration.
Description
Following the tradition of Test Driven Development, we like to present the unit tests for the Greeting Service we are developing.
"GreetingServiceImplTest.java"
package org.helloopensource.hellospring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import junit.framework.TestCase;
public class GreetingServiceImplTest extends TestCase {
public void testShouldUseValidGreetingService() throws Exception {
assertNotNull(createGreetingBean());
}
public void testShouldReturnGreetingInFrench() throws Exception {
GreetingServiceImpl greetingService = createGreetingBean();
assertEquals("Bonjour Santhosh", greetingService.sayGreeting("Santhosh", "French"));
}
public void testShouldReturnGreetingInHindi() throws Exception {
GreetingServiceImpl greetingService = createGreetingBean();
assertEquals("Namaste John", greetingService.sayGreeting("John", "Hindi"));
}
public void testShouldReturnGreetingInItalian() throws Exception {
GreetingServiceImpl greetingService = createGreetingBean();
assertEquals("Ciao Sachin", greetingService.sayGreeting("Sachin", "Italian"));
}
private GreetingServiceImpl createGreetingBean() {
BeanFactory factory = new XmlBeanFactory(new FileSystemResource("GreetingServiceXml.xml"));
GreetingServiceImpl greeting = (GreetingServiceImpl) factory.getBean("greetingService");
return greeting;
}
}
The test consists of 5 test cases such as, ShouldUseValidGreetingService();/i> and testShouldReturnGreetingInItalian(). Others are similar to the one mentioned. One notable method in the test class is the createGreetingBean which provides the test cases with an object of "GreetingServiceImpl" to work with.
ShouldUseValidGreetingService() asserts if a valid greetingService bean is created upon instantiation.
testShouldReturnGreetingInItalian() and the various other forms of the same will check if the greetingService is working as it is suppose to do i.e. translating any salutation into its respective language and greeting the user.
Another important method, createGreetingBean() which is private for the class, is used to reduce code duplication. It loads the bean information from the XML configuration file, more of which is explained a bit later, and gets a bean which can be used by other methods. For more information on Java Beans, click here.
"GreetingServiceImpl.java"
package org.helloopensource.hellospring;
/* Implementation of GreetingService Interface with Translation */
public class GreetingServiceImpl {
private String salutation;
private ITranslator translator;
public GreetingServiceImpl() {
}
public void setTranslator(ITranslator translator) {
this.translator = translator;
}
public String sayGreeting(String name, String toLanguage) {
salutation = translator.translate(toLanguage);
return salutation + " " + name;
}
}
The "GreetingServiceImpl" definition is pretty simple. It consists of two methods, setTranslator(ITranslator) and sayGreeting(String, String). Also, it has two private attributes, namely, salutation which holds the salutation which is put up based on the language and translator which is the concrete implementation of the ITranslator interface.
The setTranslator(ITranslator) method assigns a TranslatorImpl instance which is used by the greeting service.
The sayGreeting(String, String) method takes in two String params wherein, the first indicates the user's name and the second, the language that is used to greet.
"ITranslator.java"
package org.helloopensource.hellospring;
/* Translator contract */
public interface ITranslator {
public String translate(String toLanguage);
}
The above interface defines the contract for the Translator class. It has a single method declaration which upon implementation, helps in translating salutations from one language to another.
"TranslatorImpl.java"
package org.helloopensource.hellospring;
/* Implementation of Translator Interface */
public class TranslatorImpl implements ITranslator {
public String translate(String toLanguage) {
toLanguage = toLanguage.toLowerCase();
if (toLanguage.equals("hindi"))
return "Namaste";
if (toLanguage.equals("french"))
return "Bonjour";
if (toLanguage.equals("italian"))
return "Ciao";
return "Hello";
}
}
The actual implementation of the ITranslator interface, which defines the method translate(String), which takes in the language that the salutation is needed in and returns the appropriate salutation. By default, it returns "Hello". This serves the basic purpose at this point of time, but more conditional returns can be added if needed.
"GreetingServiceXml.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="Translator" class="org.helloopensource.hellospring.TranslatorImpl" />
<bean id="greetingService" class="org.helloopensource.hellospring.GreetingServiceImpl">
<property name="translator">
<ref bean="Translator" />
</property>
</bean>
</beans>
The XML file serves as the configuration file to help Spring inject dependency during execution. The XML file has the bean ID and the fully qualified path for the appropriate class. As seen above, the greetingService bean is being injected by the Translator bean using the setter methods i.e. setTranslator.

WOOHOO!!!
Comments (0)
You don't have permission to comment on this page.