Refactoring Hello World Application
using Spring Framework
This lab explores the motivation of Spring framework by exploring
step-by-step refactoring processes for the good ol' "Hello World"
sample application. It is highly recommended you do read and
study the presentation slides of the
"Refactoring
HelloWorld Application using Spring Framework" along with this
hands-on lab.
Expected duration: 200 minutes
(excluding homework)
Software Needed
Before you begin, you need to install the following software on your
computer.
- Java Standard Development Kit (JDK™) version
6.0 (download)
- If you already have installed JDK 5.0 or JDK 6.0, you
can skip this.
- NetBeans IDE 6.1 (download)
- Download either "Web &
Java EE" or "All"
bundles.
- When you install NetBeans IDE, it will ask you which JDK
you want to use.
- Spring framework
- The Spring framework library jar files that are needed for
building Spring applications
of
this lab are provided as part of the hands-on zip file under <LAB_UNZIPPED_DIRECTORY>/springhelloworld/lib
directory so there is no need to download them yourself.
- You can also use the NetBeans Spring framework plug-in's
instead of the library jar files. The installation instruction of
the NetBeans Spring framework plug-in's is described as part of LAB-4918:
Spring MVC.
- 4911_springhelloworld.zip (download)
- The zip file contains this document and the lab contents
- Download it and unzip in a directory of your choice
OS platforms you can use
- Windows
- Solaris x86, Solaris Sparc
- Linux
- Mac OS X
Change Log
- June 26th, 2007: Created
- Aug. 26th, 2007: More explanations are added to the exercises.
Things are cleaned up a bit. Homework is clarified a bit.
- Jan. 4th, 2008: Uses Spring framework 2.5
- July 26th, 2008: NetBeans 6.1 and GlassFish v2 are used
Things to do (by Sang Shin)
- Add learning points to each exercise.
Lab Exercises
Exercise 1: Build and run
"HelloWorld" sample application
In this exercise, you are going to build
the good ol' HelloWorld application you typically do. This sample
application will be the base for the several refactorings you will do
in the subsequent exercises initially without using Spring framework's
Dependency Injection (DI) - Exercises from 2 to 5 - and then with
DI - exercises from 6 to 10.
(1.1)
Open, build, and run "HelloWorld" sample application
0. Start NetBeans IDE.
1. Open HelloWorld
NetBeans project.
- Select File->Open Project (Ctrl+Shift+O).
- Observe that he Open Project
dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/springhelloworld/samples
directory.
- Windows: If you unzipped the 4911_springhelloworld.zip
file under C:\handsonlabs
directory,
the directory to which you want
to browse down should be C:\handsonlans\springhelloworld\samples.
- Solaris/Linux: If you unzipped the 4911_springhelloworld.zip
file under $HOME/handsonlabs
directory, the directory to which you want
to browse down should be $HOME/handsonlabs/springhelloworld/samples.
- Select HelloWorld.
- Click Open Project.
- Observe that the HelloWorld
project node appears under Projects tab
window.
2. Build and run HelloWorld project.
- Right-click HelloWorld
project and select Run.
- Observe "Hello World!"
message is displayed in the Output
window. (Figure-1.11 below)

Figure-1.11: Result
(1.2) Look under the
hood of the "HelloWorld" sample application
1. Open and study HelloWorld.java.
- Expand HelloWorld->Source
Packages.
- Expand <default package>
- Double-click HelloWorld.java.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello
World!");
}
}
|
Code-1.21: HelloWorld.java
Summary
In this exercise, you have built
and run "HelloWorld" sample
application in the simplest possible way.
return to the top
Exercise 2: Build and run
"HelloWorldWithCommandLineArguments" sample application
You are going to do your first but still
obvious refactoring, in which you are going to pass command line
arguments to the program.
(2.1)
Open, build, and run "HelloWorldWithCommandLineArguments" sample
application
1. Open HelloWorldWithCommandLineArguments
NetBeans project.
- Select File->Open Project (Ctrl+Shift+O). The Open Project dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/springhelloworld/samples
directory.
- Select HelloWorldWithCommandLineArguments.
- Click Open Project.
The HelloWorldWithCommandLineArguments
project node appears under Projects tab
window.
2. Build and run HelloWorldWithCommandLineArguments project.
- Right-click HelloWorldWithCommandLineArguments
project and select Run.
- Observe "Hello, Peace!"
message is displayed in the Output
window.
(2.2) Look under the
hood of the "HelloWorldWithCommandLineArguments" sample application
1. Study HelloWorldWithCommandLineArguments.java
public class
HelloWorldWithCommandLineArguments {
public static void main(String[] args) {
if(args.length > 0) {
System.out.println(args[0]);
} else {
System.out.println("Hello World!");
}
}
}
|
Code-2.21: HelloWorldWithCommandLineArguments.java
2. Observe that the project passes an argument.
- Right click HelloWorldWithCommandLineArguments
project node and select Properties.
- Select Run on the left
under Categories.
- Observe that the Arguments field
is set with "Hello, Peace!".
(Figure-2.22 below)
- Click OK.

Figure-2.22: Runtime argument
Summary
In this exercise, you have built
and run a ready-to-build-and-run sample
application.
return to
the top
Exercise 3: Build and run
"HelloWorldDecoupled" sample application
(3.1)
Open, build, and run "HelloWorldDecoupled" sample application
1. Open HelloWorldDecoupled
NetBeans project.
- Select File->Open Project (Ctrl+Shift+O). The Open Project dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/springhelloworld/samples
directory.
- Select HelloWorldDecoupled.
- Click Open Project.
The HelloWorldDecoupled
project node appears under Projects tab
window.
2. Build and run HelloWorldDecoupled project.
- Right-click HelloWorldDecoupledInterface
project and select Run.
- Observe "Hello World!"
message is displayed in the Output
window.
(3.2) Look under the
hood of the "HelloWorldDecoupled" sample application
1.Study HelloWorldDecoupled.java.
public class HelloWorldDecoupled
{
public static void main(String[] args) {
StandardOutMessageRenderer mr = new StandardOutMessageRenderer();
HelloWorldMessageProvider mp = new HelloWorldMessageProvider();
mr.setMessageProvider(mp);
mr.render();
}
}
|
2. StandardOutMessageRenderer.java
public class
StandardOutMessageRenderer {
private HelloWorldMessageProvider messageProvider =
null;
public void render() {
if (messageProvider == null)
{
throw new RuntimeException(
"You must set the property messageProvider of class:"
+ StandardOutMessageRenderer.class.getName());
}
System.out.println(messageProvider.getMessage());
}
public void
setMessageProvider(HelloWorldMessageProvider provider) {
this.messageProvider = provider;
}
public HelloWorldMessageProvider
getMessageProvider() {
return this.messageProvider;
}
}
|
3. HelloWorldMessageProvider.java
public class
HelloWorldMessageProvider {
public String getMessage() {
return "Hello World!";
}
}
|
Exercise 4: Build and run
"HelloWorldDecoupledInterface" sample application
(4.1)
Open, build, and run "HelloWorldDecoupledInterface" sample application
1. Open HelloWorldDecoupledInterface
NetBeans project.
- Select File->Open Project (Ctrl+Shift+O). The Open Project dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/springhelloworld/samples
directory.
- Select HelloWorldDecoupledInterface.
- Click Open Project.
The HelloWorldDecoupledInterface
project node appears under Projects tab
window.
2. Build and run HelloWorldDecoupledInterface project.
- Right-click HelloWorldDecoupledInterfaceproject
and select Run.
- Observe "Hello World!"
message is displayed in the Output
window.
(4.2) Look under the
hood of the "HelloWorldDecoupledInterface" sample application
1. HelloWorldDecoupledInterface.java
public class
HelloWorldDecoupledInterface {
public static void main(String[] args) {
MessageRenderer mr = new StandardOutMessageRenderer();
MessageProvider mp = new HelloWorldMessageProvider();
mr.setMessageProvider(mp);
mr.render();
}
}
|
2. StandardOutMessageRenderer.java
public class StandardOutMessageRenderer implements
MessageRenderer {
private MessageProvider messageProvider = null;
public void render() {
if (messageProvider == null)
{
throw new RuntimeException(
"You must set the property messageProvider of class:"
+ StandardOutMessageRenderer.class.getName());
}
System.out.println(messageProvider.getMessage());
}
public void setMessageProvider(MessageProvider
provider) {
this.messageProvider =
provider;
}
public MessageProvider getMessageProvider() {
return this.messageProvider;
}
}
|
3. MessageRenderer.java
public interface MessageRenderer
{
public void render();
public void setMessageProvider(MessageProvider
provider);
public MessageProvider getMessageProvider();
}
|
4. HelloWorldMessageProvider.java
public class HelloWorldMessageProvider implements
MessageProvider {
public String getMessage() {
return "Hello World!";
}
}
|
5. MessageProvider.java
public interface MessageProvider
{
public String getMessage();
}
|
Summary
In this exercise, you have built
and run a ready-to-build-and-run "HelloWorldDecoupledInterface" sample
application.
return to
the top
Exercise 5: Build and run
"HelloWorldDecoupledInterfaceWithFactory" sample application
(5.1)
Open, build, and run "HelloWorldDecoupledInterfaceWithFactory" sample
application
1. Open HelloWorldDecoupledInterfaceWithFactory
NetBeans project.
- Select File->Open Project (Ctrl+Shift+O). The Open Project dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/springhelloworld/samples
directory.
- Select HelloWorldDecoupledInterfaceWithFactory.
- Click Open Project.
The HelloWorldDecoupledInterfaceWithFactory
project node appears under Projects tab
window.
2. Build and run HelloWorldDecoupledInterfaceWithFactory project.
- Right-click HelloWorldDecoupledInterfaceWithFactory
project and select Run.
- Observe "Hello World!"
message is displayed in the Output
window.
(5.2) Look under the
hood of the "HelloWorldDecoupledInterface" sample application
1. HelloWorldDecoupledWithFactory.java
public class
HelloWorldDecoupledWithFactory {
public static void main(String[] args) {
MessageRenderer mr = MessageSupportFactory.getInstance().getMessageRenderer();
MessageProvider mp = MessageSupportFactory.getInstance().getMessageProvider();
mr.setMessageProvider(mp);
mr.render();
}
}
|
2. MessageSupportFactory.java
import java.io.FileInputStream;
import java.util.Properties;
public class MessageSupportFactory {
private static MessageSupportFactory instance =
null;
private Properties props = null;
private MessageRenderer renderer = null;
private MessageProvider provider = null;
private MessageSupportFactory() {
props = new Properties();
try {
props.load(new FileInputStream("msf.properties"));
// get the
implementation classes
String rendererClass = props.getProperty("renderer.class");
String providerClass = props.getProperty("provider.class");
renderer = (MessageRenderer) Class.forName(rendererClass).newInstance();
provider = (MessageProvider) Class.forName(providerClass).newInstance();
} catch (Exception ex) {
ex.printStackTrace();
}
}
static {
instance = new
MessageSupportFactory();
}
public static MessageSupportFactory getInstance() {
return instance;
}
public
MessageRenderer getMessageRenderer() {
return renderer;
}
public
MessageProvider getMessageProvider() {
return provider;
}
}
|
3. msf.properties
# msf.properties
renderer.class=StandardOutMessageRenderer
provider.class=HelloWorldMessageProvider
|
Summary
In this exercise, you have built
and run a ready-to-build-and-run
"HelloWorldDecoupledInterfaceWithFactory" sample
application.
return to
the top
Exercise 6: Build and run
"HelloWorldSpring" sample application
This exercise is the first time you are
going to do refactoring using Dependency Injection (DI) feature of the
Spring framework.
(6.1)
Open, build, and run "HelloWorldSpring" sample application
1. Open HelloWorldSpring
NetBeans project.
- Select File->Open Project (Ctrl+Shift+O).
- Observe that the Open Project
dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/springhelloworld/samples
directory.
- Select HelloWorldSpring.
- Click Open Project.
- You might encounter the following dialog box being displayed. If
you do, click Close.
- Observe that the HelloWorldSpring
project node appears under Projects tab
window.
2. Add Spring
framework library jar files and commons logging library files to the
project.
- You might observe that the icon that represents the SpringInjectSimpleValue project has
a
little yellow triangle. This indicates that the project needs
library files but the IDE could not locate it by itself. (It is because
the
location of the file in your system is different from the one that is
used in creating the project.) So you have to
manually tell the IDE where they are located. If you don't
see the yello triangle, then you can skip this step and proceed to
build and run the project.
- Right click SpringInjectSimpleValue
project node and select Properties.
- Observe Project Properties dialog
box appears.
- Select Libraries under Categories on the left.
- Select all the broken references and select Remove. This will remove all
broken references. (Figure-6.10 below)

Figure-6.10: Remove broken references
- Select Add JAR/Folder on
the right. (Figure-1.61 below)

Figure-6.11: Add jar files to the project
- The Add JAR/Folder
dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/springhelloworld/lib/spring-framework-2.5.
- Select all jar files and click Open. (You
probably do not need all these files for this project but , for the
sake of simplicity, you are choosing them all.) (Figure-1.12 below)

Figure-6.12: Add Spring jar files.
- Select Add JAR/Folder
again in the Project Properties dialog box.
- Observe the Add JAR/Folder
dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/springhelloworld/lib/jakarta-commons.
- Select all jar files and click Open. (Figure-1.13 below)

Figure-6.13: Add commons logging jar files
- Click OK to close the Propject Properties dialog box
Note that you don't need all these jar files for this simple
application but for the sake of simplifying this exercise, you are
adding them all to the project's classpath.
3. Build and run HelloWorldSpring project.
- Right-click HelloWorldSpring
project and select Run.
- Observe "Hello World!" message is displayed in the Output
window.
(6.2) Look under the
hood of the "HelloWorldSpring" sample application
1. HelloWorldSpring.java
import java.io.FileInputStream;
import java.util.Properties;
import org.springframework.beans.factory.BeanFactory;
import
org.springframework.beans.factory.support.DefaultListableBeanFactory;
import
org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
public class HelloWorldSpring {
public static void main(String[] args) throws
Exception {
// get the bean factory
BeanFactory factory =
getBeanFactory();
MessageRenderer mr = (MessageRenderer)
factory.getBean("renderer");
MessageProvider mp = (MessageProvider) factory.getBean("provider");
mr.setMessageProvider(mp);
mr.render();
}
private static BeanFactory getBeanFactory() throws
Exception {
// get the bean factory
DefaultListableBeanFactory
factory = new DefaultListableBeanFactory();
// create a definition reader
PropertiesBeanDefinitionReader rdr = new PropertiesBeanDefinitionReader(
factory);
// load the configuration
options
Properties props = new
Properties();
props.load(new
FileInputStream("beans.properties"));
rdr.registerBeanDefinitions(props);
return factory;
}
}
|
2. beans.properties
# beans.properties
renderer.class=StandardOutMessageRenderer
provider.class=HelloWorldMessageProvider
|
Summary
In this exercise, you have built
and run a ready-to-build-and-run "HelloWorldSpring" sample
application.
return to
the top
Exercise 7: Build and run
"HelloWorldSpringWithDI" sample application
(7.1)
Open, build, and run "HelloWorldSpringWithDI" sample application
1. Open HelloWorldSpringWithDI
NetBeans project.
- Select File->Open Project (Ctrl+Shift+O). The Open Project dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/springhelloworld/samples
directory.
- Select HelloWorldSpringWithDI.
- Click Open Project.
The HelloWorldSpringWithDI
project node appears under Projects tab
window.
2. Add Spring framework library files as described above.
3. Build and run HelloWorldSpringWithDI project.
- Right-click HelloWorldSpringWithDI
project and select Run.
- Observe "Hello World!"
message is displayed in the Output
window.
(7.2) Look under the
hood of the "HelloWorldSpringWithDI" sample application
1. HelloWorldSpringWithDI.java
import java.io.FileInputStream;
import java.util.Properties;
import org.springframework.beans.factory.BeanFactory;
import
org.springframework.beans.factory.support.DefaultListableBeanFactory;
import
org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
public class HelloWorldSpringWithDI {
public static void main(String[] args) throws
Exception {
// get the bean factory
BeanFactory factory =
getBeanFactory();
MessageRenderer mr =
(MessageRenderer) factory.getBean("renderer");
mr.render();
}
private static BeanFactory getBeanFactory() throws
Exception {
// get the bean factory
DefaultListableBeanFactory
factory = new DefaultListableBeanFactory();
// create a definition reader
PropertiesBeanDefinitionReader rdr = new PropertiesBeanDefinitionReader(
factory);
// load the configuration options
Properties props = new Properties();
props.load(new FileInputStream("beans.properties"));
rdr.registerBeanDefinitions(props);
return factory;
}
}
|
2. beans.properties
#Message renderer
renderer.class=StandardOutMessageRenderer
# Ask Spring to assign provider bean to the MessageProvider
property
# of the Message renderer bean
renderer.messageProvider(ref)=provider
#Message provider
provider.class=HelloWorldMessageProvider
|
Summary
In this exercise, you have built
and run a ready-to-build-and-run "HelloWorldSpringWithDI" sample
application.
return to
the top
Exercise 8: Build and run
"HelloWorldSpringWithDIXMLFile" sample application
.
(8.1)
Open, build, and run "HelloWorldSpringWithDIXMLFile" sample application
1. Open HelloWorldSpringWithDIXMLFile
NetBeans project.
- Select File->Open Project (Ctrl+Shift+O). The Open Project dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/springhelloworld/samples
directory.
- Select HelloWorldSpringWithDIXMLFile.
- Click Open Project.
The HelloWorldSpringWithDIXMLFile
project node appears under Projects tab
window.
2. Add Spring framework library files as described above.
3. Build and run HelloWorldSpringWithDIXMLFile
project.
- Right-click HelloWorldSpringWithDIXMLFile
project and select Run.
- Observe "Hello World!"
message is displayed in the Output
window.
(8.2) Look under the
hood of the "HelloWorldSpringWithDIXMLFile" sample application
1. HelloWorldSpringWithDIXMLFile.java
import
org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class HelloWorldSpringWithDIXMLFile {
public static void main(String[] args) throws
Exception {
// get the bean factory
BeanFactory factory =
getBeanFactory();
MessageRenderer mr =
(MessageRenderer) factory.getBean("renderer");
mr.render();
}
private static BeanFactory getBeanFactory() throws
Exception {
// get the bean factory
BeanFactory factory = new
XmlBeanFactory(new FileSystemResource(
"beans.xml"));
return factory;
}
}
|
2. beans.xml
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean
id="renderer" class="StandardOutMessageRenderer">
<property name="messageProvider">
<ref local="provider"/>
</property>
</bean>
<bean
id="provider" class="HelloWorldMessageProvider"/>
</beans>
|
Summary
In this exercise, you have built
and run a ready-to-build-and-run "HelloWorldSpringWithDIXMLFile" sample
application.
return to
the top
Exercise 9: Build and run
"HelloWorldSpringWithDIXMLFileConstructorArgument" sample application
(9.1)
Open, build, and run
"HelloWorldSpringWithDIXMLFileConstructorArgument" sample application
1. Open HelloWorldSpringWithDIXMLFileConstructorArgument
NetBeans project.
- Select File->Open Project (Ctrl+Shift+O). The Open Project dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/springhelloworld/samples
directory.
- Select HelloWorldSpringWithDIXMLFileConstructorArgument.
- Click Open Project.
The HelloWorldSpringWithDIXMLFileConstructorArgument
project node appears under Projects tab
window.
2. Add Spring framework library files as described above.
3. Build and run HelloWorldSpringWithDIXMLFileConstructorArgument
project.
- Right-click HelloWorldSpringWithDIXMLFileConstructorArgument
project and select Run.
- Observe "Hello World!" message is displayed in the Output
window.
(9.2) Look under the
hood of the "HelloWorldSpringWithDIXMLFileConstructorArgument" sample
application
1. HelloWorldSpringWithDIXMLFileConstructorArgument.java
import
org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class HelloWorldSpringWithDIXMLFileConstructorArgument {
public static void main(String[] args) throws
Exception {
// get the bean factory
BeanFactory factory =
getBeanFactory();
MessageRenderer mr =
(MessageRenderer) factory.getBean("renderer");
mr.render();
}
private static BeanFactory getBeanFactory() throws
Exception {
// get the bean factory
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"beans.xml"));
return factory;
}
}
|
2. beans.xml
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean
id="renderer" class="StandardOutMessageRenderer">
<property name="messageProvider">
<ref local="provider"/>
</property>
</bean>
<bean
id="provider" class="ConfigurableMessageProvider">
<constructor-arg>
<value>This is a configurable message</value>
</constructor-arg>
</bean>
</beans>
|
3. ConfigurableMessageProvider.java
public class
ConfigurableMessageProvider implements MessageProvider {
private String message;
public
ConfigurableMessageProvider(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
|
Summary
In this exercise, you have built
and run a ready-to-build-and-run
"HelloWorldSpringWithDIXMLFileConstructorArgument" sample
application.
return to
the top
Homework
Exercise (for people
who
are taking Sang Shin's "Java EE Programming online course")
1.
The homework is to modify the HelloWorldSpringWithDIXMLFile project as described below. (You
might want to create a new project by copying
the HelloWorldSpringWithDIXMLFile
project. You can name
the
homework project in any way you want
but here I am going to call it MyHelloWorldSpringWithDIXMLFile.)
- GreetingProvider interface,
which has a single method called getGreeting()
method
- MyGreetingProvider class
which implements the GreetingProvider
interface - its getGreeting() method returns "Good Morning, World!".
- Modify MessageProvider interface
to have setGreetingProvider(GreetingProvider
provider) and getGreetingProvider()
methods
- Modify HelloWorldMessageProvider
class so that its getMessage()
method gets a message from the getGreeting()
method of the GreetingProvider
object.
- Modify beans.xml
accordingly
- When you build and run the project, it displays "Good Morning, World!"
2
. Send the following files to
j2eehomeworks@sun.com
with Subject
as J2EEHomework-springhelloworld.
- Zip file of the the
MyHelloWorldSpringWithDIXMLFile
NetBeans project. (Someone else
should be able to open and run it as a NetBeans project.) You can
use your favorite zip utility or you can use "jar" utility that comes
with JDK as following.
- cd <parent directory that contains MyHelloWorldSpringWithDIXMLFile
directory>
(assuming you named your project as MyHelloWorldSpringWithDIXMLFile)
- jar cvf MyHelloWorldSpringWithDIXMLFile.zip
MyHelloWorldSpringWithDIXMLFile (MyHelloWorldSpringWithDIXMLFile
should contain nbproject
directory)
- Captured output screen -
name it as J2EEHomework-springhelloworld.gif
orJ2EEHomework-springhelloworld.jpg (or J2EEHomework-springhelloworld.<whatver
graphics format>)
- Any screen capture that shows that your program is working is
good enough.
- If you decide to use
different IDE other than NetBeans, the zip
file should contain all the files that are needed for rebuilding the
project - war file with necessary source files is OK.