【Spring IOC容器】XML自动装配



自动装配

Bean的装配可以理解为依赖关系注入,Bean的装配方式即Bean依赖注入的方式。Spring容器支持多种形式的Bean的装配方式,如:基于xml的装配、基于注解(Annotation)的装配和自动装配等(其中最常用的是基于注解的装配。)

今天主要来了解一下自动装配的方式!

① 概念

自动装配就是根据指定装配规则(属性名称或者属性类型),Spring 自动将匹配的属性值进行注入。


② 自动装配类型

实现自动装配

  • bean 标签属性 autowire,配置自动装配
    • autowire 属性常用两个值:
      • byName 根据属性名称注入注入值 bean 的 id 值和类属性名称一样
      • byType 根据属性类型注入

Ⅰ.byName根据属性名称注入

▶Dept类

public class Dept {
    
    
    // 部门名称
    private String dName;
    // toString()
    @Override
    public String toString() {
    
    
        return "Dept{" +
                "dName='" + dName + '\'' +
                '}';
    }
}

▶Emp类

public class Emp {
    
    
    // 声明属性
    private Dept dept;
    // set()
    public void setDept(Dept dept){
    
    
        this.dept = dept;
    }
    // toString()
    @Override
    public String toString() {
    
    
        return "Emp{" +
                "dept=" + dept +
                '}';
    }
    // test()
    public void test(){
    
    
        System.out.println(dept);
    }
}

▶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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">

    <!--  配置创建对象  -->
    <!--实现自动装配
         bean 标签属性 autowire,配置自动装配
            autowire 属性常用两个值:
               byName 根据属性名称注入 ,注入值 bean 的 id 值和类属性名称一样
               byType 根据属性类型注入
      -->
    <bean id="emp" class="自动装配.Emp" autowire="byName">
        <!--           手动注入          -->
        <!--<property name="dept" ref="dept"></property>-->
    </bean>
    <bean  id="dept" class="自动装配.Dept"></bean>
</beans>

注入值 bean 的 id 值和类属性名称一样
在这里插入图片描述


▶Test类

public class Test {
    
    
    @org.junit.Test
    public void test_Emp(){
    
    
        try {
    
    
            ApplicationContext context = new ClassPathXmlApplicationContext("自动装配/bean_Emp.xml");
            Emp emp = context.getBean("emp",Emp.class);
            System.out.println(emp);
            emp.test();
        } catch (Exception e){
    
    
            e.printStackTrace();
        }
    }
}

在这里插入图片描述

返回顶部


Ⅱ.byType根据属性类型注入

  • 只要修改autowire属性值为byType即可

▶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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">

    <!--  配置创建对象  -->
    <!--实现自动装配
         bean 标签属性 autowire,配置自动装配
            autowire 属性常用两个值:
               byName 根据属性名称注入 ,注入值 bean 的 id 值和类属性名称一样
               byType 根据属性类型注入
      -->
    <bean id="emp" class="自动装配.Emp" autowire="byType">
        <!--           手动注入          -->
        <!--<property name="dept" ref="dept"></property>-->
    </bean>
    <bean  id="dept" class="自动装配.Dept"></bean>
</beans>
☠ 注意

当通过属性类型进行自动注入的时候,不能同时存在多个同类型的bean标签,否则Spring分不清楚该使用哪一个,就会报错。此时只能使用byName的形式进行注入~
在这里插入图片描述

返回顶部


猜你喜欢

转载自blog.csdn.net/qq_45797116/article/details/113571596