Datasheet

Figure 1-1
In Figure 1-1, there is still one problem: the code of
CalculateScreen must be modified and recom-
piled if you need to change the math operation performed or where to display the result. The code that
creates instances of
OpAdd and ScreenWriter is hard-coded in CalculateScreen. Spring can help in
this case, as the next section demonstrates.
Using Spring to Configure a Modularized Application
Figure 1-2 shows graphically how Spring can assist in flexibly interchanging the implementation of the
math operation (say, from
OpAdd to OpMultiply) and/or the implementation of ResultWriter.
The circle in Figure 1-2 is the Spring container. It reads a configuration file, a context descriptor named
beans.xml in this case, and then uses the contained information to wire the components together. The
context descriptor is a kind of a configuration file for creating applications out of components. You will
see many examples of context descriptors throughout this book.
In Figure 1-2, the
CalculateSpring main class does not directly instantiate the operation or
ResultWriter. Instead, it defers to the Spring container to perform this task, the instantiation. The
Spring container reads the
beans.xml context descriptor, instantiates the beans, and then wires them
up according to the configuration information contained in
beans.xml.
You can see the code for all of this in the next section.
Modularized Application
Calculate (application)
Add two numbers
Print output to screen
CalculateScreen (component)
Component logic
Operation
interface
ResultWriter
interface
OpAdd
(component)
ScreenWriter
(component)
Operation
interface
OpMultiply
(component)
FileWriter
(component)
Monolithic Application
ResultWriter
interface
8
Chapter 1: Jump Start Spring 2
01612c01.qxd:WroxPro 10/31/07 10:42 AM Page 8