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

}
发布了37 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/masteryourself/article/details/104269114
今日推荐