Spring dependency Injection
The dependency injection concept of beans
Dependency Injection: It is an IOC implementation at the heart of the Spring framework. When you write your program, you leave the creation of the object to Spring through inversion of control, but you can’t have code that doesn’t have dependencies. IOC decoupling only reduces their dependencies, but does not eliminate them. For example, the business layer still calls the methods of the persistence layer. The dependency between the business layer and the persistence layer will be maintained by Spring after spring is used. In simple terms, we simply sit back and wait for the framework to pass persistence layer objects into the business layer without having to fetch them ourselves.
Dependency injection of beans
- A constructor
- Create a constructor that takes arguments, and create a constructor that takes no arguments
- Configure injection when the Spring container calls a parameter construct
-
<bean id="userDao" class="com.dao.impl.UserDaoImpl"/> <bean id="userService" class="com.service.impl.UserServiceImpl"> <constructor-arg name="userDao" ref="userDao"></constructor-arg> </bean> Copy the code
- Set method
-
Set the set method on the object to be injected in the corresponding method
-
Configure the Spring container to call the set method for injection
-
<bean id="userDao" class="com.dao.impl.UserDaoImpl"/> <bean id="userService" class="com.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"/> </bean> Copy the code
-
There is a P namespace injection in the set method
The dependency injection data type of the bean
In addition to reference objects, collections and common data types can be injected in the Spring container
-
Ordinary data injection
-
<bean id="userDao" class="com.dao.impl.UserDaoImpl"> <property name="company" value="aaa"></property> <property name="age" value="15"></property> </bean> Copy the code
-
List collection injection
-
<bean id="userDao" class="com.dao.impl.UserDaoImpl"> <property name="strList"> <list> <value>aaa</value> <value>bbb</value> <value>ccc</value> </list> </property> </bean> Copy the code
-
Or inject a reference type into a list collection
<bean id="user1" class="com.zyl.domain.User"> <property name="age" value="12"></property> <property name="name" value="zyl"></property> </bean> <bean id="user2" class="com.zyl.domain.User"> <property name="age" value="13"></property> <property name="name" value="zyl2"></property> </bean> <bean id="userDao" class="com.zyl.dao.impl.UserDaoImpl"> <property name="userList"> <list> <ref bean="user1"/> <ref bean="user2"/> </list> </property> </bean> Copy the code
-
Map collection injection
<bean id="u1" class="com.domain.User"/> <bean id="u2" class="com.domain.User"/> <bean id="userDao" class="com.dao.impl.UserDaoImpl"> <property name="userMap"> <map> <entry key="user1" value-ref="u1"/> <entry key="user2" value-ref="u2"/> </map> </property> </bean>Copy the code
-
Properties collection injection
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"> <property name="properties"> <props> <prop key="p1">aaa</prop> <prop key="p2">bbb</prop> <prop key="p3">ccc</prop> </props> </property> </bean> Copy the code
Spring introduces additional configuration files
<import resource="applicationContext-xxx.xml"/>
Copy the code