| home / programming / spring / 1 | [previous] |
|
|
The GreetingServiceImpl class has a single property: the greeting property. This
property is simply a String that holds the text that is the message that will be
printed when the sayGreeting() method is called. You may have noticed that the
greeting can be set in two different ways: by the constructor or by the property’s
setter method.
What’s not apparent just yet is who will make the call to either the constructor
or the setGreeting() method to set the property. As it turns out, we’re going to let
the Spring container set the greeting property. The Spring configuration file
(hello.xml) in listing 1.3 tells the container how to configure the greeting service.
| Listing 1.3 Configuring Hello World in Spring |

The XML file in listing 1.3 declares an instance of a GreetingServiceImpl in the
Spring container and configures its greeting property with a value of “Buenos
Dias!” Let’s dig into the details of this XML file a bit to understand how it works.
At the root of this simple XML file is the element, which is the root element
of any Spring configuration file. The element is used to tell the
Spring container about a class and how it should be configured. Here, the id
attribute is used to name the bean greetingService and the class attribute specifies
the bean’s fully qualified class name.
Within the element, the element is used to set a property, in
this case the greeting property. By using , we’re telling the Spring
container to call setGreeting() when setting the property.
The value of the greeting is defined within the element. Here we’ve
given the example a Spanish flair by choosing “Buenos Dias” instead of the traditional
“Hello World.”
The following snippet of code illustrates roughly what the container does when instantiating the greeting service based on the XML definition in listing 1.3:2
GreetingServiceImpl greetingService = new GreetingServiceImpl();
greetingService.setGreeting("Buenos Dias!");
Similarly, we may choose to have Spring set the greeting property through
GreetingServiceImpl’s single argument constructor. For example:
Similarly, we may choose to have Spring set the greeting property through GreetingServiceImpl’s
single argument constructor. For example:

The following code illustrates how the container will instantiate the greeting service
when using the
The last piece of the puzzle is the class that loads the Spring container and uses it to retrieve the greeting service. Listing 1.4 shows this class.
2 The container actually performs other activities involving the life cycle of the bean. But for illustrative purposes, these two lines are sufficient.
| Listing 1.4 The Hello World main class |

The BeanFactory class used here is the Spring container. After loading the
hello.xml file into the container, the main() method calls the getBean() method
on the BeanFactory to retrieve a reference to the greeting service. With this reference
in hand, it finally calls the sayGreeting() method. When we run the Hello
application, it prints (not surprisingly)
Buenos Dias!
This is about as simple a Spring-enabled application as we can come up with.
But it does illustrate the basics of configuring and using a class in Spring.
Unfortunately, it is perhaps too simple because it only illustrates how to configure
a bean by injecting a String value into a property. The real power
of Spring lies in how beans can be injected into other beans using IoC.
Next week, we begin with Understanding inversion of control.
Spring in Action: A Spring Jump Start is written by Craig Walls and Ryan Breidenbach and reproduced from "Spring in Action" by permission of Manning Publications Co. ISBN 1932394354, copyright 2005. All rights reserved. See http://www.manning.com for more information.| home / programming / spring / 1 | [previous] |
Created: March 27, 2003
Revised: March 07, 2005
URL: http://webreference.com/programing/spring/1