bean的自动装配

自动装配是Spring框架中管理bean依赖的一种方法,包括在XML中显式装配、Java配置中装配以及隐式自动装配(重点)。通过byName和byType两种方式,Spring会根据bean的id或class自动赋值。此外,还介绍了使用@Autowired和@Resource注解进行注解装配,其中@Autowired允许指定required参数,而@Resource默认尝试按名称装配,找不到时按类型装配。

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

自动装配是Spring满足bean依赖的一种方式
Spring会在上下文中自动寻找,并自动给bean装配属性
也就是给对象赋值
三种方式:

  • 1、在xml中显式地装配
  • 2、在java中显式地装配
  • 3、隐式地自动装配bean(重要)

搭建环境:

public class Cat {
    private String name;
    private String ouch;

    public Cat(){
        System.out.println("这个是猫");
    }
    public String getName() {
        return name;
    }

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

    public String getOuch() {
        return ouch;
    }

    public void setOuch(String ouch) {
        this.ouch = ouch;
    }

    @Override
    public String toString() {
        return "这是一只猫,喵喵吗";
    }
}

public class Address {
    private String phone;
    private String address;
public Address(){
    System.out.println("实例化地址");
}

    public Address(String address) {
    this.address=address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Address(String phone, String address) {
        this.phone = phone;
        this.address = address;
    }

    @Override
    public String toString() {
        return "Address{" +
                "phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                '}'+"地址被使用了";
    }
}

import lombok.Data;

@Data
public class Student {
    private String name;
    private String id;
    private String gender;
    private String age;


}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {

    private String name;
    private Cat cat;
    private Student student;


}


beans.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"
       xmlns:context = "http://www.springframework.org/schema/context"
       xmlns:aop = "http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        ">
<!--    指定要扫描的包-->


<context:component-scan base-package="grandzio.pojos"/>
    <context:annotation-config></context:annotation-config>

    <bean id="student" class="grandzio.pojos.Student">
    <property name="name" value="王杰"/>
    <property name="id" value="1223"/>
    </bean>

<bean id="address1" name="adrs" class="grandzio.pojos.Address">
    <property name="address" value="南京市"/>
    <property name="phone" value="12345678"/>
</bean>
    <bean id="address2" name="adrss" class="grandzio.pojos.Address">
        <constructor-arg name="address" value="河南省" />

    </bean>
    <bean name="cat" class="grandzio.pojos.Cat">
        <property name="ouch" value="miao"/>
        <property name="name" value="cat1"/>
    </bean>
    <bean name="teacher" class="grandzio.pojos.Teacher" >

        <property name="cat" ref="cat"/>
        <property name="student" ref="student"/>
        <property name="name" value="王老师"/>
    </bean>

</beans>

默认方式,即以上的使用ref 引用bean的name

在这里插入图片描述
(1)通过byName
修改以上xml部分内容

 <bean name="teacher" class="grandzio.pojos.Teacher" autowire="byName" >

        <property name="name" value="王老师"/>
    </bean>

在这里插入图片描述(2)通过byType
在这里插入图片描述

注意:

  • byName的时候,保证所有bean的id值唯一,并且这个bean需要和自动注入的属性的set方法的值一致
  • byName的时候,保证所有bean的class值唯一,并且这个bean需要和自动注入的属性的类型一致

使用注解方式注入
@Autowired
直接在属性上使用,也可以在set方式上使用!
使用Autowired可以不用再编写Set方法,但这个自动装配的属性在IOC容器中存在,并且符合名字byName

@Autowired有一个参数required 当required =false时,表示可以为空否则不允许为空

@Qualifier(value = “”) 如果自动装配环境比较复杂,可以使用 Qualifier(value = “”)配合Autowired指定一个唯一的bean对象

public class Teacher {

    private String name;
    @Autowired
    @Qualifier(value = "cat2")
    private Cat cat;
    @Autowired(required = false)
    private Student student;
}

@Resource注解

public class Teacher {

    private String name;
    @Resource(name = "cat2")
    private Cat cat;
    @Autowired(required = false)
    private Student student;
}


@Resource和@Autowired区别
都是用于自动装配
@Autowired通过byName的方式实现,并且要求对象必须存在
@Resource默认通过byname的方式实现,如果找不到名字,byType实现,如果两个都找不到,就报错。【常用】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值