08.Spring Framework 之自動裝配模式

1. Autowiring Collaborators

詳見文檔 https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-autowire

Mode Explanation

no

(Default) No autowiring. Bean references must be defined by ref elements. Changing the default setting is not recommended for larger deployments, because specifying collaborators explicitly gives greater control and clarity. To some extent, it documents the structure of a system.

(默認)無自動裝配。 Bean引用必須由 ref 元素定義。對於較大的部署,建議不要更改默認設置,因爲明確指定協作者可以提供更好的控制和清晰度。 在某種程度上,它記錄了系統的結構

byName

Autowiring by property name. Spring looks for a bean with the same name as the property that needs to be autowired. For example, if a bean definition is set to autowire by name and it contains a master property (that is, it has a setMaster(..) method), Spring looks for a bean definition named master and uses it to set the property.

按屬性名稱自動佈線。 Spring 尋找與需要自動裝配的屬性同名的 bean。 例如,如果一個 bean 定義被設置爲按名稱自動裝配,並且包含一個 master 屬性(即,它具有 setMaster(..)方法),那麼 Spring 將查找一個名爲 master 的 bean 定義並使用它來設置該屬性

byType

Lets a property be autowired if exactly one bean of the property type exists in the container. If more than one exists, a fatal exception is thrown, which indicates that you may not use byType autowiring for that bean. If there are no matching beans, nothing happens (the property is not set).

如果容器中恰好存在一個屬性類型的 bean,則使該屬性自動連接。 如果存在多個錯誤,則將引發致命異常,這表明您不能對該 bean 使用 byType 自動裝配。 如果沒有匹配的 bean,則什麼都不會發生(未設置該屬性)

constructor

Analogous to byType but applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised.

byType 類似,但適用於構造函數參數。 如果容器中不存在構造函數參數類型的一個bean,則將引發致命錯誤。

2. byName

代碼已經上傳至 https://github.com/masteryourself-tutorial/tutorial-spring ,詳見 tutorial-spring-framework/tutorial-spring-framework-autowiring/tutorial-spring-framework-autowiring-byname 工程

2.1 配置文件

1. spring.xml
<?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"
       default-autowire="byName">

    <bean id="person" class="pers.masteryourself.tutorial.spring.framework.autowiring.bean.Person"/>
    <bean id="dog1" class="pers.masteryourself.tutorial.spring.framework.autowiring.bean.Dog">
        <property name="name" value="dog1"/>
    </bean>
    <bean id="dog2" class="pers.masteryourself.tutorial.spring.framework.autowiring.bean.Dog">
        <property name="name" value="dog2"/>
    </bean>

</beans>

2.2 核心代碼

1. Person
@ToString
public class Person {

    private Dog dog2;

    /**
     * byName 會根據 setDog1() 方法的名字注入 bean,即 [dog1],和屬性名字 [dog2] 無關係
     * 如果方法名稱改爲 setDog(),因爲容器中存在 Dog 的 beanName 是 [dog1] 和 [dog2],所以將會注入 null
     *
     * @param dog
     */
    public void setDog1(Dog dog) {
        this.dog2 = dog;
    }

}

3. byType

代碼已經上傳至 https://github.com/masteryourself-tutorial/tutorial-spring ,詳見 tutorial-spring-framework/tutorial-spring-framework-autowiring/tutorial-spring-framework-autowiring-byType 工程

3.1 配置文件

1. spring.xml
<?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"
       default-autowire="byType">

    <bean id="person" class="pers.masteryourself.tutorial.spring.framework.autowiring.bean.Person"/>
    <bean id="dog1" class="pers.masteryourself.tutorial.spring.framework.autowiring.bean.Dog">
        <property name="name" value="dog1"/>
    </bean>
    <!-- byType 不能有多個相同類型的 bean -->
    <!--<bean id="dog2" class="pers.masteryourself.tutorial.spring.framework.autowiring.bean.Dog">
        <property name="name" value="dog2"/>
    </bean>-->

</beans>

3.2 核心代碼

1. Person
@ToString
public class Person {

    private Dog dog;

    /**
     * byType 會從容器中查找 setDog() 方法中的入參類型的 bean,不能有多個類型的 bean
     *
     * @param dog
     */
    public void setDog(Dog dog) {
        this.dog = dog;
    }

}

4. constructor

代碼已經上傳至 https://github.com/masteryourself-tutorial/tutorial-spring ,詳見 tutorial-spring-framework/tutorial-spring-framework-autowiring/tutorial-spring-framework-autowiring-constructor 工程

4.1 配置文件

1. spring.xml
<?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"
       default-autowire="constructor">

    <bean id="person" class="pers.masteryourself.tutorial.spring.framework.autowiring.bean.Person"/>
    <bean id="dog1" class="pers.masteryourself.tutorial.spring.framework.autowiring.bean.Dog">
        <property name="name" value="dog1"/>
    </bean>
    <bean id="dog2" class="pers.masteryourself.tutorial.spring.framework.autowiring.bean.Dog">
        <property name="name" value="dog2"/>
    </bean>

</beans>

4.2 核心代碼

1. Person
@ToString
public class Person {

    private Dog dog;

    /**
     * constructor 會根據構造函數中的入參查找容器中是否有對應的 bean
     * 先根據類型查找,如果有多個類型的 bean,再根據 beanName 獲取,如下是獲取 beanName=dog2 的 bean
     *
     * @param dog2
     */
    public Person(Dog dog2) {
        this.dog = dog2;
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章