Spring(四):Bean的自动装配

本文深入解析Spring框架中的自动装配机制,包括byName、byType及@Autowired和@Resource的使用方法,探讨了不同装配方式的特点和适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自动装配是使用spring满足bean依赖的一种方法

spring会在应用上下文中为某个bean寻找其依赖的bean.

三种装配机制

Spring中bean有三种装配机制,分别是:

  1. 在xml中显式配置;
  2. 在java中显式配置;
  3. 隐式的bean发现机制和自动装配。

测试环境搭建

xml配置测试:

新建两个实体类,Cat Dog 都有一个叫的方法

public class Dog {
    public void shout(){
        System.out.println("wang汪");
    }
    
public class Cat {
    public void shout(){
        System.out.println("miao喵");
    }

创建一个实体类:People

public class People {
    private Cat cat;
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }

编写Spring配置文件

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dog" class="com.Devin.pojo.Dog"/>
    <bean id="cat" class="com.Devin.pojo.Cat"/>

    <bean id="people" class="com.Devin.pojo.People">
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
        <property name="name" value="Devin学习Java"/>
    </bean>



</beans>

测试

public class MyTest {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getDog().shout();
        people.getCat().shout();
    }

}


ByName自动装配

修改bean配置,增加一个属性 autowire="byName"

<bean id="user" class="com.kuang.pojo.User" autowire="byName">
   <property name="name" value="Devin"/>
</bean>

小结: 当一个bean节点带有 autowire byName的属性时。

  1. 将查找其类中所有的set方法名,例如setCat,获得将set去掉并且首字母小写的字符串,即cat。
  2. 去spring容器中寻找是否有此字符串名称id的对象。
  3. 如果有,就取出注入;如果没有,就报空指针异常。

ByType自动装配

<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="cat" class="com.kuang.pojo.Cat"/>
<!--
byName:会自动在容器上下文中查找,和自己对象set方法后面的值对象bean  id!
byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean
-->


<bean id="user" class="com.kuang.pojo.User" autowire="byName">
   <property name="name" value="Devin"/>
</bean>

小结:

当一个bean节点带有 autowire byType的属性时。

  1. 需要保证所有bean的class是唯一的,并且这个bean需要和自动注入的属性的类型一致!

使用注解自动装配:@Autowired和@Resource

1、导入约束:context约束

2、在spring配置注解支持:context:annotation-config/

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>
@Autowired

直接在属性上使用,也可以用在set方法上!

使用@Autowired,我们可以不编写set方法,前提是自动装配的属性在IOC (Spring) 容器中存在,并且符合名字byname!

@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
<bean id="dog" class="com.Devin.pojo.Dog"/>
<bean id="cat" class="com.Devin.pojo.Cat"/>
<bean id="people" class="com.Devin.pojo.People"/>

另一种方法:

@Autowired配和@Qualifier使用,从定义bean方法里面的byName名字
public class People {
    @Autowired
    @Qualifier(value = "cat01")
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog01")
    private Dog dog;
<bean id="dog01" class="com.Devin.pojo.Dog"/>
<bean id="cat01" class="com.Devin.pojo.Cat"/>

@Autowired(required=false) 说明:false,对象可以为null;true,对象必须存对象,不能为null。

@Resource
public class People {
    @Resource
    private Cat cat;
    @Resource
    private Dog dog;

或者:

public class People {
    @Resource(name = "cat22")
    private Cat cat;
    @Resource(name = "dog22")
    private Dog dog;
<bean id="dog22" class="com.Devin.pojo.Dog"/>
<bean id="cat22" class="com.Devin.pojo.Cat"/>

Resource后面没有name属性,会在bean标签先查找id,id找不到会找class对象,只有有符合就OK,但是bean里面有两个全限定类名一样,就需要用name来确定id的值

小结:

@Autowired 和 @Resource 的区别

都是用来自动装配的,都可以放在属性字段上

@Autowired 通过byType的方式实现,而且必须要求这个对象存在!

@Resource 默认通过byName的方式实现,如果找不到名字,则通过byType实现.如果两个都找不到就报错

执行顺序不同:

​ @Autowired 通过byType的方式实现

​ @Resource 默认通过byName的方式实现

本文章来源于B站(狂神说) B站地址:https://space.bilibili.com/95256449

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值