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;
}
}