依赖注入之set注入

HeJin大约 1 分钟Spring全家桶Spring入门

1、普通值注入

<property name="name" value="张三"/>

2、Bean注入ref

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     https://www.springframework.org/schema/beans/spring-beans.xsd">

 <bean id="address" class="com.kuang.pojo.Address">
     <property name="address" value="贵州"/>
 </bean>

 <bean id="student" class="com.kuang.pojo.Student">
     <!--第二种,Bean注入,ref-->
     <property name="address" ref="address"/>
</beans>

3、数组注入array

<property name="books">
 <array>
     <value>红楼梦</value>
     <value>西游记</value>
     <value>三国演义</value>
 </array>
</property>

4、List注入list

<property name="hobbies">
 <list>
     <value>听歌</value>
     <value>敲代码</value>
     <value>看电影</value>
 </list>
</property>

5、map注入map

<property name="card">
 <map>
     <entry key="身份证" value="123456789"/>
     <entry key="银行卡" value="234567891011"/>
     <entry key="" value=""/>
 </map>
</property>

6、set注入,set

<property name="games">
 <set>
     <value>英雄联盟</value>
     <value>王者荣耀</value>
 </set>
</property>

7、空值注入null

<property name="wife">
 <null/>
</property>

8、Properties注入props

<property name="info">
 <props>
     <prop key="学号">111222</prop>
     <prop key="性别"></prop>
     <prop key="姓名">张三</prop>
     <prop key="password">123456</prop>
     <prop key="db">mysql</prop>
 </props>
</property>

结果:

Student{name='张三', address=Address{address='贵州'}, books=[红楼梦, 西游记, 三国演义], hobbies=[听歌, 敲代码, 看电影], card={身份证=123456789, 银行卡=234567891011, =}, games=[英雄联盟, 王者荣耀], wife='null', info={学号=111222, 性别=, db=mysql, password=123456, 姓名=张三}}