ApplicationContext.xml 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
  6. <bean id="studentOne" class="com.cdxw.spring.pojo.Student"></bean>
  7. <bean id="studentTwo" class="com.cdxw.spring.pojo.Student">
  8. <!--
  9. property:通过成员变量的set方法进行赋值
  10. name:设置需要赋值的属性名(和set方法有关)
  11. value:设置为属性所赋的值
  12. -->
  13. <property name="sid" value="1001"></property>
  14. <property name="sname" value="张三"></property>
  15. <property name="age" value="23"></property>
  16. <property name="gender" value="男"></property>
  17. </bean>
  18. <bean id="studentThree" class="com.cdxw.spring.pojo.Student">
  19. <constructor-arg value="1002"></constructor-arg>
  20. <constructor-arg value="李四"></constructor-arg>
  21. <constructor-arg value="24"></constructor-arg>
  22. <constructor-arg value="女"></constructor-arg>
  23. </bean>
  24. <bean id="studentFour" class="com.cdxw.spring.pojo.Student">
  25. <property name="sid" value="1003"></property>
  26. <property name="sname" value="王五"></property>
  27. <property name="gender">
  28. <null/>
  29. </property>
  30. </bean>
  31. <bean id="studentFive" class="com.cdxw.spring.pojo.Student">
  32. <property name="sid" value="1003"></property>
  33. <!--
  34. <:&lt;
  35. >:&gt;
  36. CDATA节其中的内容会原样解析<![CDATA[ ... ]]>
  37. CDATA节是xml中一个特殊的标签,因此不能写在一个属性中,只能以
  38. 标签的形式来使用
  39. -->
  40. <property name="sname" value="&lt;王五&gt;"></property>
  41. </bean>
  42. <bean id="studentSix" class="com.cdxw.spring.pojo.Student">
  43. <property name="sid" value="1003"></property>
  44. <property name="sname">
  45. <value><![CDATA[<王五>]]></value>
  46. </property>
  47. <property name="gender">
  48. <null/>
  49. </property>
  50. </bean>
  51. <bean id="studentSeven" class="com.cdxw.spring.pojo.Student">
  52. <property name="sid" value="1004"></property>
  53. <property name="sname" value="赵六"></property>
  54. <property name="age" value="26"></property>
  55. <property name="gender" value="男"></property>
  56. <!--ref:引用IOC容器中的某个bean的id,将对应的bean为属性赋值-->
  57. <property name="clazz" ref="clazzOne"></property>
  58. </bean>
  59. <bean id="clazzOne" class="com.cdxw.spring.pojo.Clazz">
  60. <property name="cid" value="1111"></property>
  61. <property name="cname" value="最强王者班"></property>
  62. <property name="students">
  63. <list>
  64. <ref bean="studentOne"></ref>
  65. <ref bean="studentTwo"></ref>
  66. <ref bean="studentThree"></ref>
  67. </list>
  68. </property>
  69. </bean>
  70. <bean id="studentEight" class="com.cdxw.spring.pojo.Student">
  71. <property name="sid" value="1004"></property>
  72. <property name="sname" value="赵六"></property>
  73. <property name="age" value="26"></property>
  74. <property name="gender" value="男"></property>
  75. <!--ref:引用IOC容器中的某个bean的id,将对应的bean为属性赋值-->
  76. <property name="clazz" ref="clazzOne"></property>
  77. <!--级联的方式,要保证提前为clazz属性赋值或者实例化-->
  78. <!-- <property name="clazz.cid" value="2222"></property>-->
  79. <!-- <property name="clazz.cname" value="远大前程班"></property>-->
  80. </bean>
  81. <bean id="studentNine" class="com.cdxw.spring.pojo.Student">
  82. <property name="sid" value="1004"></property>
  83. <property name="sname" value="赵六"></property>
  84. <property name="age" value="26"></property>
  85. <property name="gender" value="男"></property>
  86. <property name="clazz">
  87. <!--内部bean,只能在当前bean的内部使用,不能直接通过IOC容器获取-->
  88. <bean id="clazzInner" class="com.cdxw.spring.pojo.Clazz">
  89. <property name="cid" value="3333"></property>
  90. <property name="cname" value="宇宙第一班"></property>
  91. </bean>
  92. </property>
  93. <property name="hobby">
  94. <array>
  95. <value>抽烟</value>
  96. <value>喝酒</value>
  97. <value>烫头</value>
  98. </array>
  99. </property>
  100. </bean>
  101. <!--配置一个集合类型的bean,需要使用util的约束-->
  102. <util:list id="studentList">
  103. <ref bean="studentOne"></ref>
  104. <ref bean="studentTwo"></ref>
  105. <ref bean="studentThree"></ref>
  106. </util:list>
  107. <bean id="clazzTwo" class="com.cdxw.spring.pojo.Clazz">
  108. <property name="cid" value="1111"></property>
  109. <property name="cname" value="最强王者班"></property>
  110. <property name="students" ref="studentList"></property>
  111. </bean>
  112. <bean id="studentTen" class="com.cdxw.spring.pojo.Student">
  113. <property name="sid" value="1004"></property>
  114. <property name="sname" value="赵六"></property>
  115. <property name="age" value="26"></property>
  116. <property name="gender" value="男"></property>
  117. <property name="clazz">
  118. <!--内部bean,只能在当前bean的内部使用,不能直接通过IOC容器获取-->
  119. <bean id="clazzInner" class="com.cdxw.spring.pojo.Clazz">
  120. <property name="cid" value="2222"></property>
  121. <property name="cname" value="远大前程班"></property>
  122. </bean>
  123. </property>
  124. <property name="hobby">
  125. <array>
  126. <value>抽烟</value>
  127. <value>喝酒</value>
  128. <value>烫头</value>
  129. </array>
  130. </property>
  131. <property name="teacherMap">
  132. <map>
  133. <!--一个entry表示一个键值对-->
  134. <entry key="10086" value-ref="teacherOne"></entry>
  135. <entry key="10010" value-ref="teacherTwo"></entry>
  136. </map>
  137. </property>
  138. </bean>
  139. <!--配置teacher类的bean-->
  140. <bean id="teacherOne" class="com.cdxw.spring.pojo.Teacher">
  141. <property name="tid" value="10086"></property>
  142. <property name="tname" value="大宝"></property>
  143. </bean>
  144. <bean id="teacherTwo" class="com.cdxw.spring.pojo.Teacher">
  145. <property name="tid" value="10010"></property>
  146. <property name="tname" value="小宝"></property>
  147. </bean>
  148. <bean id="studentEleven" class="com.cdxw.spring.pojo.Student">
  149. <property name="sid" value="1004"></property>
  150. <property name="sname" value="赵六"></property>
  151. <property name="age" value="26"></property>
  152. <property name="gender" value="男"></property>
  153. <property name="clazz">
  154. <!--内部bean,只能在当前bean的内部使用,不能直接通过IOC容器获取-->
  155. <bean id="clazzInner" class="com.cdxw.spring.pojo.Clazz">
  156. <property name="cid" value="2222"></property>
  157. <property name="cname" value="远大前程班"></property>
  158. </bean>
  159. </property>
  160. <property name="hobby">
  161. <array>
  162. <value>抽烟</value>
  163. <value>喝酒</value>
  164. <value>烫头</value>
  165. </array>
  166. </property>
  167. <property name="teacherMap" ref="teacherMap"></property>
  168. </bean>
  169. <util:map id="teacherMap">
  170. <entry key="10086" value-ref="teacherOne"></entry>
  171. <entry key="10010" value-ref="teacherTwo"></entry>
  172. </util:map>
  173. <bean id="studentTwelve" class="com.cdxw.spring.pojo.Student"
  174. p:sid="1005" p:sname="小明" p:teacherMap-ref="teacherMap"></bean>
  175. </beans>