Spring IOC的配置使用(轉)

轉:http://www.cnblogs.com/linjiqin/p/3408306.html

Spring IOC的配置使用

1.1.1 XML配置的結構
通常配置文件結構以下:html

複製代碼
<beans>
    <import resource=」resource1.xml」 />
    <bean id=」bean1」 class=」」></bean>
    <bean name=」bean2」 class=」」></bean>
    <alias alias="bean3" name="bean2" />
    <import resource=」resource2.xml」 />
</beans>
複製代碼

一、<bean>標籤主要用來進行Bean定義;
二、alias用於定義Bean別名的;
三、import用於導入其餘配置文件的Bean定義,這是爲了加載多個配置文件,固然也能夠把這些配置文件構造爲一個數組(new String[] {「config1.xml」, config2.xml})傳給ApplicationContext實現進行加載多個配置文件,那一個更適合由用戶決定;這兩種方式都是經過調用Bean Definition Reader 讀取Bean定義,內部實現沒有任何區別。<import>標籤能夠放在<beans>下的任何位置,沒有順序關係。java

1.1.2 Bean的配置
Spring IoC容器目的就是管理Bean,這些Bean將根據配置文件中的Bean定義進行建立,而Bean定義在容器內部由BeanDefinition對象表示,該定義主要包含如下信息:
●全限定類名(FQN):用於定義Bean的實現類;
●Bean行爲定義:這些定義了Bean在容器中的行爲;包括做用域(單例、原型建立)、是否惰性初始化及生命週期等;
●Bean建立方式定義:說明是經過構造器仍是工廠方法建立Bean;
●Bean之間關係定義:即對其餘bean的引用,也就是依賴關係定義,這些引用bean也能夠稱之爲同事bean或依賴bean,也就是依賴注入。spring

Bean定義只有「全限定類名」在當使用構造器或靜態工廠方法進行實例化bean時是必須的,其餘都是可選的定義。難道Spring只能經過配置方式來建立Bean嗎?回答固然不是,某些SingletonBeanRegistry接口實現類實現也容許將那些非BeanFactory建立的、已有的用戶對象註冊到容器中,這些對象必須是共享的,好比使用DefaultListableBeanFactory 的registerSingleton() 方法。不過建議採用元數據定義。編程

1.1.3 Bean的命名
每一個Bean能夠有一個或多個id(或稱之爲標識符或名字),在這裏咱們把第一個id稱爲「標識符」,其他id叫作「別名」;這些id在IoC容器中必須惟一。如何爲Bean指定id呢,有如下幾種方式:
1、不指定id,只配置必須的全限定類名,由IoC容器爲其生成一個標識,客戶端必須經過接口「T getBean(Class<T> requiredType)」獲取Bean;例如:數組

<bean class=」com.ljq.test.HelloWorldImpl」/>

測試代碼片斷以下:post

複製代碼
@Test
public void sayHello() {
    BeanFactory beanFactory =
              new ClassPathXmlApplicationContext("helloworld.xml");
    //根據類型獲取bean
    HelloService helloService = beanFactory.getBean(HelloService.class);
    helloService.sayHello();
}
複製代碼

 

2、指定id,必須在Ioc容器中惟一;例如:測試

<bean id="helloWorld" class=」com.ljq.test.HelloWorldImpl」/> 

測試代碼片斷以下:ui

複製代碼
@Test
public void sayHello() {
    BeanFactory beanFactory =
              new ClassPathXmlApplicationContext("helloworld.xml");
    //根據id獲取bean 
    HelloService helloService = beanFactory.getBean("helloWorld", HelloService.class);
    helloService.sayHello();
}
複製代碼

3、指定name,這樣name就是「標識符」,必須在Ioc容器中惟一;例如:this

<bean name="helloWorld" class=」com.ljq.test.HelloWorldImpl」/> 

測試代碼片斷以下:url

複製代碼
@Test
public void sayHello() {
    BeanFactory beanFactory =
              new ClassPathXmlApplicationContext("helloworld.xml");
    //根據name獲取bean  
    HelloService helloService = beanFactory.getBean("helloWorld", HelloService.class);
    helloService.sayHello();
}
複製代碼

4、指定id和name,id就是標識符,而name就是別名,必須在Ioc容器中惟一;例如:

<bean id=」bean1」 name=」alias1」 class=」com.ljq.test.HelloWorldImpl」/> 
<!-- 若是id和name同樣,IoC容器能檢測到,並消除衝突 -->
<bean id="bean3" name="bean3" class=」com.ljq.test.HelloWorldImpl」/>

測試代碼片斷以下:

複製代碼
@Test
public void sayHello() {
    BeanFactory beanFactory =
              new ClassPathXmlApplicationContext("helloworld.xml");
    //根據id獲取bean 
    HelloService bean1 = beanFactory.getBean("bean1", HelloService.class);  
    bean1.sayHello();  
    //根據別名獲取bean  
    HelloService bean2 = beanFactory.getBean("alias1", HelloService.class);  
    bean2.sayHello();  
    //根據id獲取bean  
    HelloService bean3 = beanFactory.getBean("bean3", HelloService.class);  
    bean3.sayHello();  
    String[] bean3Alias = beanFactory.getAliases("bean3");  
    //所以別名不能和id同樣,若是同樣則由IoC容器負責消除衝突  
    Assert.assertEquals(0, bean3Alias.length);  
}
複製代碼

5、指定多個name,多個name用「,」、「;」、「 」分割,第一個被用做標識符,其餘的(alias一、alias二、alias3)是別名,全部標識符也必須在Ioc容器中惟一;

<bean name=」bean1;alias11,alias12;alias13 alias14」
      class=」com.ljq.test.HelloWorldImpl」/>   
<!-- 當指定id時,name指定的標識符所有爲別名 -->
<bean id="bean2" name="alias21;alias22"
      class=」com.ljq.test.HelloWorldImpl」/>   

測試代碼片斷以下:

複製代碼
@Test
public void sayHello() {
    BeanFactory beanFactory =
              new ClassPathXmlApplicationContext("helloworld.xml");
    //1根據id獲取bean
    HelloService bean1 = beanFactory.getBean("bean1", HelloService.class);
    bean1.sayHello();
    //2根據別名獲取bean
    HelloService alias11 = beanFactory.getBean("alias11", HelloService.class);
    alias11.sayHello();
    //3驗證確實是四個別名       
    String[] bean1Alias = beanFactory.getAliases("bean1");
    System.out.println("=======namingbean5.xml bean1 別名========");
    for(String alias : bean1Alias) {
        System.out.println(alias);
    }
    Assert.assertEquals(4, bean1Alias.length);
    //根據id獲取bean
    HelloService bean2 = beanFactory.getBean("bean2", HelloService.class);
    bean2.sayHello();
    //2根據別名獲取bean
    HelloService alias21 = beanFactory.getBean("alias21", HelloService.class);
    alias21.sayHello();
    //驗證確實是兩個別名
    String[] bean2Alias = beanFactory.getAliases("bean2");
    System.out.println("=======namingbean5.xml bean2 別名========");
    for(String alias : bean2Alias) {
        System.out.println(alias);
    }   
    Assert.assertEquals(2, bean2Alias.length);   
}
複製代碼

6、使用<alias>標籤指定別名,別名也必須在IoC容器中惟一

<bean name="bean" class=」com.ljq.test.HelloWorldImpl」/> 
<alias alias="alias1" name="bean"/> 
<alias alias="alias2" name="bean"/> 

測試代碼片斷以下:

複製代碼
@Test
public void test6() {
    BeanFactory beanFactory =
         new ClassPathXmlApplicationContext("chapter2/namingbean6.xml");
    //根據id獲取bean
    HelloService bean = beanFactory.getBean("bean", HelloService.class);
    bean.sayHello();
    //根據別名獲取bean
    HelloService alias1 = beanFactory.getBean("alias1", HelloService.class);
    alias1.sayHello();
    HelloService alias2 = beanFactory.getBean("alias2", HelloService.class);
    alias2.sayHello();
    String[] beanAlias = beanFactory.getAliases("bean");
    System.out.println("=======namingbean6.xml bean 別名========");
    for(String alias : beanAlias) {
        System.out.println(alias);
   }
   System.out.println("=======namingbean6.xml bean 別名========");
   Assert.assertEquals(2, beanAlias.length);
}
複製代碼

從定義來看,name或id若是指定它們中的一個時都做爲「標識符」,那爲何還要有id和name同時存在呢?這是由於當使用基於XML的配置元數據時,在XML中id是一個真正的XML id屬性,所以當其餘的定義來引用這個id時就體現出id的好處了,能夠利用XML解析器來驗證引用的這個id是否存在,從而更早的發現是否引用了一個不存在的bean,而使用name,則可能要在真正使用bean時才能發現引用一個不存在的bean。

●Bean命名約定:Bean的命名遵循XML命名規範,但最好符合Java命名規範,由「字母、數字、下劃線組成「,並且應該養成一個良好的命名習慣, 好比採用「駝峯式」,即第一個單詞首字母開始,從第二個單詞開始首字母大寫開始,這樣能夠增長可讀性。

1.1.4 實例化Bean
Spring IOC容器如何實例化Bean呢?傳統應用程序能夠經過new和反射方式進行實例化Bean。而Spring IOC容器則須要根據Bean定義裏的配置元數據使用反射機制來建立Bean。在Spring IOC容器中根據Bean定義建立Bean主要有如下幾種方式:
1、使用構造器實例化Bean:這是最簡單的方式,Spring IOC容器即能使用默認空構造器也能使用有參數構造器兩種方式建立Bean,如如下方式指定要建立的Bean類型:

使用空構造器進行定義,使用此種方式,class屬性指定的類必須有空構造器

<bean id="helloServiceNoWithArgs" class="com.ljq.test.HelloServiceImpl2" />

使用有參數構造器進行定義,使用此種方式,可使用<constructor-arg>標籤指定構造器參數值,其中index表示位置,value表示常量值,也能夠指定引用,指定引用使用ref來引用另外一個Bean定義,後邊會詳細介紹:

<bean id="helloServiceWithArgs" class="com.ljq.test.HelloServiceImpl2">
    <!-- 指定構造器參數 -->  
    <constructor-arg index="0" value="Hello Spring!"/>  
</bean>

知道如何配置了,讓咱們作個例子來實踐一下:
a、準備Bean class(HelloWorldImpl2.java),該類有一個空構造器和一個有參構造器:

複製代碼
package com.ljq.test;

public class HelloServiceImpl2 implements HelloService {
    private String message;

    /**
     * 空構造器
     */
    public HelloServiceImpl2() {
        this.message = "Hello World!";
    }

    /**
     * 帶參構造器
     * 
     * @param message
     */
    public HelloServiceImpl2(String message) {
        this.message = message;
    }

    public void sayHello() {
        System.out.println(message);
    }
}
複製代碼

b、在配置文件helloworld.xml配置Bean定義,以下所示:

複製代碼
    <!--使用默認構造參數--> 
    <bean id="helloServiceNoWithArgs" class="com.ljq.test.HelloServiceImpl2" />
    
    <!--使用有參數構造參數-->  
    <bean id="helloServiceWithArgs" class="com.ljq.test.HelloServiceImpl2">
        <!-- 指定構造器參數 -->  
        <constructor-arg index="0" value="Hello Spring!"/>  
    </bean>
複製代碼

c、配置完了,寫段測試代碼看是否工做:

複製代碼
    
    /**
     * 使用默認構造參數
     */
    @Test
    public void testHelloWorld2() {
        // 一、讀取配置文件實例化一個IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("helloworld.xml");
        // 二、從容器中獲取Bean,注意此處徹底「面向接口編程,而不是面向實現」
        HelloService helloService = context.getBean("helloServiceNoWithArgs", HelloService.class);
        // 三、執行業務邏輯
        helloService.sayHello();
    }
    
    /**
     * 使用有參數構造參數
     */
    @Test
    public void testHelloWorld3() {
        // 一、讀取配置文件實例化一個IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("helloworld.xml");
        // 二、從容器中獲取Bean,注意此處徹底「面向接口編程,而不是面向實現」
        HelloService helloService = context.getBean("helloServiceWithArgs", HelloService.class);
        // 三、執行業務邏輯
        helloService.sayHello();
    }
複製代碼

2、使用靜態工廠方式實例化Bean,使用這種方式除了指定必須的class屬性,還要指定factory-method屬性來指定實例化Bean的方法,並且使用靜態工廠方法也容許指定方法參數,spring IOC容器將調用此屬性指定的方法來獲取Bean,配置以下所示:
a、先來看看靜態工廠類代碼HelloApiStaticFactory:

複製代碼
package com.ljq.test;

/**
 * 靜態工廠類
 * 
 * @author 林計欽
 * @version 1.0 2013-11-5 下午10:03:49
 */
public class HelloWorldStaticFactory {
    /**
     * 工廠方法 
     * 
     * @param message
     * @return
     */
    public static HelloService newInstance(String message) {
        // 返回須要的Bean實例
        return new HelloServiceImpl2(message);
    }
     
}
複製代碼

b、在配置文件helloworld.xml配置Bean定義,以下所示:

    <!--使用有參數構造參數-->  
    <bean id="helloServiceStaticFactory" class="com.ljq.test.HelloWorldStaticFactory" factory-method="newInstance">
        <!-- 指定構造器參數 -->  
        <constructor-arg index="0" value="Hello Static Factory"/>  
    </bean>

c、配置完了,寫段測試代碼看是否工做:

複製代碼
/**
     * 使用有參數構造參數
     */
    @Test
    public void helloServiceStaticFactory() {
        // 一、讀取配置文件實例化一個IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("helloworld.xml");
        // 二、從容器中獲取Bean,注意此處徹底「面向接口編程,而不是面向實現」
        HelloService helloService = context.getBean("helloServiceStaticFactory", HelloService.class);
        // 三、執行業務邏輯
        helloService.sayHello();
    }
複製代碼

3、使用實例工廠方法實例化Bean,使用這種方式不能指定class屬性,此時必須使用factory-bean屬性來指定工廠Bean,factory-method屬性指定實例化Bean的方法,並且使用實例工廠方法容許指定方法參數,方式和使用構造器方式同樣,配置以下:
a、準備Bean class(HelloWorldImpl2.java),該類有一個空構造器和一個有參構造器:

複製代碼
package com.ljq.test;

/**
 * 靜態工廠類
 * 
 * @author 林計欽
 * @version 1.0 2013-11-5 下午10:03:49
 */
public class HelloWorldStaticFactory {
    
    /**
     * 工廠方法 
     * 
     * @param message
     * @return
     */
    public HelloService newInstance2(String message) {
        // 返回須要的Bean實例
        return new HelloServiceImpl2(message);
    }
}
複製代碼

b、在配置文件helloworld.xml配置Bean定義,以下所示:

    <!-- 一、定義實例工廠Bean -->
    <bean id="beanInstanceFactory" class="com.ljq.test.HelloWorldStaticFactory" />
    <!-- 二、使用實例工廠Bean建立Bean -->
    <bean id="bean4" factory-bean="beanInstanceFactory" factory-method="newInstance2">
         <constructor-arg index="0" value="Hello Spring!"></constructor-arg>
    </bean>

c、配置完了,寫段測試代碼看是否工做:

複製代碼
    @Test
    public void helloServiceStaticFactory2() {
        // 一、讀取配置文件實例化一個IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("helloworld.xml");
        // 二、從容器中獲取Bean,注意此處徹底「面向接口編程,而不是面向實現」
        HelloService helloService = context.getBean("bean4", HelloService.class);
        // 三、執行業務邏輯
        helloService.sayHello();
    }
複製代碼

經過以上例子咱們已經基本掌握瞭如何實例化Bean了,你們是否注意到?這三種方式只是配置不同,從獲取方式看徹底同樣,沒有任何不一樣。這也是Spring IOC的魅力,Spring IOC幫你建立Bean,咱們只管使用就能夠了,是否是很簡單。

1.1.5 小結到此咱們已經講完了Spring IOC基礎部分,包括IOC容器概念,如何實例化容器,Bean配置、命名及實例化,Bean獲取等等。不知你們是否注意到到目前爲止,咱們只能經過簡單的實例化Bean,沒有涉及Bean之間關係。接下來一章讓咱們進入配置Bean之間關係章節,也就是依賴注入。

相關文章
相關標籤/搜索