Datasheet
1. Change the directory to the source directory, and then compile the code using Maven 2:
cd src\chapter1\springfirst
mvn compile
This may take a little while, since Maven 2 will download all the Spring libraries that you need
from the global repository. Once the libraries are downloaded, Maven 2 keeps them in your local
repository on your computer’s hard disk, and you will not have to wait for them again.
2. To run the Springwired version of the modularized application, the CalculateSpring class,
use the following Maven 2 command:
mvn exec:java -Dexec.mainClass=com.wrox.begspring.CalculateSpring –Dexec.args=”3000 3”
3. Your output from this Spring-wired application should be:
The result of 3000 times 3 is 9000!
How It Works
The ability to easily wire and rewire reusable Java beans for an application is central to the flexibility
offered by the Spring framework.
The
CalculateSpring main class, instead of instantiating concrete instances of Operation or
ResultWriter, delegates this task to the Spring container. The Spring container in turn reads your
configuration file, called the bean descriptor file (or the context descriptor).
In the
CalculateSpring class, shown here, the highlighted code hooks into the Spring container and
tells it to perform the task of wiring together the beans:
package com.wrox.begspring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class CalculateSpring {
private Operation ops;
private ResultWriter wtr;
public void setOps(Operation ops) {
this.ops = ops;
}
public void setWriter(ResultWriter writer) {
this.wtr = writer;
}
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext(
“beans.xml”);
10
Chapter 1: Jump Start Spring 2
01612c01.qxd:WroxPro 10/31/07 10:42 AM Page 10










