This is a simplified version of the "hello world" from
Spring in Action, 2nd Ed. (p 11-12), done all "by hand". The example in the book is straight forward enough, but doing it at the command line from scratch took a me few attempts. I'll run through this from a Linux perspective, but it's nearly identical on Windows.
1) Get the Spring Framework
All you need is the spring jar file. If you have netbeans, skip this step -- we'll use the jar file from NetBeans later (i.e. netbeans-6.5/java2/modules/ext/spring/spring-2.5.jar). Otherwise,
download the latest spring framework jar file. You're looking for the spring-x.y.jar file.
2) Create a folder structure
Create a folder structure for your java source files. For this example, use
com/rootsilver/spring. You can create this in one shot with:
mkdir -p com/rootsilver/spring
3) Create your Bean (hint: it's just a simple class)
Don't worry about the "bean" naming nonsense. Create the class com/rootsilver/spring/HelloBean.java:
//file: com/rootsilver/spring/HelloBean.java
package com.rootsilver.spring;
public class HelloBean{
public String greeting;
public void sayGreeting(){
System.out.println(this.greeting);
}
public void setGreeting(String greeting){
this.greeting = greeting;
}
}
4) Create your main class
Now, create the main class under com/rootsilver/spring/Test.java:
//file: com/rootsilver/spring/Test.java
package com.rootsilver.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class Test{
public static void main(String[] args) throws Exception{
FileSystemResource resource = new FileSystemResource("hello.xml");
BeanFactory factory = new XmlBeanFactory(resource);
HelloBean helloBean = (HelloBean)factory.getBean("HelloBean");
helloBean.sayGreeting();
}
}
We now have a basic class (HelloBean.java) and a main class (Test.java) that
leverages Spring to create an instance of our test class. We wire this all together with an XML configuration file.
5) Create your configuration file
This XML file, which the main class (Test.java) passes into spring via the XmlBeanFactory, holds the whole thing together:
//file: hello.xml (in the root of the project)
<?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="HelloBean" class="com.rootsilver.spring.HelloBean">
<property name="greeting">
<value>Hello World</value>
</property>
</bean>
</beans>
In the
tag, note that the class is the fully qualified class: package name + class name. This is probably patently obvious to Java folks, but it's something .NET folks tend to miss. Also, in Test.java, notice we're creating the bean using .getBean, referencing the bean by its ID as defined in hello.xml
6) Compile and run
This is actually a tricky step if you aren't used to building java apps at the command line. I always mess up the dots and slashes. You need to make sure you've got the spring jar file in your classpath.
javac -classpath "/path/to/spring-2.5.jar" com/rootsilver/spring/*.java
Spring apparently depends on the commons-logging jar. So to run it, with with spring jar file, commons-logging, and "." in your path, executing com.rootsilver.spring/Test:
java -classpath ".:/path/to/spring-2.5.jar:/path/to/commons-logging-1.1.jar" com.rootsilver.spring.Test
Results:
Feb 16, 2009 11:09:39 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [/home/jknight/code/RandomSampleCode/Spring/hello.xml]
Hello World
7) Download Source
Spring.rootsilver.zip