狂神說Java:https://space.bilibili.com/95256449
環境搭建:008 DI依賴注入環境
1、普通值注入,value
<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, 姓名=張三}}