Spring——自动装配

假设一个场景:

一个人(Person)有一条狗(Dog)和一只猫(Cat),狗和猫都会叫,狗叫是"汪汪",猫叫是"喵喵",同时人还有一个自己的名字。

将上述场景 抽象出三个实体类:Person、Dog和Cat

Persion.java

java 复制代码
package com.zbt.autowire.dto;

/**
 * @author
 * @createAt 2025/1/9 20:44
 */


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

    public Dog getDog() {
        return dog;
    }

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

    public Cat getCat() {
        return cat;
    }

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

    public String getName() {
        return name;
    }

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

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

Dog.java

java 复制代码
package com.zbt.autowire.dto;

/**
 * @author
 * @createAt 2025/1/9 20:42
 */


public class Dog {
    public void shout(){
        System.err.println("小狗叫:wang~");
    }
}

Cat.java

java 复制代码
package com.zbt.autowire.dto;

/**
 * @author
 * @createAt 2025/1/9 20:43
 */


public class Cat {
    public void shout(){
        System.err.println("小猫叫:miao~");
    }
}

手动装配

在配置文件中注入属性---这种方式为xml手动注入(显示注入)

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">

    <bean id="dog" class="com.zbt.autowire.dto.Dog"/>
    <bean id="cat" class="com.zbt.autowire.dto.Cat"/>
    <bean id="person" class="com.zbt.autowire.dto.Person">
        <property name="name" value="张三"/>
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
    </bean>
</beans>

通过配置文件 自动装配

bean标签有一个属性叫做 autowire 它配置的是自动装配方式

byName注入

通过配置的其他的bean的id自动匹配参数名,相同则注入(换种说法:将和属性名相同的Bean id 对应的对象当做值进行注入)

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">

    <bean id="dog" class="com.zbt.autowire.dto.Dog"/>
    <bean id="cat" class="com.zbt.autowire.dto.Cat"/>
    <!--<bean id="person" class="com.zbt.autowire.dto.Person">
        <property name="name" value="张三"/>
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
    </bean>-->
    <bean id="person" class="com.zbt.autowire.dto.Person" autowire="byName">
        <property name="name" value="张三"/>
    </bean>
</beans>

上面的例子中,Person中有cat和dog 在配置文件中Cat和Dog类的Bean id也是cat和dog,此时可以通过在Person的Bean上设置**autowire="byName"**自动将cat和dog注入到Person中
注意 如果需要被注入的bean的id与属性名不同则无法注入
例如下面这种情况就无法给Person注入dog

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">

    <bean id="dog1" class="com.zbt.autowire.dto.Dog"/>
    <bean id="cat" class="com.zbt.autowire.dto.Cat"/>
    <!--<bean id="person" class="com.zbt.autowire.dto.Person">
        <property name="name" value="张三"/>
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
    </bean>-->
    <bean id="person" class="com.zbt.autowire.dto.Person" autowire="byName">
        <property name="name" value="张三"/>
    </bean>
</beans>

接下来我们在另一个包entity下在新建一个Cat类,同样也有一个shout方法

java 复制代码
package com.zbt.autowire.entity;

/**
 * @author
 * @createAt 2025/1/9 21:09
 */


public class Cat {
public void shout(){
    System.err.println("entity里的Cat");
}
}

在配置文件中对两个Cat类的bean做如下配置

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">

    <bean id="dog1" class="com.zbt.autowire.dto.Dog"/>
    <bean id="cat1" class="com.zbt.autowire.dto.Cat"/>
    <bean id="cat" class="com.zbt.autowire.entity.Cat"/>
    <!--<bean id="person" class="com.zbt.autowire.dto.Person">
        <property name="name" value="张三"/>
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
    </bean>-->
    <bean id="person" class="com.zbt.autowire.dto.Person" autowire="byName">
        <property name="name" value="张三"/>
    </bean>
</beans>

此时Person注入的cat对象为entity包下的Cat

编写如下测试代码可验证结果:

java 复制代码
import com.zbt.autowire.dto.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author
 * @createAt 2025/1/9 21:01
 */


public class MyTest {
    @Test
    public void testAutowire(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beansAutowire.xml");
        Person person = context.getBean("person", Person.class);
        person.getCat().shout();
    }
}

控制台输出为

注意我划线部分的报错,意思是无法将entity下的Cat转换为dto下的Cat(因为Person类中的Cat类型为dto下的Cat,而注入的根据bean的id匹配到的是entity下的,所以报了这个错误)

byType注入

通过配置的其他的bean的calss设置的类自动匹配参数类型,相同则注入(换种说法:查找与属性类型相同的Bean对应的对象当做值进行注入)

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">

    <bean id="dog1" class="com.zbt.autowire.dto.Dog"/>
    <bean id="cat" class="com.zbt.autowire.dto.Cat"/>
    <!--<bean id="person" class="com.zbt.autowire.dto.Person">
        <property name="name" value="张三"/>
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
    </bean>-->
    <bean id="person" class="com.zbt.autowire.dto.Person" autowire="byType">
        <property name="name" value="张三"/>
    </bean>
</beans>

上面的例子中,Person中有cat和dog 在配置文件中也配置了Cat和Dog类的Bean,此时可以通过在Person的Bean上设置**autowire="byType"**自动将cat和dog注入到Person中,与Cat和Dog的Bean的id无关。

同时在配置文件中也配置entity.cat的bean

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">

    <bean id="dog1" class="com.zbt.autowire.dto.Dog"/>
    <bean id="cat" class="com.zbt.autowire.dto.Cat"/>
    <bean id="cat1" class="com.zbt.autowire.entity.Cat"/>
    <!--<bean id="person" class="com.zbt.autowire.dto.Person">
        <property name="name" value="张三"/>
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
    </bean>-->
    <bean id="person" class="com.zbt.autowire.dto.Person" autowire="byType">
        <property name="name" value="张三"/>
    </bean>
</beans>

发现最终注入的还是dto下的Cat(可以通过上述的测试代码可以发现控制台输出的是:"小猫叫:miao~"而不是:"entity里的Cat",可以验证结论)

弊端: 必须保证每个类只能在配置文件里配置一次,一个类多次被配置时无法使用byType自动装配

通过注解 自动装配(@Autowire 、@Resource)

需要在配置文件中导入约束(context --- 共三个)并添加一项配置( context:annotation-config/) 才能支持注解的使用

context 约束:

  1. xmlns:context="http://www.springframework.org/schema/context"
    2.xsi:schemaLocation下的:" http://www.springframework.org/schema/context"
    3.xsi:schemaLocation下的:" https://www.springframework.org/schema/context/spring-context.xsd"
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"
    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>

@Autowire

原理:

  1. Autowire注解优先通过byType方式匹配bean,byType匹配不到再通过byName匹配
  2. 为了防止Autowire匹配不到bean,经常将Autowire与Qualifire搭配使用 通过 @Qualifire(value="配置文件中的bean id") 指定要匹配的bean 的id(Autowire + Qualifire 等价于 Resource)

用法

写在属性名上一行或者set方法上一行即可自动装配

推荐写在属性名上一行,因为属性名上使用此注解后,可以省略set方法(前提是配置文件中配置了对应的bean)

autowire的require属性:默认为true 代表这个属性的值不能为null,如果@Autowire(require = false) 则代表这个属性可以为null

@Resource

@Resource是jdk自带的注解

它可以指定name(bean的id),如@Resource(name="cat")

在不指定name的情况下:先byName,后byType。与Autowire的正好相反
用法

写在属性名上一行或者set方法上一行即可自动装配

推荐写在属性名上一行,因为属性名上使用此注解后,可以省略set方法(前提是配置文件中配置了对应的bean)

相关推荐
梁同学与Android31 分钟前
Android ---【内存优化】常见的内存泄露以及解决方案
android·java·内存泄漏
007php00739 分钟前
服务器上PHP环境安装与更新版本和扩展(安装PHP、Nginx、Redis、Swoole和OPcache)
运维·服务器·后端·nginx·golang·测试用例·php
武子康2 小时前
Java-71 深入浅出 RPC Dubbo 上手 父工程配置编写 附详细POM与代码
java·分布式·程序人生·spring·微服务·rpc·dubbo
武子康4 小时前
Java-72 深入浅出 RPC Dubbo 上手 生产者模块详解
java·spring boot·分布式·后端·rpc·dubbo·nio
_殊途4 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
椰椰椰耶5 小时前
【Spring】拦截器详解
java·后端·spring
没有bug.的程序员6 小时前
JAVA面试宝典 - 《MyBatis 进阶:插件开发与二级缓存》
java·面试·mybatis
brzhang6 小时前
我操,终于有人把 AI 大佬们 PUA 程序员的套路给讲明白了!
前端·后端·架构
没有羊的王K7 小时前
SSM框架学习——day1
java·学习