Dashboard > OpenSource Project > ... > Spring > 入門 09 - 屬性參考與自動綁定
OpenSource Project Log In   View a printable version of the current page.
入門 09 - 屬性參考與自動綁定
Added by minqing, last edited by frank on Jul 20, 2005  (view change)
Labels: 
(None)

Reference by caterpillar ,caterpillar

 在定義Bean時,除了直接指定值給屬性值之外,還可以直接參考定義檔中的其它Bean,例如HelloBean是這樣的話:

HelloBean.java
package onlyfun.caterpillar; 

import java.util.Date; 

public class HelloBean { 
    private String helloWord = "Hello!World!"; 
    private Date date; 
    
    public void setHelloWord(String helloWord) { 
        this.helloWord = helloWord; 
    } 
    public String getHelloWord() { 
        return helloWord + ":" + date.toString(); 
    } 
    public void setDate(Date date) { 
        this.date = date; 
    }    
    public Date getDate() { 
        return date; 
    } 
}

 我們的Bean定義檔中,先定義了一個dateBean,之後helloBean可以直接參考至dateBean,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="dateBean" class="java.util.Date"/> 
    
    <bean id="helloBean" class="onlyfun.caterpillar.HelloBean"> 
        <property name="helloWord"> 
            <value>Hello!Justin!</value> 
        </property> 
        <property name="date"> 
            <ref bean="dateBean"/> 
        </property> 
    </bean> 
</beans>

 直接指定值或是使用<ref>直接指定參考至其它的Bean,這種顯式的關係指定是比較好的,不過Spring也支援隱式的自動綁定,您可以透過類型(byType)或名稱(byName)將Bean綁定至其它Bean上對應的屬性,下面是個byType的例子:

<?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="dateBean" class="java.util.Date"/> 
    
    <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="byType"> 
        <property name="helloWord"> 
            <value>Hello!Justin!</value> 
        </property> 
        
    </bean> 
</beans>

 在這邊,我們並沒有指定helloBean的Date屬性,而是透過自動綁定,由於autowire指定了byType,所以會根據Date屬性所接受的型態,判斷BeanFactory中是否有類似的型態物件,並將之綁定至Date屬性上。

 您也可以指定byName來綁定,則Spring會根據bean的別名與屬性名稱是否符合來進行自動綁定,舉個例子來說,如果是byName而Date屬性要完成依賴注入的話,則必須修改一下第一個Bean的id值為date:

<?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="date" class="java.util.Date"/> 
    
    <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="byName"> 
        <property name="helloWord"> 
            <value>Hello!Justin!</value> 
        </property> 
        
    </bean> 
</beans>

 隱式的自動綁定沒辦法從定義檔中,清楚的看到是否每個屬性都完成設定,我們可以加入相依檢查,在<bean>上加入dependency-check,有四種相依檢查方式:simple、objects、all、none。第一個只檢查簡單的屬性,像是原生(primitive)資料型態或字串物件,objects檢查物件屬性,all則檢查全部的屬性,none是預設,表示不檢查相依性。

 下面是一個設定的例子:

<?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="dateBean" class="java.util.Date"/> 
    
    <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="byType" dependency-check="all"> 
        <property name="helloWord"> 
            <value>Hello!Justin!</value> 
        </property> 
        
    </bean> 
</beans>

 如果相依檢查發現有未完成的依賴關係,則運行時會丟出UnsatisfiedDependencyException。

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.1.5a Build:#411 Mar 16, 2006) - Bug/feature request - Contact Administrators