Spring配置 bean

在 Spring 的 IOC 容器裏配置 Bean

<bean id="helloWorld" class="com.xiya.spring.beans.HelloWorld">
    <property name="name" value="lgh"/>
</bean>

在 xml 文件中經過 bean 節點來配置 bean
id:Bean 的名稱。
在 IOC 容器中必須是惟一的
若 id 沒有指定,Spring 自動將權限定性類名做爲 Bean 的名字
id 能夠指定多個名字,名字之間可用逗號、分號、或空格分隔

  • 在 Spring IOC 容器讀取 Bean 配置建立 Bean 實例以前, 必須對它進行實例化. 只有在容器實例化後, 才能夠從 IOC 容器裏獲取 Bean 實例並使用.
  • Spring 提供了兩種類型的 IOC 容器實現. 
    • BeanFactory: IOC 容器的基本實現.
    • ApplicationContext: 提供了更多的高級特性. 是 BeanFactory 的子接口.BeanFactory 是 Spring 框架的基礎設施,面向 Spring 自己;ApplicationContext 面向使用 Spring 框架的開發者,幾乎全部的應用場合都直接使用 ApplicationContext 而非底層的 BeanFactory
  • 不管使用何種方式, 配置文件時相同的
ApplicationContext 的主要實現類:
ClassPathXmlApplicationContext:從 類路徑下加載配置文件
FileSystemXmlApplicationContext: 從文件系統中加載配置文件
ConfigurableApplicationContext 擴展於 ApplicationContext,新增長兩個主要方法:refresh() 和 close(), 讓 ApplicationContext 具備啓動、刷新和關閉上下文的能力
ApplicationContext 在初始化上下文時就實例化全部單例的 Bean。
WebApplicationContext 是專門爲 WEB 應用而準備的,它容許從相對於 WEB 根目錄的路徑中完成初始化工做


依賴注入的方式:
一、屬性注入
屬性注入即經過 setter 方法注入Bean 的屬性值或依賴的對象
屬性注入使用 <property> 元素, 使用 name 屬性指定 Bean 的屬性名稱,value 屬性或 <value> 子節點指定屬性值 
屬性注入是實際應用中最經常使用的注入方式
<bean id="helloWorld" class="com.xiya.spring.beans.HelloWorld">
    <property name="name" value="lgh"/>
</bean>

二、構造器注入
經過構造方法注入Bean 的屬性值或依賴的對象,它保證了 Bean 實例在實例化後就可使用。
構造器注入在 <constructor-arg> 元素裏聲明屬性, <constructor-arg> 中沒有 name 屬性
<bean id="car" class="com.xiya.spring.beans.Car">
    <constructor-arg value="BMW"/>
    <constructor-arg value="china"/>
    <constructor-arg value="100000"/>
</bean>


測試:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
        配置bean
        class:bean的全類名,經過反射的方式在IOC容器中建立bean,因此要求bean中必須有無參數的構造器
        id:標識容器中的bean,且惟一
    -->
    <bean id="helloWorld" class="com.xiya.spring.beans.HelloWorld">
        <property name="name" value="lgh"/>
    </bean>

    <!--
        經過構造方法來配置bean的屬性
    -->
    <bean id="car1" class="com.xiya.spring.beans.Car">
        <constructor-arg value="BMW" index="0"/>
        <constructor-arg value="beijing" index="1"/>
        <constructor-arg value="100000" index="2" type="double"/>
    </bean>
    <!--
        使用構造器注入屬性值能夠指定參數的位置和參數的類型!以區分重載的構造器
    -->
    <bean id="car2" class="com.xiya.spring.beans.Car">
        <constructor-arg value="Audi" index="0" type="java.lang.String"/>
        <constructor-arg value="shanghai" index="1" type="java.lang.String"/>
        <constructor-arg value="240" index="2" type="int"/>
    </bean>
</beans>

package com.xiya.spring.beans;

/**  * Created by N3verL4nd on 2017/3/22.  */ public class Car {
    private String brand;
    private String corp;
    private double price;
    private int maxSpeed;

    public Car(String brand, String corp, double price) {
        this.brand = brand;
        this.corp = corp;
        this.price = price;
    }

    public Car(String brand, String corp, int maxSpeed) {
        this.brand = brand;
        this.corp = corp;
        this.maxSpeed = maxSpeed;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", corp='" + corp + '\'' +
                ", price=" + price +
                ", maxSpeed=" + maxSpeed +
                '}';
    }
}


package com.xiya.spring.beans;

/**  * Created by N3verL4nd on 2017/3/22.  */ public class HelloWorld {
    private String name;

    public HelloWorld() {
        System.out.println("HelloWorld's constructor...");
    }

    public void setName(String name) {
        this.name = name;
    }

    public void hello() {
        System.out.println("hello " + name);
    }
}

package com.xiya.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**  * Created by N3verL4nd on 2017/3/22.  */ public class Main {
    public static void main(String[] args) {
        /*
        //IOC容器爲咱們執行建立對象與賦值操做
        HelloWorld helloWorld = new HelloWorld();
        //name屬性賦值
        helloWorld.setName("lgh");*/


        //1、建立springIOC容器對象
        //ApplicationContext表明IOC容器
        //ClassPathXmlApplicationContext:ApplicationContext接口的實現類,該實現類從類路徑下(src目錄下)加載配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2、從IOC容器中獲取bean實例
        //利用id返回到IOC容器中的bean
        HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
        //利用類型返回IOC容器中的bean,但要求IOC容器中必須只能有一個該類型的bean
        //HelloWorld helloWorld = context.getBean(HelloWorld.class);
        //3、調用hello()方法
        helloWorld.hello();


        Car car = (Car) context.getBean("car1");
        System.out.println(car);

        car = (Car) context.getBean("car2");
        System.out.println(car);
    }
}
字面值:可用字符串表示的值,能夠經過 <value> 元素標籤或 value 屬性進行注入。
基本數據類型及其封裝類、String 等類型均可以採起字面值注入的方式
若字面值中包含特殊字符,可使用 <![CDATA[]]> 把字面值包裹起來,或者使用XML裏的轉譯字符。
<value><![CDATA[<Audi>]]]></value>
XML中的轉譯字符
&(邏輯與)  &amp;        
<(小於)    &lt;        
>(大於)    &gt;        
"(雙引號)  &quot;      
'(單引號)  &apos;
<bean id="car1" class="com.xiya.spring.beans.Car">
    <constructor-arg value="beijing" index="1"/>
    <constructor-arg value="&lt;BMW&gt;" index="0"/>
    <constructor-arg value="100000" index="2" type="double"/>
</bean>
<!--
    使用構造器注入屬性值能夠指定參數的位置和參數的類型!以區分重載的構造器
-->
<bean id="car2" class="com.xiya.spring.beans.Car">
    <constructor-arg index="0" type="java.lang.String">
        <value><![CDATA[<Audi>]]]></value>
    </constructor-arg>
    <constructor-arg value="shanghai" index="1" type="java.lang.String"/>
    <constructor-arg index="2" type="int" value="240"/>
</bean>
組成應用程序的 Bean 常常須要相互協做以完成應用程序的功能. 要使 Bean 可以相互訪問, 就必須在 Bean 配置文件中指定對 Bean 的引用
在 Bean 的配置文件中, 能夠經過 <ref> 元素或 ref  屬性爲 Bean 的屬性或構造器參數指定對 Bean 的引用. 
<bean id="person1" class="com.xiya.spring.beans.Person">
    <!--屬性注入-->
    <!--<property name="name" value="lgh"/>
    <property name="age" value="24"/>
    <property name="car" ref="car2"/>-->
    <!--構造器注入-->
    <constructor-arg value="lgh"/>
    <constructor-arg value="24"/>
    <!--可使用propertyref屬性創建bean之間的引用關係-->
    <constructor-arg index="2" ref="car1"/>
</bean>

也能夠在屬性或構造器裏包含 Bean 的聲明, 這樣的 Bean 稱爲內部 Bean
<bean id="person2" class="com.xiya.spring.beans.Person">
    <property name="name" value="lgh"/>
    <property name="age" value="24"/>
    <!--內部bean,不可以被外部引用-->
    <property name="car">
        <bean class="com.xiya.spring.beans.Car">
            <constructor-arg value="BMW"/>
            <constructor-arg value="shanghai"/>
            <constructor-arg value="100000.0"/>
            <constructor-arg value="240"/>
        </bean>
    </property>
</bean>
當 Bean 實例僅僅給一個特定的屬性使用時, 能夠將其聲明爲內部 Bean. 內部 Bean 聲明直接包含在 <property> 或 <constructor-arg> 元素裏, 不須要設置任何 id 或 name 屬性
內部 Bean 不能使用在任何其餘地方
可使用專用的 <null/> 元素標籤爲 Bean 的字符串或其它對象類型的屬性注入 null 值
和 Struts、Hiberante 等框架同樣,Spring 支持級聯屬性的配置。
<property name="car">
    <bean class="com.xiya.spring.beans.Car">
        <constructor-arg value="BMW"/>
        <constructor-arg>
            <null/>
        </constructor-arg>
        <constructor-arg value="100000.0"/>
        <constructor-arg value="240"/>
    </bean>
</property>

<constructor-arg index="2" ref="car1"/>
<property name="car.brand" value="Audi"/>
在 Spring中能夠經過一組內置的 xml 標籤(例如: <list>, <set> 或 <map>) 來配置集合屬性.
配置 java.util.List 類型的屬性, 須要指定 <list>  標籤, 在標籤裏包含一些元素. 這些標籤能夠經過 <value> 指定簡單的常量值, 經過 <ref> 指定對其餘 Bean 的引用. 經過<bean> 指定內置 Bean 定義. 經過 <null/> 指定空元素. 甚至能夠內嵌其餘集合.
數組的定義和 List 同樣, 都使用 <list>
配置 java.util.Set 須要使用 <set> 標籤, 定義元素的方法與 List 同樣.
Java.util.Map 經過 <map> 標籤訂義, <map> 標籤裏可使用多個 <entry> 做爲子標籤. 每一個條目包含一個鍵和一個值. 
必須在 <key> 標籤裏定義鍵
由於鍵和值的類型沒有限制, 因此能夠自由地爲它們指定 <value>, <ref>, <bean> 或 <null> 元素. 
能夠將 Map 的鍵和值做爲 <entry> 的屬性定義: 簡單常量使用 key 和 value 來定義; Bean 引用經過 key-ref 和 value-ref 屬性定義
使用 <props> 定義 java.util.Properties, 該標籤使用多個 <prop> 做爲子標籤. 每一個 <prop> 標籤必須定義 key 屬性. 
<property name="cars">
    <!--使用list節點爲list類型的屬性賦值-->
    <list>
      <ref bean="car1"/>
      <ref bean="car2"/>
        <bean class="com.xiya.spring.beans.collections.Car">
            <constructor-arg value="Ford"/>
            <constructor-arg value="changan"/>
            <constructor-arg value="200000.0"/>
            <constructor-arg value="200"/>
        </bean>
    </list>
</property>
<!--配置Properties屬性值-->
<bean id="dataSource" class="com.xiya.spring.beans.collections.DataSource">
    <property name="properties">
        <!--使用propsprop子節點來爲Properties屬性賦值-->
        <props>
            <prop key="user">root</prop>
            <prop key="password">root</prop>
        </props>
    </property>
</bean>
<bean id="newPerson" class="com.xiya.spring.beans.collections.NewPerson">
    <property name="name" value="xiya"/>
    <property name="age" value="24"/>
    <property name="cars">
        <map>
            <entry key="BMW" value-ref="car1"/>
            <entry key="Audi" value-ref="car2"/>
        </map>
    </property>
</bean>
使用基本的集合標籤訂義集合時, 不能將集合做爲獨立的 Bean 定義, 致使其餘 Bean 沒法引用該集合, 因此沒法在不一樣 Bean 之間共享集合.
可使用 util schema 裏的集合標籤訂義獨立的集合 Bean. 須要注意的是, 必須在 <beans> 根元素裏添加 util schema 定義
<!--配置獨立的集合bean,以供多個bean進行引用-->
<util:list id="cars">
    <ref bean="car1"/>
    <ref bean="car2"/>
</util:list>

<bean id="person4" class="com.xiya.spring.beans.collections.Person">
    <property name="name" value="lgh"/>
    <property name="age" value="24"/>
    <property name="cars" ref="cars"/>
</bean>
•爲了簡化 XML 文件的配置,愈來愈多的XML文件採用屬性而非子元素配置信息。
•Spring 從 2.5版本開始引入了一個新的p命名空間,能夠經過<bean>元素屬性的方式配置Bean的屬性。
•使用 p 命名空間後,基於XML的配置方式將進一步簡化

Spring IOC 容器能夠自動裝配 Bean. 須要作的僅僅是在 <bean> 的 autowire 屬性裏指定自動裝配的模式
byType(根據類型自動裝配): 若 IOC 容器中有多個與目標 Bean 類型一致的 Bean. 在這種狀況下, Spring 將沒法斷定哪一個 Bean 最合適該屬性, 因此不能執行自動裝配.
byName(根據名稱自動裝配): 必須將目標 Bean 的名稱和屬性名設置的徹底相同.
constructor(經過構造器自動裝配): 當 Bean 中存在多個構造器時, 此種自動裝配方式將會很複雜. 不推薦使用
在 Bean 配置文件裏設置 autowire 屬性進行自動裝配將會裝配 Bean 的全部屬性. 然而, 若只但願裝配個別屬性時, autowire 屬性就不夠靈活了. 
autowire 屬性要麼根據類型自動裝配, 要麼根據名稱自動裝配, 不能二者兼而有之.
通常狀況下,在實際的項目中不多使用自動裝配功能,由於和自動裝配功能所帶來的好處比起來,明確清晰的配置文檔更有說服力一些
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="address" class="com.xiya.spring.beans.autowire.Address" p:city="beijing" p:street="dawanglu"/>
    <bean id="car" class="com.xiya.spring.beans.autowire.Car" p:brand="Audi" p:price="400000"/>
    <bean id="person" class="com.xiya.spring.beans.autowire.Person" p:name="lgh" p:address-ref="address" p:car-ref="car"/>
    <!--
        可使用autowire屬性指定自動裝配的方式
        byName根據bean的名字和當前beansetter風格的屬性名進行自動裝配,若沒有匹配的,則不進行裝配。
        byType根據bean的類型和當前bean的屬性的類型進行自動裝配。若IOC容器由1個以上的配型匹配,則拋異常。
    -->
    <!--<bean id="person" class="com.xiya.spring.beans.autowire.Person" p:name="autowire" autowire="byType"/>-->
</beans>
Spring 容許繼承 bean 的配置, 被繼承的 bean 稱爲父 bean. 繼承這個父 Bean 的 Bean 稱爲子 Bean
子 Bean 從父 Bean 中繼承配置, 包括 Bean 的屬性配置
子 Bean 也能夠覆蓋從父 Bean 繼承過來的配置
父 Bean 能夠做爲配置模板, 也能夠做爲 Bean 實例. 若只想把父 Bean 做爲模板, 能夠設置 <bean> 的abstract 屬性爲 true, 這樣 Spring 將不會實例化這個 Bean
並非 <bean> 元素裏的全部屬性都會被繼承. 好比: autowire, abstract 等.
也能夠忽略父 Bean 的 class 屬性, 讓子 Bean 指定本身的類, 而共享相同的屬性配置. 但此時 abstract 必須設爲 true
<!--
    抽象beanbeanabstract屬性爲truebean,它不能被IOC容器實例化,只能被繼承
    若某一個beanclass屬性沒有指定,則該bean必須是一個抽象bean
    -->
<bean id="address" class="com.xiya.spring.beans.autowire.Address"
      p:city="beijing" p:street="dawanglu" abstract="false"/>

<!--bean配置的繼承:使用beanparent屬性指定繼承哪一個bean的配置-->
<bean id="address2" parent="address" p:street="wudaokou "/>
Spring 容許用戶經過 depends-on 屬性設定 Bean 前置依賴的Bean,前置依賴的 Bean 會在本 Bean 實例化以前建立好
若是前置依賴於多個 Bean,則能夠經過逗號,空格或的方式配置 Bean 的名稱
<!--要求在配置bean時,必須有一個關聯的car,或者說person這個bean依賴於car這個bean-->
<bean id="person" class="com.xiya.spring.beans.autowire.Person"
      p:name="lgh" p:address-ref="address2" depends-on="car"/>
相關文章
相關標籤/搜索