<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    yeafee@夜飛郎

    it's usually better to solve problems with simplicity and finesse rather than muscle.

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      33 隨筆 :: 3 文章 :: 2 評論 :: 0 Trackbacks
    Spring jump start

    Spring-enabled applications are like any Java application. They are made up of  several classes, each performing a specific purpose within the application. What makes Spring-enabled applications different, however, is how these classes are configured and introduced to each other. Typically, a Spring application has an XML file that describes how to configure the classes, known as the Spring configuration file.
    Listing 1.1
    The GreetingService interface separates the service's  implementation from its interface.   
    package com.springinaction.chapter01.hello;
    public interface GreetingService {
    public void sayGreeting();
    }

    Listing 1.2 GreetingServiceImpl.java: Responsible for printing the greeting                        
    package com.springinaction.chapter01.hello;
    public class GreetingServiceImpl implements GreetingService {
    private String greeting;
    public GreetingServiceImpl() {}
    public GreetingServiceImpl(String greeting) {
    this.greeting = greeting;
    }

    public void sayGreeting() {
    System.out.println(greeting);
    }

    public void setGreeting(String greeting) {
    this.greeting = greeting;
    }

    }

    Noticed that the greeting can be set in two different ways: by the constructor or by the property’s setter method.

    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                                                                      

    <?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="greetingService"
    class
    ="com.springinaction.chapter01.hello.GreetingServiceImpl">
    <property name="greeting">
    <value>Buenos Dias!</value>
    </property>
    </bean>
    </beans>

    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 <beans> element, which is the root element of any Spring configuration file. The <bean> 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.

    xml文件的<value>元素定義了javabean之 greeting的值.下邊的一段代碼粗略的闡明了基于xml1.3文件的內容,容器都做了些什么.
    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:
    <bean id="greetingService"
    class
    ="com.springinaction.chapter01.hello.GreetingServiceImpl">
    <constructor-arg>
    <value>Buenos Dias!</value>
    </constructor-arg>
    </bean>

    The following code illustrates how the container will instantiate the greeting service
    when using the <constructor-arg> element:
    GreetingServiceImpl greetingService =new GreetingServiceImpl(“Buenos 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.
    Listing 1.4 The Hello World main class                                                                                

    package com.springinaction.chapter01.hello;
    import java.io.FileInputStream;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    public class HelloApp {
    public static void main(String[] args) throws Exception {
    BeanFactory factory 
    =
    new XmlBeanFactory(new FileInputStream("hello.xml"));
    GreetingService greetingService 
    =
    (GreetingService) factory.getBean(
    "greetingService");
    greetingService.sayGreeting();
    }

    }

    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!

    The real power of Spring lies in how beans can be injected into other beans using IoC.
    see next........continue!
    posted on 2005-12-13 14:56 @yeafee 閱讀(325) 評論(0)  編輯  收藏 所屬分類: OpenSource
    主站蜘蛛池模板: 亚洲女人初试黑人巨高清| 亚洲国产老鸭窝一区二区三区| 激情内射亚洲一区二区三区爱妻| 99热这里只有精品免费播放| 亚洲一区二区中文| 麻豆高清免费国产一区| 91亚洲国产成人久久精品网址| 中文字幕免费高清视频| 亚洲午夜久久久久久尤物| 成人免费一区二区无码视频| 一本色道久久88—综合亚洲精品| 在线免费观看视频你懂的| 妇女自拍偷自拍亚洲精品| 亚洲精品第一国产综合精品99 | 国产无遮挡色视频免费视频| 亚洲乱亚洲乱妇24p| 四虎永久成人免费影院域名| 免费的黄色网页在线免费观看| 亚洲人成色7777在线观看不卡| 国产免费拔擦拔擦8X高清在线人 | 亚洲欧洲日产国码无码久久99| 中国好声音第二季免费播放| 亚洲人成亚洲精品| 免费黄色网址入口| 精品久久久久久无码免费| 亚洲综合亚洲国产尤物| 午夜无遮挡羞羞漫画免费| 丰满少妇作爱视频免费观看| 亚洲精品视频在线| 好吊妞在线新免费视频| 巨胸喷奶水www永久免费| 91亚洲自偷手机在线观看| 日韩免费a级在线观看| 精品一区二区三区高清免费观看| 亚洲精品456在线播放| 精品免费国产一区二区三区 | 亚洲精品成人无限看| 97视频免费在线| 一区二区三区在线免费观看视频| 亚洲精品视频专区| 亚洲AV中文无码乱人伦在线视色|