Detailed explanation of Spring's automatic assembly mechanism

1. Manual assembly

First of all, what does Spring assembly do? My understanding is that it is a means to resolve dependencies between beans.

For example, I have a People class and a Dog class here. People depends on Dog. That is to say, if a person owns a dog, there is a dependency relationship between people and dogs. We all know thatThe IOC container is responsible for managing the creation of objects and the maintenance of dependencies., then the dependency between dogs and people will naturally be managed by the Spring container, and this dependency needs to be assembled.

This article mainly explains automatic assembly, but before talking about automatic assembly, let's first take a look at how manual assembly is assembled. Through comparison, you will know how complex automatic assembly is!

① Entity class preparation, a People class and a Dog class

Insert image description here

Insert image description here

② Write the core configuration file and manually assemble bean dependencies

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dog" class="com.zxe.pojo.Dog"/>

    <bean id="people" class="com.zxe.pojo.People">
        <property name="name" value="栈老师"/>
        <property name="dog" ref="dog"/>
    </bean>

</beans>

可以看到,手动装配的 dog 依赖,是用 ref 引用的!

2. Automatic assembly

1. XML method

This method mainly uses the autowire attribute, and the attribute values ​​include byName and byType. Next, take byName as an example. The entity classes are still People and Dog above.

① Assemble in byName method

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dog" class="com.zxe.pojo.Dog"/>

    <bean id="people" class="com.zxe.pojo.People" autowire="byName">
        <property name="name" value="栈老师"/>
    </bean>

</beans>

Insert image description here

Here we need to further explain the difference between byName and byType:
① byName, as the name implies, is assembled by name. To inject a Dog into People, you need to take out the dog in the setDog method name and search it in the core configuration. What are you looking for? Find the bean with the id name dog, and automatically inject it after finding it 注意从实体类里面拿到的这个 dog 一定是 set 后面的内容,并不是变量名;
② byType, it is assembled by type, take out the Dog type in People, find the bean with class Dog in the configuration file, and then automatically inject it bean 的类型肯定就不能一样了,否则无法找到.

2. Annotation method

Another most commonly used assembly method is annotation assembly, which is implemented through reflection.

① Turn on annotation support,<context:annotation-config/>

<?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  http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>
    <bean id="dog" class="com.zxe.pojo.Dog"/>

    <bean id="people" class="com.zxe.pojo.People">
        <property name="name" value="栈老师"/>
    </bean>

</beans>

② Use @Autowired annotation to annotate dependency attributes

Insert image description here

The process of assembling attributes by annotation methods. By default, @Autowired will give priority to finding the corresponding component in the container according to the type of the attribute, and assign the value when found. If multiple components of the same type are found, we can use @Autowired in combination with @Qualifier, @Qualifier It is searched based on the name.

Guess you like

Origin blog.csdn.net/m0_52861684/article/details/132448985