Two ways of automatic injection in Spring

Table of contents

1 Introduction

 2. Set injection and construction injection.

3. Under what circumstances should automatic injection be used?

4. Automatic injection of byName

5. ByType injection of automatic injection


 

1 Introduction

Spring's core technology IOC (Intorol of Converse Control Inversion) is implemented through DI (Dependency Insert Dependency Injection). There are two ways to implement dependency injection (DI), xml and annotation. As shown below:

 2. Set injection and construction injection.

Set injection and construction injection, these two methods are the first way we learn to use the Spring container to create objects and assign values ​​when we first come into contact with Spring. I have written the specific code and detailed explanation in my previous article, here is the link: Click to see the detailed introduction and usage of set injection and construction injection.

http://t.csdn.cn/19XGd

3. Under what circumstances should automatic injection be used?

After we have learned set injection and construction injection, we may find that if the reference property of a certain class is also the property of other classes, if we use a lot of <propterty name="" ref=""> to Assigning values ​​to this reference attribute of other classes will appear very redundant. As follows:

<beans>

<bean id="a" class="">
    <property name="" value=""/>
    <property name="11" ref="22"/> <!-某个引用类型的属性是很多其他类的属性-->
</bean>

<bean id="b" class="">
    <property name="" value=""/>
    <property name="11" ref="22"/> <!-某个引用类型的属性是很多其他类的属性-->
</bean>


<bean id="c" class="">
    <property name="" value=""/>
    <property name="11" ref="22"/><!-某个引用类型的属性是很多其他类的属性-->
</bean>


<bean id="d" class="">
    <property name="" value=""/>
    <property name="11" ref="22"/><!-某个引用类型的属性是很多其他类的属性-->
</bean>


</beans>

The above writing method is very redundant, so is there a simple writing method? Of course there is, and this easy way is automatic injection.

4. Automatic injection of byName

The code immediately below is the byName method in automatic injection.

The feature of byName automatic injection is that an additional attribute is written in the <bean> tag, type="byName".

<bean id="" class="" type="byName">
    <property name="" value=""/> <!--给基本类型属性赋值,不能使用byName自动注入-->
    <property name="" ref=""/>  <!--引用类型属性赋值,可以使用byName自动注入,进而实现简写-->
</bean>

Requirements for automatic injection byName:

  • Must rely on the set() method, that is to say, only supports shorthand for the set injection method.
<bean>
    <property name="" value=""/>
    <property name="" ref=""/>
</bean>

 Construct injection is not supported.

<bean>
    <constructor-arg name="" value=""/>
</bean>
  • And it only supports using the byName method to assign values ​​to reference type attributes when assigning values ​​to reference type attributes; to assign values ​​to simple type attributes, you cannot use the byName construct to inject, and you need to write normally.

  • Then when assigning a value to a property of a reference type, byName automatic injection is used, so we don't need to write <property name="" ref="">. So how does byName implement the assignment to the reference type attribute?

The answer is: when Spring finds that you use automatic injection such as byName, it will automatically find the class class of the current <bean> tag, and check the reference attributes of this class, and then go to the current xml file, go to Compare to find out whether the id of other <bean> tags is equal to the reference attribute name of this class, and whether the class attribute value of other <bean> tags is consistent with the class of the reference attribute to be assigned currently. If both id and class match, then the reference type attribute will be assigned a value.

5. ByType injection of automatic injection

The code immediately below is the byType method in automatic injection.

The feature of byType automatic injection is that an additional attribute is written in the <bean> tag, type="byType". The method of automatic injection byType is almost the same as the method and requirements of byName, except that it is slightly different when matching assignments.

<bean id="" class="" type="byType">
    <property name="" value=""/> <!--给基本类型属性赋值,不能使用byType自动注入,需要正常写-->
    <property name="" ref=""/>  <!--引用类型属性赋值,可以使用byType自动注入,进而实现简写-->
</bean>

<!---->

Requirements for using byType automatic injection:

  • Must rely on the set method, so it must be a shorthand for the set injection method

Support set injection: as shown below

<bean>
    <property name="" value=""/>
    <property name="" ref=""/>
</bean>

Construct injection is not supported: as follows

<bean>
    <constructor-arg name="" value=""/>
</bean>
  • It only supports abbreviation when assigning values ​​​​to reference type attributes in set injection, and does not support assigning values ​​​​to simple types in set injection. (Assignment to simple types in set injection still needs to be written normally) as shown below
<bean id="" class="">
    <property name="" value=""/> <!--给基本类型属性赋值,不能使用byType自动注入,需要正常写-->
    <property name="" ref=""/>  <!--引用类型属性赋值,可以使用byType自动注入,进而实现简写-->
</bean>
  • Then, when assigning values ​​​​to attributes of reference types, byType automatic injection is used, so we don't need to write <property name="" ref="">. So how does byType realize the assignment to the reference type attribute?

The answer is: when Spring sees byType automatic injection, it will automatically match whether the class of other <bean> tags is the same as the reference type attribute of the object to be created, a subclass, or an interface implementation class. If so, the value of the matching reference type will be automatically assigned to the reference type attribute of the currently created object.

! ! Notice! ! :

Since Spring is a singleton mode, when using the <bean> tag that is automatically injected to match the attributes of the reference type, if two <bean> tags are matched, an error will be reported.

Guess you like

Origin blog.csdn.net/weixin_44362089/article/details/127347641