Chinaunix首页 | 论坛 | 博客
  • 博客访问: 835235
  • 博文数量: 756
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 4980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:40
文章分类

全部博文(756)

文章存档

2011年(1)

2008年(755)

我的朋友

分类:

2008-10-13 16:10:53

Spring in Action: A Spring Jump Start. Part 1
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抯 setter method.

What抯 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抮e 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 揃uenos Dias!?Let抯 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抯 fully qualified class name.

Within the element, the element is used to set a property, in this case the greeting property. By using , we抮e telling the Spring container to call setGreeting() when setting the property.

The value of the greeting is defined within the element. Here we抳e given the example a Spanish flair by choosing 揃uenos Dias?instead of the traditional 揌ello World.?/p>

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抯 single argument constructor. For example:

Similarly, we may choose to have Spring set the greeting property through GreetingServiceImpl抯 single argument constructor. For example:

 

The following code illustrates how the container will instantiate the greeting service when using the element:

    GreetingServiceImpl greetingService =
        new GreetingServiceImpl(揃uenos Dias?;
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 for more information.


--------------------next---------------------

阅读(365) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~