Spring dependency injection (DI configuration)

insert image description here
[Spring Technology] Spring Architecture
[Spring Technology] Spring IOC and DI Entry Case
[Spring Technology] Spring Bean Configuration and Examples
[Spring Technology] Spring Dependency Injection (DI Configuration)
[Spring Technology] Spring Third-Party Resource Configuration Management
[Spring Technology] Spring Container Technology
[Spring Technology] Spring Annotation Development
[Spring Technology] Spring Integration Mybatis&Junit Unit Test
[Spring Technology] Spring AOP
[Spring Technology] Spring Transaction Management

1. Dependency Injection Method [Key Point]

problem import

How many ways are there for dependency injection?

1.1 Two ways of dependency injection

  • setter injection
    simple type
    reference type (very common)
  • Constructor injection
    Simple type
    Reference type

1.2 Setter injection

problem import

What subtag is used for setter injection?

reference type

image-20210729203626540

simple type

image-20210729203728173

1.3 Construction method injection

problem import

What subtag does constructor injection use?

reference type

image-20210729203859855

simple type

image-20210729204006542

Parameter adaptation [understand]

image-20210729204117697

1.4 Dependency injection method selection

  1. Mandatory dependency is performed using the constructor, and there is a probability that the injection will not be performed using the setter injection, resulting in a null object
  2. Optional dependencies use setter injection for flexibility
  3. The Spring framework advocates the use of constructors, and most of the third-party frameworks use the form of constructor injection for data initialization, which is relatively rigorous
  4. If necessary, both can be used at the same time, use constructor injection to complete the injection of mandatory dependencies, and use setter injection to complete the injection of optional dependencies
  5. In the actual development process, it should be analyzed according to the actual situation. If the controlled object does not provide a setter method, constructor injection must be used.
  6. Self-developed modules are recommended to use setter injection

2. Rely on automatic assembly [understand]

problem import

How to configure autowiring by type?

2.1 The concept of automatic assembly

  • The process that the IoC container automatically finds and injects the bean in the container according to the resources that the bean depends on is called automatic assembly
  • Automatic assembly method
    By type (commonly used)
    By Name
    By Constructor
    Without Autowiring

2.2 Automatic assembly type

Dependency autowiring

Use the bean tag autowire attribute in the configuration to set the type of autowiring

<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl" autowire="byType"/>
Depends on autowiring features
  1. Autowiring is used for reference type dependency injection and cannot operate on simple types
  2. When using assembly by type (byType), the bean of the same type in the container must be guaranteed to be unique, and it is recommended to use
  3. When using assembly by name (byName), the bean with the specified name in the container must be guaranteed, because the variable name is coupled with the configuration, it is not recommended
  4. The priority of automatic assembly is lower than that of setter injection and constructor injection, and the automatic assembly configuration fails when it occurs at the same time

3. Collection Injection

3.1 Injecting array type data

<property name="array">
    <array>
        <value>100</value>
        <value>200</value>
        <value>300</value>
    </array>
</property>

3.2 Inject List type data

<property name="list">
    <list>
        <value>itcast</value>
        <value>itheima</value>
        <value>boxuegu</value>
        <value>chuanzhihui</value>
    </list>
</property>

3.3 Inject Set type data

<property name="set">
    <set>
        <value>itcast</value>
        <value>itheima</value>
        <value>boxuegu</value>
        <value>boxuegu</value>
    </set>
</property>

3.4 Inject Map type data

<property name="map">
    <map>
        <entry key="country" value="china"/>
        <entry key="province" value="henan"/>
        <entry key="city" value="kaifeng"/>
    </map>
</property>

3.5 Injecting Properties type data

<property name="properties">
    <props>
        <prop key="country">china</prop>
        <prop key="province">henan</prop>
        <prop key="city">kaifeng</prop>
    </props>
</property>

Explanation: The property tag indicates setter injection, and the constructor injection constructor-arg tag can also write <array>, <list>, <set>, <map>, <props> tags

Guess you like

Origin blog.csdn.net/qq_51808107/article/details/130316132