What is auto-assembly?

In the previous section, each property is injected with a property tag, such as:

    <bean id="book" class="com.pingguo.spring5.collectiontype.Book" scope="prototype">
        <property name="list" ref="bookList"></property>
    </bean>
Copy the code

This is manual assembly.

In autowiring, Spring automatically injects matching attribute values according to the specified assembly rule (attribute name or attribute type).

Automatic assembly process

1. Create two classes

These are the Department class and the Employee class.

package com.pingguo.spring5.autowire; public class Department { @Override public String toString() { return "Department{}"; }}Copy the code

The employee class has a department attribute that represents the department to which the employee belongs. The other methods are for later demonstration output.

package com.pingguo.spring5.autowire; public class Employee { private Department department; public void setDepartment(Department department) { this.department = department; } @Override public String toString() { return "Employee{" + "department=" + department + '}'; } public void test() { System.out.println(department); }}Copy the code

2. Configuration file

<? The 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="employee" class="com.pingguo.spring5.autowire.Employee"> <property name="department" ref="department"></property> </bean> <bean id="department" class="com.pingguo.spring5.autowire.Department"></bean> </beans>Copy the code

3. Test method

    @Test
    public void test5() {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean5.xml");
        Employee employee = context.getBean("employee", Employee.class);
        System.out.println(employee);
    }
Copy the code

Running results:

Employee{department=Department{}}

Process finished with exit code 0
Copy the code

Ok, up here, this is actually the manual assembly process.

Implement autowire in the configuration file via the property autowire in the bean tag:

  • Autowire =”byName” : automatic injection based on attribute name.
  • Autowire =”byType” : automatic injection based on attribute type.

1) byName demo

The id value of the injected bean is the same as the class attribute name, for example:

Modify the configuration file, add autowire=”byName”, and comment out the property.

<bean id="employee" class="com.pingguo.spring5.autowire.Employee" autowire="byName"> <! --<property name="department" ref="department"></property>--> </bean> <bean id="department" class="com.pingguo.spring5.autowire.Department"></bean>Copy the code

Execute the test function:

Employee{department=Department{}}

Process finished with exit code 0
Copy the code

The result is the same as that of manual assembly using property.

2) byType demo

The type of the bean to inject the value is the same as that in the property, for example:

Now go ahead and modify the configuration file, add autowire=”byType”, and comment out the property.

<bean id="employee" class="com.pingguo.spring5.autowire.Employee" autowire="byType"> <! --<property name="department" ref="department"></property>--> </bean> <bean id="department" class="com.pingguo.spring5.autowire.Department"></bean>Copy the code

Run the test again:

Employee{department=Department{}}

Process finished with exit code 0
Copy the code

The result is the same as that of manual assembly using property.

However, the use of autoneassembly in XML is rare in practice, and generally in the form of annotations, as we’ll learn later.