Spring framework(九)自动装配

一、概念

Bean的装配可以理解为依赖关系注入,Spring容器支持多种Bean装配方式,基于XML的Bean装配,基于Annotation的装配和自动装配。

自动装配就是指Spring容器在不适用和标签情况下,可以自动装配(autowire)相互协作的Bean之间的关联关系,将一个Bean注入到其他Bean的Property中。

使用自动装配需要配置元素的autowire属性,autowire属性有五个值:

名称描述
no默认值,表示不使用自动装配,Bean依赖必须通过ref元素定义。
byName根据Property的name自动装配,如果一个Bean的name和另一个Bean中的Property的name相同,则自动装配这个Bean到Property中。
byType根据Property的数据类型(Type)自动装配,如果一个Bean的数据类型兼容另一个Bean中Property的数据类型,则自动装配。
constructor类似于byType,根据构造方法参数的数据类型,进行byType模式的自动装配。
autodetect如果 Bean 中有默认的构造方法,则用 constructor 模式,否则用 byType 模式。

二、实例

public class Man {
    private String name;
    private Integer age;

    public Man() {
        System.out.println("Man的无参构造函数");
    }

    public Man(String name, Integer age) {
        System.out.println("Man的有参构造函数");
        this.name = name;
        this.age = age;
    }

    public void show(){
        System.out.println("名称:"+name+",年龄:"+age);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
public class Person {
    private Man man;

    public Person() {
        System.out.println("在Person的无参构造函数内");
    }

    public Person(Man man) {
        System.out.println("在Person的有参构造函数内");
        this.man = man;
    }

    public void man() {
        man.show();
    }

    public Man getMan() {
        return man;
    }

    public void setMan(Man man) {
        this.man = man;
    }
}
public class MainApp1 {
    public static void main(String[] args) {
        ApplicationContext context3 = new ClassPathXmlApplicationContext("Beans3.xml");
        Person person = (Person) context3.getBean("person");
        person.man();
    }
} 

2.1、不使用自动装配

autowire=“no” 表示不使用自动装配,需要手动注入,Bean 依赖通过 ref 元素定义

<?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-3.0.xsd">
    <bean id="man" class="com.jsonliu.bean.Man">
        <property name="age" value="20"/>
        <property name="name" value="王者"/>
    </bean>
    <bean id="person" class="com.jsonliu.bean.Person" autowire="no">
        <constructor-arg ref="man" type="com.jsonliu.bean.Man"/>
    </bean>
</beans>

运行结果:

2.2、按名称自动装配

autowire=“byName” 表示按属性名称自动装配,XML 文件中 Bean 的 id 必须与类中的属性名称相同

<?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-3.0.xsd">
    <bean id="man" class="com.jsonliu.bean.Man">
        <property name="age" value="20"/>
        <property name="name" value="王者"/>
    </bean>
    <bean id="person" class="com.jsonliu.bean.Person" autowire="byName">
    </bean>
</beans>

运行结果:

在这里插入图片描述

名称不同报错如下:

在这里插入图片描述

2.3、按类型自动装配

autowire=“byType” Bean 的 id 与类中的属性名称可以不同,但必须只有一个类型的 Bean

<?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-3.0.xsd">
    <bean id="man" class="com.jsonliu.bean.Man">
        <property name="age" value="20"/>
        <property name="name" value="王者"/>
    </bean>
    <bean id="person" class="com.jsonliu.bean.Person" autowire="byType">
    </bean>
</beans>

运行结果:

在这里插入图片描述

多个Bean报错如下:

在这里插入图片描述

2.4、构造函数自动装配

autowire=“constructor” 构造函数的参数必须在配置文件中有相同的类型

<?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-3.0.xsd">
    <bean id="man1" class="com.jsonliu.bean.Man">
        <property name="age" value="20"/>
        <property name="name" value="王者"/>
    </bean>
    <bean id="person" class="com.jsonliu.bean.Person" autowire="constructor">
    </bean>
</beans>

运行结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zDahOCEi-1638542091206)(4A77F1A1302F4B1F95F466B13954E662)]

三、自动装配优缺点

优点:

  • 自动装配只需要较少的代码就可以实现依赖注入

缺点:

  • 不能自动装配简单数据类型:int、boolean、String等
  • 相比显示装配,自动装配不受程序员控制
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑谈子云亭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值