[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
Spring dependency injection
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
simple type
1.3 Construction method injection
problem import
What subtag does constructor injection use?
reference type
simple type
Parameter adaptation [understand]
1.4 Dependency injection method selection
- 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
- Optional dependencies use setter injection for flexibility
- 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
- 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
- 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.
- 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
- Autowiring is used for reference type dependency injection and cannot operate on simple types
- 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
- 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
- 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