[Spring IOC container] XML automatic assembly



Automatic assembly

The assembly of Bean can be understood as dependency injection, and the way of Bean assembly is the way of Bean dependency injection. The Spring container supports many forms of Bean assembly methods, such as: xml-based assembly, annotation-based assembly and automatic assembly, etc. (The most commonly used is annotation-based assembly.)

Today, I mainly come to understand 自动装配the way!

① Concept

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


② Automatic assembly type

Realize automatic assembly

  • bean 标签Properties autowire, configure automatic assembly
    • There are two commonly used values ​​for the autowire attribute:
      • byName 根据属性名称注入, The id value of the injected value bean is the same as the class attribute name
      • byType 根据属性类型注入

Ⅰ.byName is injected according to the attribute name

▶Dept class

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

▶Emp class

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 configuration file

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

The id value of the injected value bean is the same as the class attribute name
Insert picture description here


▶Test class

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();
        }
    }
}

Insert picture description here

Back to top


Ⅱ.byType is injected according to the attribute type

  • Just modify the autowire attribute value to byType

▶XML configuration file

<?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>
☠ Note

When auto-injecting through the attribute type, multiple bean tags of the same type cannot exist at the same time, otherwise Spring will not know which one to use, and an error will be reported . At this time, it can only be injected in the form of byName~
Insert picture description here

Back to top


Guess you like

Origin blog.csdn.net/qq_45797116/article/details/113571596