7.4.2 Dependencies and configuration in detail

如上一節所述,您能夠將bean屬性和構造函數參數定義爲對其餘受管Bean(協做者)的引用,也能夠定義爲inline。Spring的基於XML的配置元數據支持其<property />和<constructor-arg />元素中的子元素類型。html

Straight values (primitives, Strings, and so on)

    <property />元素的value屬性將一個屬性或構造函數參數指定爲人類可讀的字符串表示形式。Spring的轉換服務用於將這些值從String轉換爲實際的屬性或參數類型。java

    

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <!-- results in a setDriverClassName(String) call -->
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
    <property name="username" value="root"/>
    <property name="password" value="masterkaoli"/>
</bean>

如下示例使用p命名空間進行更簡潔的XML配置。mysql

<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="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close"
        p:driverClassName="com.mysql.jdbc.Driver"
        p:url="jdbc:mysql://localhost:3306/mydb"
        p:username="root"
        p:password="masterkaoli"/>

</beans>

    前面的XML更簡潔;可是,在運行時而不是設計時發現錯字,除非您在建立bean定義時使用支持自動屬性完成的IDE(如IntelliJ IDEA或Spring Tool Suite(STS))。spring

    您還能夠將java.util.Properties實例配置爲:sql

<bean id="mappings"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

    <!-- typed as a java.util.Properties -->
    <property name="properties">
        <value>
            jdbc.driver.className=com.mysql.jdbc.Driver
            jdbc.url=jdbc:mysql://localhost:3306/mydb
        </value>
    </property>
</bean>

Spring容器經過使用JavaBeans PropertyEditor機制將<value />元素內的文本轉換爲java.util.Properties實例。這是一個很好的捷徑,並且是Spring團隊同意在value屬性樣式中使用嵌套的<value />元素的幾個地方之一。apache

The idref elementapp

idref元素只是將容器中另外一個bean的id(字符串值 - 不是引用)傳遞給<constructor-arg />或<property />元素的方法。函數

<bean id="theTargetBean" class="..."/>

<bean id="theClientBean" class="...">
    <property name="targetName">
        <idref bean="theTargetBean"/>
    </property>
</bean>

上述bean定義片斷與如下代碼片斷徹底相同(在運行時):ui

<bean id="theTargetBean" class="..." />

<bean id="client" class="...">
    <property name="targetName" value="theTargetBean"/>
</bean>

第一種形式優於第二種形式,由於使用idref標籤容許容器在部署時驗證引用的命名bean實際存在。在第二個變體中,不會對傳遞給客戶端bean的targetName屬性的值執行驗證。客戶端bean實際上被實例化時,纔會發現錯字(最多是致命(fatal)的結果)。若是客戶端bean是原型bean( prototype bean),則此錯誤和生成的異常只能在容器部署後才能被發現。this

[Note] 在4.0 beans xsd中,idref元素上的local屬性再也不受支持,由於它再也不提供超過常規bean引用的值。在升級到4.0模式時,只需將您現有的idref本地引用更改成idref bean。

一個常見的地方(至少在Spring 2.0以前的版本中),其中<idref />元素帶來價值是在ProxyFactoryBean bean定義中配置AOP攔截器。指定攔截器名稱時使用<idref />元素可防止拼寫錯誤。

References to other beans (collaborators)

ref元素是<constructor-arg />或<property />定義元素中的最後一個元素。在這裏,您能夠將bean的指定屬性的值設置爲對由容器管理的另外一個bean(協做者)的引用。引用的bean是其屬性將被設置的bean的依賴關係,而且在設置屬性以前根據須要初始化它。(若是協做者( collaborator)是單例bean,則能夠由容器初始化它。)全部references最終都是對另外一個對象的引用。​​​​​​​範圍和驗證取決因而否經過bean,本地或父屬性指定其餘對象的id / name。​​​​​​​經過<ref />標籤的bean屬性指定目標bean是最通用的形式,容許建立對同一容器或父容器中任何bean的引用,而無論它們是否在同一個XML文件中。​​​​​​​bean屬性的值可能與目標bean的id屬性相同,或者與目標bean的name屬性中的值之一相同。​​​​​​​經過父屬性指定目標bean建立對當前容器的父容器中的bean的引用。​​​​​​​​​​​​​​

<ref bean="someBean"/>

​​​​​​​父屬性的值可能與目標bean的id屬性或目標bean的name屬性中的值之一相同,目標bean必須位於當前bean的父容器中。​​​​​​​您使用此bean參考變體主要是在具備容器層次結構時,而且想要使用與父bean具備相同名稱的代理將現有bean包裝在父容器中。

<!-- in the parent context -->
<bean id="accountService" class="com.foo.SimpleAccountService">
    <!-- insert dependencies as required as here -->
</bean>
<!-- in the child (descendant) context -->
<bean id="accountService" <!-- bean name is the same as the parent bean -->
    class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target">
        <ref parent="accountService"/> <!-- notice how we refer to the parent bean -->
    </property>
    <!-- insert other configuration and dependencies as required here -->
</bean>
[Note]     
4.0 bean xsd中再也不支持ref元素上的local屬性,由於它再也不提供超過常規bean引用的值。在升級到4.0模式時,只需將現有的ref參考引用改成ref bean。

Inner beans

<property />或<constructor-arg />元素中的<bean />元素定義了一個所謂的內部bean。

<bean id="outer" class="...">
    <!-- instead of using a reference to a target bean, simply define the target bean inline -->
    <property name="target">
        <bean class="com.example.Person"> <!-- this is the inner bean -->
            <property name="name" value="Fiona Apple"/>
            <property name="age" value="25"/>
        </bean>
    </property>
</bean>

內部bean定義不須要定義的id或名稱;若是指定,容器不使用這樣的值做爲標識符。容器也會忽略建立的範圍標誌:內部bean老是匿名的,它們老是使用外部bean建立的。將內部bean注入到除了封閉bean以外的協做bean中,或者獨立訪問它們是不可能的。

Collections

在<list />,<set />,<map />和<props />元素中,您能夠分別設置Java集合類型列表,集合,映射和屬性的屬性和參數。

7.4.5 Autowiring collaborators

相關文章
相關標籤/搜索