1.实现DI1
1.1DI给对象属性赋值
【1】通过set方法给对象属性赋值
<bean id="user2" class="org.example.User" > <property name="userId" value="1"></property> <property name="uname" value="漳卅"></property> <property name="password" value="abcdef"></property> </bean>
【2】通过有参构造方法给对象属性赋值
<bean id="user3" class="org.example.User" > <constructor-arg name="userId" value="1" ></constructor-arg> <constructor-arg name="uname" value="宿舍" ></constructor-arg> <constructor-arg name="password" value="asdads" ></constructor-arg> </bean>
【3】通过p名称空间和c名称空间实现DI
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="user4" class="org.example.User"p:userId="5" p:uname="晓东" p:password="123456" ></bean> <bean id="user6" class="org.example.User"c:userId="6" c:uname="1东" c:password="123456" ></bean>
2.实现DI2
2.1注入空值和特殊符号
3.实现DI3
3.1对象引用的方式
分为外部引用和内部引用,外部引用使用的多一些
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="date1" class="java.util.Date"></bean> <bean id="mouse" class="org.example.Mouse" > <property name="name" value="杰瑞"></property> <!--引用外部bean--> <property name="brith" ref="date1"></property> </bean> <bean id="cat" class="org.example.Cat"> <property name="name" value="汤姆"></property> <!--引用内部bean--> <property name="mouse1"> <bean class="org.example.Mouse"> <property name="name" value="杰瑞"></property> <property name="brith" > <bean class="java.util.Date"></bean> </property> </bean> </property> </bean> </beans>
4.实现DI4
4.1集合属性注入
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student" class="org.example.Student"> <!--数组属性注入--> <property name="books"> <array> <value>JAVA</value> <value>Mysql</value> <value>Spring</value> </array> </property> <!--set集合注入--> <property name="bookset"> <set> <value>JAVA</value> <value>Mysql</value> <value>Spring</value> </set> </property> <!--list集合注入--> <property name="bookList"> <list> <value>JAVA</value> <value>Mysql</value> <value>Spring</value> </list> </property> <!--map集合注入--> <property name="bookmap"> <map> <entry key="JAVA" value="马士兵"></entry> <entry key="Golang" value="马士兵"></entry> <entry key="JVM" value="马士兵"></entry> </map> </property> <!--list对象集合注入--> <property name="bookList2"> <list> <bean id="b1" class="org.example.Book"> <property name="bname" value="JAVA"></property> <property name="author" value="马士兵"></property> </bean> <bean id="b2" class="org.example.Book"> <property name="bname" value="Go"></property> <property name="author" value="马士兵"></property> </bean> <bean id="b3" class="org.example.Book"> <property name="bname" value="JVM"></property> <property name="author" value="马士兵"></property> </bean> <!--<ref bean="b1"></ref> <ref bean="b2"></ref> <ref bean="b3"></ref>--> </list> </property> </bean> <!--<bean id="b1" class="org.example.Book"> <property name="bname" value="wang"></property> <property name="author" value="xue"></property> </bean> <bean id="b2" class="org.example.Book"> <property name="bname" value="hu"></property> <property name="author" value="guo"></property> </bean>--> </beans>