Spring's IOC, injection issues, including conditions and prerequisites for 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:
insert image description here

What is Property Injection

Attribute injection is to assign values ​​to the attributes in the object at the same time when the object is instantiated.
That is, in layman's terms, attribute injection is to assign values ​​to attributes in a class.

The method of attribute injection

Automatic injection of attributes can be achieved by configuring xml documents or using annotations

via the xml document

set injection and construction injection

set method injection
  • Precondition: There needs to be a set method for the corresponding attribute in the configured class
<!--   set方法注入属性-->
    <bean id="user" class="com.liu.test.User">
        <property name="name" value="root"/>
        <property name="password" value="666666"/>
        <property name="uid" value="0000000001"/>
    </bean>
  • property is the attribute
  • name is the attribute name, which is the name of the attribute to be injected in the class
  • value the value to inject
  • ref reference value, refer to other beans

Realize automatic injection of attributes through annotations
Let's first understand automatic injection

automatic injection

The so-called spring automatic injection means that when a component in the container needs to use another component (such as an aggregation relationship), it relies on the spring container to create the object instead of creating it manually.

autowire property
<bean id="person" class="class" autowire="*">

The autowire attribute has the following six values, and their descriptions are as follows:

  1. No: Autowiring is not enabled. Autowire default value.
  2. byName: Find the object that the JavaBean depends on by the name of the attribute and inject it. The Spring IoC container will look up the bean with the id/name attribute and the name of the injected attribute name in the configuration file, and then use the Set method to inject it. (If there is a bean of the same type as the specified attribute in the container, it will be automatically assembled with the attribute; if there are multiple beans of this type, an exception will be thrown, and it will be pointed out that the byType method cannot be used for automatic assembly; if no match is found bean, nothing happens, you can also set it by setting)
  3. byType: Find the object that the JavaBean depends on by the type of the attribute and inject it. The Spring IoC container will look for the bean whose Class property is the same as the injected property, and use the Set method to inject it.
  4. constructor: Like byType, it also searches for dependent objects by type. The difference with byType is that it does not use the Set method to inject, but uses the constructor to inject.
  5. autodetect: Automatically select the injection method between byType and constructor.
  6. default: Determined by the default-autowire attribute of the parent tag.
annotation scenes to be used illustrate
@AutoWired Can be applied to constructors, annotation types, methods, parameters in methods, fields or properties Autowiring based on property type
@Qualifier Can be applied to any element of a class, annotation type, method, parameter in a method, field or property According to the name of the property to inject and
@Value Can be applied to, annotation types, methods, parameters in methods, fields or properties Inject normal type properties

@Autowired
is added to the property, indicating that the property is automatically injected

@Qualifier(value="*")
plus attributes, q is to specify which type of file to automatically assemble

@Value("*")
is added to the attribute, indicating that the attribute is injected with the value of "*" when it is generated

@Autowired
    //q是指定自动装配哪个类型的文件
    @Qualifier("bookDao2")
    private BookDao bookDao ;

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 attribute of a certain class is also an attribute of other classes, if we use a lot of usage to assign values ​​​​to this reference attribute of other classes, it will be It seems very redundant. As follows:

<bean id="bookDao" name="bookDaoBM" class="com.dao.impl.BookDaoImpl" init-method="init" destroy-method="destory">
        <property name="connectionNum" value="6"></property>
        <property name="databaseName" value="ltx"></property>
    </bean>


    <bean id="bookService" name="bookServiceBM bookEbi" class="com.service.impl.BookServiceImpl">
        <!--7.配置server 与dao的关系
        property 标签表示配置当ibean的属性
        name届性表示阅置哪一个具体的属性
        ref属性表示参照哪个bean

        相当于我声明了bookService中有bookDao这个类
        -->
        <property name="bookDao" ref="bookDao"></property>
    </bean>

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.

Requirements for automatic injection of byName and byType:

  • 必须依赖set()方法,也就是说只支持对set注入方式的简写。

For details on the use of byName and byType, see the two methods of automatic injection in Spring

Setter injection and constructor injection can be seen in spring learning----bean02: bean dependency injection, automatic assembly and collection injection

Guess you like

Origin blog.csdn.net/missgrass/article/details/129092499