1. Autowiring Collaborators
Mode | Explanation |
---|---|
|
(Default) No autowiring. Bean references
must be defined by ref 元素定義。對於較大的部署,建議不要更改默認設置,因爲明確指定協作者可以提供更好的控制和清晰度。 在某種程度上,它記錄了系統的結構
|
|
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 setMaster(..) 方法),那麼
Spring 將查找一個名爲 master 的 bean 定義並使用它來設置該屬性
|
|
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 自動裝配。 如果沒有匹配的
bean,則什麼都不會發生(未設置該屬性)
|
|
Analogous to 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;
}
}