spring自动注入中byName和byType

发布时间 2024-01-05 20:49:38作者: DoubleLi

spring自动注入中byName和byType
1,byName:
其实byName根据被注入的名称作为bean名称作为依赖查找,并将对象设置到该属性。(根据bean的id进行查找)

首先创建Student类:

public class Student {
    private String name;
    private String id;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", id='" + id + '\'' +
                '}';
    }

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

    public void setId(String id) {
        this.id = id;
    }
}
然后创建School类:

public class School {
    private String name;
    private String addrees;

    private Student student;

    @Override
    public String toString() {
        return "School{" +
                "name='" + name + '\'' +
                ", addrees='" + addrees + '\'' +
                ", student=" + student +
                '}';
    }

    public void setStudent(Student student) {
        this.student = student;
    }

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

    public void setAddrees(String addrees) {
        this.addrees = addrees;
    }
}
创建.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 class="java01.Student" id="student">
<property name="name" value="小明"/>
    <property name="id" value="022300190405"/>
</bean>

    <bean class="java01.School" id="school" autowire="byName">
        <property name="name" value="北师大"/>
        <property name="addrees" value="北京"/>
    </bean>
</beans>

创建测试类:

public class tests02 {
    @Test
    public void test(){
        ApplicationContext ioc = new ClassPathXmlApplicationContext("test01/teach-ioc.xml");
        Object bean01 = ioc.getBean("school");
    System.out.println(bean01);
    }
}

2,byType:
byType通过属性的类型查找javabean依赖的对象并为其注入

为应用指定多个 Spring 配置文件
在实际应用里,随着应用规模的增加,系统中 Bean 数量也大量增加,导致配置文件变 得非常庞大、臃肿。为了避免这种情况的产生,提高配置文件的可读性与可维护性,可以将 Spring 配置文件分解成多个配置文件。

包含关系的配置文件:
多个配置文件中有一个总文件,总配置文件将各其它子文件通过引入。在 Java 代码中只需要使用总配置文件对容器进行初始化即可。

 

创建新的.xml配置文件(命名tatle.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">
<import resource="classpath*:test01/spring-school.xml"/>
    <import resource="classpath*:/test01/spring-student.xml"/><!--导入配置文件-->
</beans>

创建测试类:

public class tests03 {
    @Test
    public void test()
    {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("test01/tatle.xml");
        Object bean = ioc.getBean("student");//这里的school是spring-student.xml中的bean
    System.out.println(bean);
    }
}

此时ioc.getbean调用的是spring-student中的id为student的bean。

运行结果:

 

当然也有可能会有小错误:

 

这是因为:

 

tatle.xml之前不要忘了它所在的包名。