Spring 的依赖注入

打印 上一主题 下一主题

主题 915|帖子 915|积分 2745

Spring 的依赖注入

@
目录

每博一文案
  1. "在千千万万个选择里",我永远选择去做哪些我认为值得的事,我可能干得很漂亮,也可能搞得一塌糊涂。
  2. 但没关系,重要的是我为之努力过。”我们很难做好每件事,让人生不留下任何遗憾,尽力而为就好“享受
  3. 生活的过程,接受结果。”人生是用来体验的,不是用来演绎完美的,我慢慢能接受自己身上哪些灰暗的部分,原谅自己
  4. 的迟钝和平庸,允许自己出错,允许自己偶尔断电,带着缺憾拼命绽放,这是与自己达成和解的唯一方式。”
  5. 尽力就好,允许所有的事与愿违。和不适合你的过去说再见,哪些伤痛的不堪的,霉烂的过去绝口不提。
  6. 太阳的起落在告诉我们,永远会有崭新的一天。
  7. “真正有价值的事情,都不是轻松舒服就能完成的”。那些晨间的寂静,不眠的星光,清醒的克制,
  8. 孤军奋战的坚持,暗暗许下的承诺,才是我热爱自己的时刻。"人生就是一步一步地打怪升级,坚持
  9. 下去,你所执着的努力一定会有所收获",
  10.     <bean id="" >
  11. <?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.     <bean id="peopleBean" >
  16.     </bean>
  17. </beans><constructor-arg name="" value=""></constructor-arg>
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <beans xmlns="http://www.springframework.org/schema/beans"
  20.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  22.     <bean id="peopleBean" >
  23.     </bean>
  24. </beans><constructor-arg name="" ref=""></constructor-arg>
  25.     </bean><?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  29.     <bean id="peopleBean" >
  30.     </bean>
  31. </beans>       ——————《网友的评论》
复制代码
1. 依赖注入

依赖注入实现了控制反转的思想:

  • Spring通过依赖注入的方式来完成Bean(类/对象)的管理
  • Bean的管理:Bean对象的创建,以及Bean对象中属性的赋值(或者叫做Bean对象之间的关联的维护)
依赖注入:

  • 依赖指的是对象和对象 之间的关联关系。
  • 注入指的是一种数据传递行为,通过注入行为来让对象和对象产生关系。
依赖注入常见的实现方式包括两种:

  • 第一种:set注入
  • 第二种: 构造注入
**准备工作:通过 maven 导入对应 spring6 的相关jar **
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. <?xml version="1.0" encoding="UTF-8"?>
  4. <beans xmlns="http://www.springframework.org/schema/beans"
  5.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  7.     <bean id="peopleBean" >
  8.     </bean>
  9. </beans> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10. <?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  14.     <bean id="peopleBean" >
  15.     </bean>
  16. </beans> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  17.     <modelVersion>4.0.0</modelVersion>
  18.     <groupId>org.example</groupId>
  19.     <artifactId>spring6-003-dependency-injection-blog</artifactId>
  20.     <version>1.0-SNAPSHOT</version>
  21.    
  22.     <packaging>jar</packaging>
  23.     <properties>
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans><maven.compiler.source>17</maven.compiler.source>
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans><maven.compiler.target>17</maven.compiler.target>
  38.     </properties>
  39.    
  40.     <dependencies>
  41. <?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  45.     <bean id="peopleBean" >
  46.     </bean>
  47. </beans>
  48. <?xml version="1.0" encoding="UTF-8"?>
  49. <beans xmlns="http://www.springframework.org/schema/beans"
  50.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  51.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  52.     <bean id="peopleBean" >
  53.     </bean>
  54. </beans>
  55. <?xml version="1.0" encoding="UTF-8"?>
  56. <beans xmlns="http://www.springframework.org/schema/beans"
  57.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  58.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  59.     <bean id="peopleBean" >
  60.     </bean>
  61. </beans>
  62. <?xml version="1.0" encoding="UTF-8"?>
  63. <beans xmlns="http://www.springframework.org/schema/beans"
  64.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  65.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  66.     <bean id="peopleBean" >
  67.     </bean>
  68. </beans><dependency>
  69. <?xml version="1.0" encoding="UTF-8"?>
  70. <beans xmlns="http://www.springframework.org/schema/beans"
  71.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  72.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  73.     <bean id="peopleBean" >
  74.     </bean>
  75. </beans>    <groupId>org.springframework</groupId>
  76. <?xml version="1.0" encoding="UTF-8"?>
  77. <beans xmlns="http://www.springframework.org/schema/beans"
  78.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  79.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  80.     <bean id="peopleBean" >
  81.     </bean>
  82. </beans>    <artifactId>spring-context</artifactId>
  83. <?xml version="1.0" encoding="UTF-8"?>
  84. <beans xmlns="http://www.springframework.org/schema/beans"
  85.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  86.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  87.     <bean id="peopleBean" >
  88.     </bean>
  89. </beans>    <version>6.0.3</version>
  90. <?xml version="1.0" encoding="UTF-8"?>
  91. <beans xmlns="http://www.springframework.org/schema/beans"
  92.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  93.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  94.     <bean id="peopleBean" >
  95.     </bean>
  96. </beans></dependency>
  97. <?xml version="1.0" encoding="UTF-8"?>
  98. <beans xmlns="http://www.springframework.org/schema/beans"
  99.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  100.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  101.     <bean id="peopleBean" >
  102.     </bean>
  103. </beans>
  104. <?xml version="1.0" encoding="UTF-8"?>
  105. <beans xmlns="http://www.springframework.org/schema/beans"
  106.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  107.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  108.     <bean id="peopleBean" >
  109.     </bean>
  110. </beans><dependency>
  111. <?xml version="1.0" encoding="UTF-8"?>
  112. <beans xmlns="http://www.springframework.org/schema/beans"
  113.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  114.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  115.     <bean id="peopleBean" >
  116.     </bean>
  117. </beans>    <groupId>junit</groupId>
  118. <?xml version="1.0" encoding="UTF-8"?>
  119. <beans xmlns="http://www.springframework.org/schema/beans"
  120.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  121.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  122.     <bean id="peopleBean" >
  123.     </bean>
  124. </beans>    <artifactId>junit</artifactId>
  125. <?xml version="1.0" encoding="UTF-8"?>
  126. <beans xmlns="http://www.springframework.org/schema/beans"
  127.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  128.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  129.     <bean id="peopleBean" >
  130.     </bean>
  131. </beans>    <version>4.13.2</version>
  132. <?xml version="1.0" encoding="UTF-8"?>
  133. <beans xmlns="http://www.springframework.org/schema/beans"
  134.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  135.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  136.     <bean id="peopleBean" >
  137.     </bean>
  138. </beans>    <scope>test</scope>
  139. <?xml version="1.0" encoding="UTF-8"?>
  140. <beans xmlns="http://www.springframework.org/schema/beans"
  141.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  142.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  143.     <bean id="peopleBean" >
  144.     </bean>
  145. </beans></dependency>
  146. <?xml version="1.0" encoding="UTF-8"?>
  147. <beans xmlns="http://www.springframework.org/schema/beans"
  148.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  149.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  150.     <bean id="peopleBean" >
  151.     </bean>
  152. </beans><dependency>
  153. <?xml version="1.0" encoding="UTF-8"?>
  154. <beans xmlns="http://www.springframework.org/schema/beans"
  155.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  156.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  157.     <bean id="peopleBean" >
  158.     </bean>
  159. </beans>    <groupId>org.apache.logging.log4j</groupId>
  160. <?xml version="1.0" encoding="UTF-8"?>
  161. <beans xmlns="http://www.springframework.org/schema/beans"
  162.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  163.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  164.     <bean id="peopleBean" >
  165.     </bean>
  166. </beans>    <artifactId>log4j-core</artifactId>
  167. <?xml version="1.0" encoding="UTF-8"?>
  168. <beans xmlns="http://www.springframework.org/schema/beans"
  169.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  170.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  171.     <bean id="peopleBean" >
  172.     </bean>
  173. </beans>    <version>2.19.0</version>
  174. <?xml version="1.0" encoding="UTF-8"?>
  175. <beans xmlns="http://www.springframework.org/schema/beans"
  176.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  177.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  178.     <bean id="peopleBean" >
  179.     </bean>
  180. </beans></dependency>
  181. <?xml version="1.0" encoding="UTF-8"?>
  182. <beans xmlns="http://www.springframework.org/schema/beans"
  183.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  184.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  185.     <bean id="peopleBean" >
  186.     </bean>
  187. </beans><dependency>
  188. <?xml version="1.0" encoding="UTF-8"?>
  189. <beans xmlns="http://www.springframework.org/schema/beans"
  190.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  191.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  192.     <bean id="peopleBean" >
  193.     </bean>
  194. </beans>    <groupId>org.apache.logging.log4j</groupId>
  195. <?xml version="1.0" encoding="UTF-8"?>
  196. <beans xmlns="http://www.springframework.org/schema/beans"
  197.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  198.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  199.     <bean id="peopleBean" >
  200.     </bean>
  201. </beans>    <artifactId>log4j-slf4j2-impl</artifactId>
  202. <?xml version="1.0" encoding="UTF-8"?>
  203. <beans xmlns="http://www.springframework.org/schema/beans"
  204.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  205.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  206.     <bean id="peopleBean" >
  207.     </bean>
  208. </beans>    <version>2.19.0</version>
  209. <?xml version="1.0" encoding="UTF-8"?>
  210. <beans xmlns="http://www.springframework.org/schema/beans"
  211.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  212.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  213.     <bean id="peopleBean" >
  214.     </bean>
  215. </beans></dependency>
  216.     </dependencies>
  217. </project>
复制代码
1.1 构造注入

所谓的构造注入:核心就是:调用了对应的构造方法,进行一个类/对象的属性赋值
既然要调用构造方法,进行一个属性的赋值的话,那么我们的对应属性的赋值的,构造方法必须存在才行。
构造注入:是在对象创建的时刻进行注入的。
重点:构造注入的使用的标签是:
  1. [/code][size=3]1.1.1 通过参数名进行构造注入[/size]
  2. [b]格式:[/b]
  3. [code]    <bean id="" >
  4. <?xml version="1.0" encoding="UTF-8"?>
  5. <beans xmlns="http://www.springframework.org/schema/beans"
  6.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  8.     <bean id="peopleBean" >
  9.     </bean>
  10. </beans><constructor-arg name="" value=""></constructor-arg>
  11. <?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.     <bean id="peopleBean" >
  16.     </bean>
  17. </beans><constructor-arg name="" ref=""></constructor-arg>
  18.     </bean>
复制代码
  1. package com.rainbowsea.bean;
  2. public class Cat {
  3.     private String name;
  4.     @Override
  5.     public String toString() {
  6. <?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.     <bean id="peopleBean" >
  11.     </bean>
  12. </beans>return "Cat{" +
  13. <?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="testDateBean" >
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <beans xmlns="http://www.springframework.org/schema/beans"
  20.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  22.     <bean id="peopleBean" >
  23.     </bean>
  24. </beans><property name="date" value="2023 -05-6"></property>
  25.     </bean>
  26. </beans>"name='" + name + '\'' +
  27. <?xml version="1.0" encoding="UTF-8"?>
  28. <beans xmlns="http://www.springframework.org/schema/beans"
  29.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  30.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  31.     <bean id="testDateBean" >
  32. <?xml version="1.0" encoding="UTF-8"?>
  33. <beans xmlns="http://www.springframework.org/schema/beans"
  34.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  35.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  36.     <bean id="peopleBean" >
  37.     </bean>
  38. </beans><property name="date" value="2023 -05-6"></property>
  39.     </bean>
  40. </beans>'}';
  41.     }
  42. }
复制代码
  1. package com.rainbowsea.bean;
  2. public class Zoon {
  3.     private String zoonName;
  4.     private Cat cat;
  5.     public Zoon(String zoonName, Cat cat) {
  6. <?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.     <bean id="peopleBean" >
  11.     </bean>
  12. </beans>this.zoonName = zoonName;
  13. <?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="peopleBean" >
  18.     </bean>
  19. </beans>this.cat = cat;
  20. <?xml version="1.0" encoding="UTF-8"?>
  21. <beans xmlns="http://www.springframework.org/schema/beans"
  22.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  23.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  24.     <bean id="peopleBean" >
  25.     </bean>
  26. </beans>System.out.println("执行了该 Zoon的构造器");
  27.     }
  28.     @Override
  29.     public String toString() {
  30. <?xml version="1.0" encoding="UTF-8"?>
  31. <beans xmlns="http://www.springframework.org/schema/beans"
  32.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  33.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  34.     <bean id="peopleBean" >
  35.     </bean>
  36. </beans>return "Zoon{" +
  37. <?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  41.     <bean id="testDateBean" >
  42. <?xml version="1.0" encoding="UTF-8"?>
  43. <beans xmlns="http://www.springframework.org/schema/beans"
  44.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  45.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  46.     <bean id="peopleBean" >
  47.     </bean>
  48. </beans><property name="date" value="2023 -05-6"></property>
  49.     </bean>
  50. </beans>"zoonName='" + zoonName + '\'' +
  51. <?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.     <bean id="testDateBean" >
  56. <?xml version="1.0" encoding="UTF-8"?>
  57. <beans xmlns="http://www.springframework.org/schema/beans"
  58.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  60.     <bean id="peopleBean" >
  61.     </bean>
  62. </beans><property name="date" value="2023 -05-6"></property>
  63.     </bean>
  64. </beans>", cat=" + cat +
  65. <?xml version="1.0" encoding="UTF-8"?>
  66. <beans xmlns="http://www.springframework.org/schema/beans"
  67.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  68.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  69.     <bean id="testDateBean" >
  70. <?xml version="1.0" encoding="UTF-8"?>
  71. <beans xmlns="http://www.springframework.org/schema/beans"
  72.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  73.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  74.     <bean id="peopleBean" >
  75.     </bean>
  76. </beans><property name="date" value="2023 -05-6"></property>
  77.     </bean>
  78. </beans>'}';
  79.     }
  80. }
复制代码
spring.xml
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean>   
复制代码
运行测试:
  1. package com.rainbowsea.test;
  2. import com.rainbowsea.bean.Zoon;
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. public class SpringStructure {
  7.     @Test
  8.     public void test() {
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans>// 获取到对应的 spring6当中的xml的,容器对象
  16. <?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.     <bean id="peopleBean" >
  21.     </bean>
  22. </beans>// 面向接口编程,左边为接口,
  23. <?xml version="1.0" encoding="UTF-8"?>
  24. <beans xmlns="http://www.springframework.org/schema/beans"
  25.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  26.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  27.     <bean id="peopleBean" >
  28.     </bean>
  29. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-structure.xml");
  30. <?xml version="1.0" encoding="UTF-8"?>
  31. <beans xmlns="http://www.springframework.org/schema/beans"
  32.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  33.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  34.     <bean id="peopleBean" >
  35.     </bean>
  36. </beans>// 通过 id 获取到对应的类/class
  37. <?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  41.     <bean id="peopleBean" >
  42.     </bean>
  43. </beans>Zoon zoonBean = applicationContext.getBean("zoonBean", Zoon.class);
  44. <?xml version="1.0" encoding="UTF-8"?>
  45. <beans xmlns="http://www.springframework.org/schema/beans"
  46.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  47.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  48.     <bean id="peopleBean" >
  49.     </bean>
  50. </beans>System.out.println(zoonBean);
  51.     }
  52. }
复制代码

测试如果,我们将构造方法删除了,就不行了,报如下错误:


1.1.2 通过参数的下标,进行构造注入

格式:
  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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <beans xmlns="http://www.springframework.org/schema/beans"
  7.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  8.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  9.     <bean id="peopleBean" >
  10.     </bean>
  11. </beans><constructor-arg index="0" value=""></constructor-arg>
  12. <?xml version="1.0" encoding="UTF-8"?>
  13. <beans xmlns="http://www.springframework.org/schema/beans"
  14.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  15.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  16.     <bean id="peopleBean" >
  17.     </bean>
  18. </beans><constructor-arg index="1" ref=""></constructor-arg>
  19.     </bean>
  20. </beans>
复制代码
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean>   
复制代码
运行测试:

1.1.3 不指定参数下标,不指定参数名字,通过自动装配的方式

格式:但是这种方式不建议:因为可读性十分的差。
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean>   
复制代码
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean>   
复制代码
运行测试:
1.2 set 注入

set 注入顾名思义:是基于 set () 方法实现的,底层通过反射机制调用属性对应的 set() 方法然后给属性赋值。这种方式 要求属性必须对外提供 set() 方法。
  1. package com.rainbowsea.bean;
  2. public class Cat {
  3.     private String name;
  4.     public String getName() {
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <beans xmlns="http://www.springframework.org/schema/beans"
  7.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  8.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  9.     <bean id="peopleBean" >
  10.     </bean>
  11. </beans>return name;
  12.     }
  13.     @Override
  14.     public String toString() {
  15. <?xml version="1.0" encoding="UTF-8"?>
  16. <beans xmlns="http://www.springframework.org/schema/beans"
  17.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  18.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  19.     <bean id="peopleBean" >
  20.     </bean>
  21. </beans>return "Cat{" +
  22. <?xml version="1.0" encoding="UTF-8"?>
  23. <beans xmlns="http://www.springframework.org/schema/beans"
  24.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  25.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  26.     <bean id="testDateBean" >
  27. <?xml version="1.0" encoding="UTF-8"?>
  28. <beans xmlns="http://www.springframework.org/schema/beans"
  29.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  30.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  31.     <bean id="peopleBean" >
  32.     </bean>
  33. </beans><property name="date" value="2023 -05-6"></property>
  34.     </bean>
  35. </beans>"name='" + name + '\'' +
  36. <?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  40.     <bean id="testDateBean" >
  41. <?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  45.     <bean id="peopleBean" >
  46.     </bean>
  47. </beans><property name="date" value="2023 -05-6"></property>
  48.     </bean>
  49. </beans>'}';
  50.     }
  51. }
复制代码
  1. package com.rainbowsea.bean;
  2. public class Zoon {
  3.     private String zoonName;
  4.     private Cat cat;
  5.     // set 注入,必须要提供 set() 方法
  6.     public void setZoonName(String zoonName) {
  7. <?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>this.zoonName = zoonName;
  14.     }
  15.     public void setCat(Cat cat) {
  16. <?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.     <bean id="peopleBean" >
  21.     </bean>
  22. </beans>this.cat = cat;
  23.     }
  24.     @Override
  25.     public String toString() {
  26. <?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.     <bean id="peopleBean" >
  31.     </bean>
  32. </beans>return "Zoon{" +
  33. <?xml version="1.0" encoding="UTF-8"?>
  34. <beans xmlns="http://www.springframework.org/schema/beans"
  35.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  36.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  37.     <bean id="testDateBean" >
  38. <?xml version="1.0" encoding="UTF-8"?>
  39. <beans xmlns="http://www.springframework.org/schema/beans"
  40.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  42.     <bean id="peopleBean" >
  43.     </bean>
  44. </beans><property name="date" value="2023 -05-6"></property>
  45.     </bean>
  46. </beans>"zoonName='" + zoonName + '\'' +
  47. <?xml version="1.0" encoding="UTF-8"?>
  48. <beans xmlns="http://www.springframework.org/schema/beans"
  49.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  50.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  51.     <bean id="testDateBean" >
  52. <?xml version="1.0" encoding="UTF-8"?>
  53. <beans xmlns="http://www.springframework.org/schema/beans"
  54.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  55.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  56.     <bean id="peopleBean" >
  57.     </bean>
  58. </beans><property name="date" value="2023 -05-6"></property>
  59.     </bean>
  60. </beans>", cat=" + cat +
  61. <?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  65.     <bean id="testDateBean" >
  66. <?xml version="1.0" encoding="UTF-8"?>
  67. <beans xmlns="http://www.springframework.org/schema/beans"
  68.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  69.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  70.     <bean id="peopleBean" >
  71.     </bean>
  72. </beans><property name="date" value="2023 -05-6"></property>
  73.     </bean>
  74. </beans>'}';
  75.     }
  76. }
复制代码

set注入的格式如下:
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20. <?xml version="1.0" encoding="UTF-8"?>
  21. <beans xmlns="http://www.springframework.org/schema/beans"
  22.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  23.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  24.     <bean id="peopleBean" >
  25.     </bean>
  26. </beans><constructor-arg index="0" value=""></constructor-arg>
  27. <?xml version="1.0" encoding="UTF-8"?>
  28. <beans xmlns="http://www.springframework.org/schema/beans"
  29.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  30.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  31.     <bean id="peopleBean" >
  32.     </bean>
  33. </beans><constructor-arg index="1" ref=""></constructor-arg>
  34.     </bean>
  35. </beans>
复制代码
需要注意的是:

  • 其中的  中的 name 必须是 对应类当中的 set() 方法。去了,set,其次是 首字母小写 。这是不可以乱写的。
  • Eg:
  1. 说明property标签的name是:setUserDao()方法名演变得到的。演变的规律是:
  2. ● setUsername() 演变为 username
  3. ● setPassword() 演变为 password
  4. ● setUserDao() 演变为 userDao
  5. ● setUserService() 演变为 userService
复制代码

  • 具体的如下图所示:

运行测试:
  1. package com.rainbowsea.test;
  2. import com.rainbowsea.bean.Zoon;
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. public class SpringStructure {
  7.     @Test
  8.     public void test() {
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans>// 获取到对应的 spring6当中的xml的,容器对象
  16. <?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.     <bean id="peopleBean" >
  21.     </bean>
  22. </beans>// 面向接口编程,左边为接口,
  23. <?xml version="1.0" encoding="UTF-8"?>
  24. <beans xmlns="http://www.springframework.org/schema/beans"
  25.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  26.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  27.     <bean id="peopleBean" >
  28.     </bean>
  29. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-set.xml");
  30. <?xml version="1.0" encoding="UTF-8"?>
  31. <beans xmlns="http://www.springframework.org/schema/beans"
  32.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  33.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  34.     <bean id="peopleBean" >
  35.     </bean>
  36. </beans>// 通过 id 获取到对应的类/class
  37. <?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  41.     <bean id="peopleBean" >
  42.     </bean>
  43. </beans>Zoon zoonBean = applicationContext.getBean("zoonBean", Zoon.class);
  44. <?xml version="1.0" encoding="UTF-8"?>
  45. <beans xmlns="http://www.springframework.org/schema/beans"
  46.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  47.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  48.     <bean id="peopleBean" >
  49.     </bean>
  50. </beans>System.out.println(zoonBean);
  51.     }
  52. }
复制代码

测试:我们如果把: 对应的 set 方法注释掉了,运行测试一下。

从结果上,我们可以看出:set 方法是必须 存在的。
set 注入的简单总结:

  • 实现原理:
通过property标签获取到属性名:userDao:
通过性名推断出set方法名:setUserDao
通过反射机制调用setUserDao()方法给属性赋值
property标签的 name是属性名。
property标签的ref是要注入的bean对象的id。(通过ref属性来完成bean的装配,这是bean最简单的一种装配方式。装配指的是:创建系统组件之间关联的动作)
set注入的核心实现原理:通过反射机制调用set方法来给属性赋值,让两个对象之间产生关系。
2. set注入的各种方式详解

实际上在实际的开发过程中,我们使用的更多的是 set()方法的注入 。
2.1 set 注入外部Bean

外部Bean的特点: bean定义到外面,在property标签中使用ref 属性或是 value 属性进行注入。通常这种方式是常用。
格式如下:
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean>   
复制代码

上面:我们测试用的 set 注入的方式:用的都是这种外部 Bean 的方式。
2.2 set 注入内部Bean

内部Bean的方式:在bean标签中嵌套bean标签:
具体格式如下:
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean>    <bean id="" >
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans><constructor-arg name="" value=""></constructor-arg>
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans><constructor-arg name="" ref=""></constructor-arg>
  31.     </bean>
复制代码

运行测试:

2.3 set 注入类型

2.3.1 set 注入简单类型
  1. package com.rainbowsea.bean;
  2. public class Cat {
  3.     private int age;
  4.     private String name;
  5.     // set注入:底层反射调用 set 方法
  6.     public void setAge(int age) {
  7. <?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>this.age = age;
  14.     }
  15.     public void setName(String name) {
  16. <?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.     <bean id="peopleBean" >
  21.     </bean>
  22. </beans>this.name = name;
  23.     }
  24.     @Override
  25.     public String toString() {
  26. <?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.     <bean id="peopleBean" >
  31.     </bean>
  32. </beans>return "Cat{" +
  33. <?xml version="1.0" encoding="UTF-8"?>
  34. <beans xmlns="http://www.springframework.org/schema/beans"
  35.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  36.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  37.     <bean id="testDateBean" >
  38. <?xml version="1.0" encoding="UTF-8"?>
  39. <beans xmlns="http://www.springframework.org/schema/beans"
  40.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  42.     <bean id="peopleBean" >
  43.     </bean>
  44. </beans><property name="date" value="2023 -05-6"></property>
  45.     </bean>
  46. </beans>"age=" + age +
  47. <?xml version="1.0" encoding="UTF-8"?>
  48. <beans xmlns="http://www.springframework.org/schema/beans"
  49.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  50.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  51.     <bean id="testDateBean" >
  52. <?xml version="1.0" encoding="UTF-8"?>
  53. <beans xmlns="http://www.springframework.org/schema/beans"
  54.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  55.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  56.     <bean id="peopleBean" >
  57.     </bean>
  58. </beans><property name="date" value="2023 -05-6"></property>
  59.     </bean>
  60. </beans>", name='" + name + '\'' +
  61. <?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  65.     <bean id="testDateBean" >
  66. <?xml version="1.0" encoding="UTF-8"?>
  67. <beans xmlns="http://www.springframework.org/schema/beans"
  68.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  69.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  70.     <bean id="peopleBean" >
  71.     </bean>
  72. </beans><property name="date" value="2023 -05-6"></property>
  73.     </bean>
  74. </beans>'}';
  75.     }
  76. }
复制代码
格式:
在set 注入当中:简单类型的注使用 value 标签属性,进行一个属性的赋值操作
需要特别注意:如果给简单类型赋值,使用value属性或value标签。而不是ref。
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean>
复制代码
运行测试:
  1. package com.rainbowsea.test;
  2. import com.rainbowsea.bean.Cat;
  3. import com.rainbowsea.bean.Zoon;
  4. import org.junit.Test;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. public class SpringStructure {
  8.     @Test
  9.     public void test() {
  10. <?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  14.     <bean id="peopleBean" >
  15.     </bean>
  16. </beans>// 获取到对应的 spring6当中的xml的,容器对象
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans>// 面向接口编程,左边为接口,
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sets.xml");
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans>// 通过 id 获取到对应的类/class
  38. <?xml version="1.0" encoding="UTF-8"?>
  39. <beans xmlns="http://www.springframework.org/schema/beans"
  40.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  42.     <bean id="peopleBean" >
  43.     </bean>
  44. </beans>Cat catBean = applicationContext.getBean("catBean", Cat.class);
  45. <?xml version="1.0" encoding="UTF-8"?>
  46. <beans xmlns="http://www.springframework.org/schema/beans"
  47.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  48.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  49.     <bean id="peopleBean" >
  50.     </bean>
  51. </beans>System.out.println(catBean);
  52.     }
  53. }
复制代码

既然我们知道了:简单类型如何使用 set 注入了,那么我们就需要知道哪些是 简单类型了 ?
简单类型包括哪些呢?可以通过Spring的源码来分析一下:BeanUtils类

  1.         /**
  2.          * Check if the given type represents a "simple" value type: a primitive or
  3.          * primitive wrapper, an enum, a String or other CharSequence, a Number, a
  4.          * Date, a Temporal, a URI, a URL, a Locale, or a Class.
  5.          * <p>{@code Void} and {@code void} are not considered simple value types.
  6.          * @param type the type to check
  7.          * @return whether the given type represents a "simple" value type
  8.          * @see #isSimpleProperty(Class)
  9.          */
  10.         public static boolean isSimpleValueType(Class<?> type) {
  11.                 return (Void.class != type && void.class != type &&
  12.                                 (ClassUtils.isPrimitiveOrWrapper(type) ||
  13.                                 Enum.class.isAssignableFrom(type) ||
  14.                                 CharSequence.class.isAssignableFrom(type) ||
  15.                                 Number.class.isAssignableFrom(type) ||
  16.                                 Date.class.isAssignableFrom(type) ||
  17.                                 Temporal.class.isAssignableFrom(type) ||
  18.                                 URI.class == type ||
  19.                                 URL.class == type ||
  20.                                 Locale.class == type ||
  21.                                 Class.class == type));
  22.         }
复制代码
通过源码分析得知,简单类型包括:

  • 基本数据类型
  • 基本数据类型对应的包装类
  • String或其他的CharSequence子类
  • Number子类
  • Date子类
  • Enum子类
  • URI
  • URL
  • Temporal子类
  • Locale
  • Class
  • 另外还包括以上简单值类型对应的数组类型。
测试验证:
  1. package com.rainbowsea.bean;
  2. /**
  3. * 枚举类
  4. */
  5. public enum Season {
  6.     SPRING,SUMMER,AUTUMN,WINTER
  7. }
复制代码
  1. package com.rainbowsea.bean;
  2. public class SimpleValueType {
  3.     // 下面的都是简单的类型
  4.     private int age;
  5.     private Integer age2;
  6.     private boolean flag;
  7.     private Boolean flag2;
  8.     private char c;
  9.     private Character c2;
  10.     private Season season;  // 枚举
  11.     private String username;
  12.     private Class clazz;
  13.     // set 注入必须要:设置 set() 方法
  14.     public void setAge(int age) {
  15. <?xml version="1.0" encoding="UTF-8"?>
  16. <beans xmlns="http://www.springframework.org/schema/beans"
  17.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  18.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  19.     <bean id="peopleBean" >
  20.     </bean>
  21. </beans>this.age = age;
  22.     }
  23.     public void setAge2(Integer age2) {
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans>this.age2 = age2;
  31.     }
  32.     public void setFlag(boolean flag) {
  33. <?xml version="1.0" encoding="UTF-8"?>
  34. <beans xmlns="http://www.springframework.org/schema/beans"
  35.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  36.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  37.     <bean id="peopleBean" >
  38.     </bean>
  39. </beans>this.flag = flag;
  40.     }
  41.     public void setFlag2(Boolean flag2) {
  42. <?xml version="1.0" encoding="UTF-8"?>
  43. <beans xmlns="http://www.springframework.org/schema/beans"
  44.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  45.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  46.     <bean id="peopleBean" >
  47.     </bean>
  48. </beans>this.flag2 = flag2;
  49.     }
  50.     public void setC(char c) {
  51. <?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.     <bean id="peopleBean" >
  56.     </bean>
  57. </beans>this.c = c;
  58.     }
  59.     public void setC2(Character c2) {
  60. <?xml version="1.0" encoding="UTF-8"?>
  61. <beans xmlns="http://www.springframework.org/schema/beans"
  62.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  63.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  64.     <bean id="peopleBean" >
  65.     </bean>
  66. </beans>this.c2 = c2;
  67.     }
  68.     public void setSeason(Season season) {
  69. <?xml version="1.0" encoding="UTF-8"?>
  70. <beans xmlns="http://www.springframework.org/schema/beans"
  71.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  72.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  73.     <bean id="peopleBean" >
  74.     </bean>
  75. </beans>this.season = season;
  76.     }
  77.     public void setUsername(String username) {
  78. <?xml version="1.0" encoding="UTF-8"?>
  79. <beans xmlns="http://www.springframework.org/schema/beans"
  80.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  81.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  82.     <bean id="peopleBean" >
  83.     </bean>
  84. </beans>this.username = username;
  85.     }
  86.     public void setClazz(Class clazz) {
  87. <?xml version="1.0" encoding="UTF-8"?>
  88. <beans xmlns="http://www.springframework.org/schema/beans"
  89.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  90.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  91.     <bean id="peopleBean" >
  92.     </bean>
  93. </beans>this.clazz = clazz;
  94.     }
  95.     @Override
  96.     public String toString() {
  97. <?xml version="1.0" encoding="UTF-8"?>
  98. <beans xmlns="http://www.springframework.org/schema/beans"
  99.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  100.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  101.     <bean id="peopleBean" >
  102.     </bean>
  103. </beans>return "SimpleValueType{" +
  104. <?xml version="1.0" encoding="UTF-8"?>
  105. <beans xmlns="http://www.springframework.org/schema/beans"
  106.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  107.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  108.     <bean id="testDateBean" >
  109. <?xml version="1.0" encoding="UTF-8"?>
  110. <beans xmlns="http://www.springframework.org/schema/beans"
  111.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  112.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  113.     <bean id="peopleBean" >
  114.     </bean>
  115. </beans><property name="date" value="2023 -05-6"></property>
  116.     </bean>
  117. </beans>"age=" + age +
  118. <?xml version="1.0" encoding="UTF-8"?>
  119. <beans xmlns="http://www.springframework.org/schema/beans"
  120.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  121.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  122.     <bean id="testDateBean" >
  123. <?xml version="1.0" encoding="UTF-8"?>
  124. <beans xmlns="http://www.springframework.org/schema/beans"
  125.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  126.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  127.     <bean id="peopleBean" >
  128.     </bean>
  129. </beans><property name="date" value="2023 -05-6"></property>
  130.     </bean>
  131. </beans>", age2=" + age2 +
  132. <?xml version="1.0" encoding="UTF-8"?>
  133. <beans xmlns="http://www.springframework.org/schema/beans"
  134.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  135.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  136.     <bean id="testDateBean" >
  137. <?xml version="1.0" encoding="UTF-8"?>
  138. <beans xmlns="http://www.springframework.org/schema/beans"
  139.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  140.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  141.     <bean id="peopleBean" >
  142.     </bean>
  143. </beans><property name="date" value="2023 -05-6"></property>
  144.     </bean>
  145. </beans>", flag=" + flag +
  146. <?xml version="1.0" encoding="UTF-8"?>
  147. <beans xmlns="http://www.springframework.org/schema/beans"
  148.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  149.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  150.     <bean id="testDateBean" >
  151. <?xml version="1.0" encoding="UTF-8"?>
  152. <beans xmlns="http://www.springframework.org/schema/beans"
  153.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  154.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  155.     <bean id="peopleBean" >
  156.     </bean>
  157. </beans><property name="date" value="2023 -05-6"></property>
  158.     </bean>
  159. </beans>", flag2=" + flag2 +
  160. <?xml version="1.0" encoding="UTF-8"?>
  161. <beans xmlns="http://www.springframework.org/schema/beans"
  162.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  163.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  164.     <bean id="testDateBean" >
  165. <?xml version="1.0" encoding="UTF-8"?>
  166. <beans xmlns="http://www.springframework.org/schema/beans"
  167.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  168.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  169.     <bean id="peopleBean" >
  170.     </bean>
  171. </beans><property name="date" value="2023 -05-6"></property>
  172.     </bean>
  173. </beans>", c=" + c +
  174. <?xml version="1.0" encoding="UTF-8"?>
  175. <beans xmlns="http://www.springframework.org/schema/beans"
  176.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  177.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  178.     <bean id="testDateBean" >
  179. <?xml version="1.0" encoding="UTF-8"?>
  180. <beans xmlns="http://www.springframework.org/schema/beans"
  181.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  182.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  183.     <bean id="peopleBean" >
  184.     </bean>
  185. </beans><property name="date" value="2023 -05-6"></property>
  186.     </bean>
  187. </beans>", c2=" + c2 +
  188. <?xml version="1.0" encoding="UTF-8"?>
  189. <beans xmlns="http://www.springframework.org/schema/beans"
  190.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  191.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  192.     <bean id="testDateBean" >
  193. <?xml version="1.0" encoding="UTF-8"?>
  194. <beans xmlns="http://www.springframework.org/schema/beans"
  195.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  196.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  197.     <bean id="peopleBean" >
  198.     </bean>
  199. </beans><property name="date" value="2023 -05-6"></property>
  200.     </bean>
  201. </beans>", season=" + season +
  202. <?xml version="1.0" encoding="UTF-8"?>
  203. <beans xmlns="http://www.springframework.org/schema/beans"
  204.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  205.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  206.     <bean id="testDateBean" >
  207. <?xml version="1.0" encoding="UTF-8"?>
  208. <beans xmlns="http://www.springframework.org/schema/beans"
  209.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  210.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  211.     <bean id="peopleBean" >
  212.     </bean>
  213. </beans><property name="date" value="2023 -05-6"></property>
  214.     </bean>
  215. </beans>", username='" + username + '\'' +
  216. <?xml version="1.0" encoding="UTF-8"?>
  217. <beans xmlns="http://www.springframework.org/schema/beans"
  218.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  219.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  220.     <bean id="testDateBean" >
  221. <?xml version="1.0" encoding="UTF-8"?>
  222. <beans xmlns="http://www.springframework.org/schema/beans"
  223.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  224.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  225.     <bean id="peopleBean" >
  226.     </bean>
  227. </beans><property name="date" value="2023 -05-6"></property>
  228.     </bean>
  229. </beans>", clazz=" + clazz +
  230. <?xml version="1.0" encoding="UTF-8"?>
  231. <beans xmlns="http://www.springframework.org/schema/beans"
  232.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  233.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  234.     <bean id="testDateBean" >
  235. <?xml version="1.0" encoding="UTF-8"?>
  236. <beans xmlns="http://www.springframework.org/schema/beans"
  237.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  238.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  239.     <bean id="peopleBean" >
  240.     </bean>
  241. </beans><property name="date" value="2023 -05-6"></property>
  242.     </bean>
  243. </beans>'}';
  244.     }
  245. }
复制代码
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean>    <bean id="" >
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans><constructor-arg name="" value=""></constructor-arg>
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans><constructor-arg name="" ref=""></constructor-arg>
  31.     </bean>    <bean id="" >
  32. <?xml version="1.0" encoding="UTF-8"?>
  33. <beans xmlns="http://www.springframework.org/schema/beans"
  34.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  35.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  36.     <bean id="peopleBean" >
  37.     </bean>
  38. </beans><constructor-arg name="" value=""></constructor-arg>
  39. <?xml version="1.0" encoding="UTF-8"?>
  40. <beans xmlns="http://www.springframework.org/schema/beans"
  41.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  42.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  43.     <bean id="peopleBean" >
  44.     </bean>
  45. </beans><constructor-arg name="" ref=""></constructor-arg>
  46.     </bean>    <bean id="" >
  47. <?xml version="1.0" encoding="UTF-8"?>
  48. <beans xmlns="http://www.springframework.org/schema/beans"
  49.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  50.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  51.     <bean id="peopleBean" >
  52.     </bean>
  53. </beans><constructor-arg name="" value=""></constructor-arg>
  54. <?xml version="1.0" encoding="UTF-8"?>
  55. <beans xmlns="http://www.springframework.org/schema/beans"
  56.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  57.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  58.     <bean id="peopleBean" >
  59.     </bean>
  60. </beans><constructor-arg name="" ref=""></constructor-arg>
  61.     </bean>   
复制代码
运行测试:
  1. package com.rainbowsea.test;
  2. import com.rainbowsea.bean.SimpleValueType;
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. public class SpringStructure {
  7.     @Test
  8.     public void test() {
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans>// 获取到对应的 spring6当中的xml的,容器对象
  16. <?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.     <bean id="peopleBean" >
  21.     </bean>
  22. </beans>// 面向接口编程,左边为接口,
  23. <?xml version="1.0" encoding="UTF-8"?>
  24. <beans xmlns="http://www.springframework.org/schema/beans"
  25.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  26.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  27.     <bean id="peopleBean" >
  28.     </bean>
  29. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sets.xml");
  30. <?xml version="1.0" encoding="UTF-8"?>
  31. <beans xmlns="http://www.springframework.org/schema/beans"
  32.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  33.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  34.     <bean id="peopleBean" >
  35.     </bean>
  36. </beans>// 通过 id 获取到对应的类/class
  37. <?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  41.     <bean id="peopleBean" >
  42.     </bean>
  43. </beans>SimpleValueType svt = applicationContext.getBean("svt", SimpleValueType.class);
  44. <?xml version="1.0" encoding="UTF-8"?>
  45. <beans xmlns="http://www.springframework.org/schema/beans"
  46.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  47.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  48.     <bean id="peopleBean" >
  49.     </bean>
  50. </beans>System.out.println(svt);
  51.     }
  52. }
复制代码

注意了: 特殊的日期时间进行一个特殊的赋值:
从上面的  BeanUtils 我们可以知道的的是  Date ,它是被Spring定义为了一个简单类型,来进行处理的。
但是,我们进行一个如下的测试:
  1. package com.rainbowsea.bean;
  2. import java.util.Date;
  3. public class TestDate {
  4.     private Date date;
  5.     public void setDate(Date date) {
  6. <?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.     <bean id="peopleBean" >
  11.     </bean>
  12. </beans>this.date = date;
  13.     }
  14.     @Override
  15.     public String toString() {
  16. <?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.     <bean id="peopleBean" >
  21.     </bean>
  22. </beans>return "TestDate{" +
  23. <?xml version="1.0" encoding="UTF-8"?>
  24. <beans xmlns="http://www.springframework.org/schema/beans"
  25.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  26.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  27.     <bean id="testDateBean" >
  28. <?xml version="1.0" encoding="UTF-8"?>
  29. <beans xmlns="http://www.springframework.org/schema/beans"
  30.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  31.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  32.     <bean id="peopleBean" >
  33.     </bean>
  34. </beans><property name="date" value="2023 -05-6"></property>
  35.     </bean>
  36. </beans>"date=" + date +
  37. <?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  41.     <bean id="testDateBean" >
  42. <?xml version="1.0" encoding="UTF-8"?>
  43. <beans xmlns="http://www.springframework.org/schema/beans"
  44.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  45.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  46.     <bean id="peopleBean" >
  47.     </bean>
  48. </beans><property name="date" value="2023 -05-6"></property>
  49.     </bean>
  50. </beans>'}';
  51.     }
  52. }
复制代码
  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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="testDateBean" >
  6. <?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.     <bean id="peopleBean" >
  11.     </bean>
  12. </beans><property name="date" value="2023 -05-6"></property>
  13.     </bean>
  14. </beans>
复制代码
  1. package com.rainbowsea.test;
  2. import com.rainbowsea.bean.SimpleValueType;
  3. import com.rainbowsea.bean.TestDate;
  4. import org.junit.Test;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. public class SpringStructure {
  8.     @Test
  9.     public void test() {
  10. <?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  14.     <bean id="peopleBean" >
  15.     </bean>
  16. </beans>// 获取到对应的 spring6当中的xml的,容器对象
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans>// 面向接口编程,左边为接口,
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sets.xml");
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans>// 通过 id 获取到对应的类/class
  38. <?xml version="1.0" encoding="UTF-8"?>
  39. <beans xmlns="http://www.springframework.org/schema/beans"
  40.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  42.     <bean id="peopleBean" >
  43.     </bean>
  44. </beans>TestDate testDate = applicationContext.getBean("date", TestDate.class);
  45. <?xml version="1.0" encoding="UTF-8"?>
  46. <beans xmlns="http://www.springframework.org/schema/beans"
  47.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  48.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  49.     <bean id="peopleBean" >
  50.     </bean>
  51. </beans>System.out.println(testDate);
  52.     }
  53. }
复制代码

报错的原因是:
从报错的信息上,我们可以看到:
[
说:'java.lang.String' to required type 'java.util.Date'  说 这个  2023 -05-6 这个字符串,无法转换成 java.util.Date 类型。  如果你硬要把Date 当作简单类型的话,使用 value 赋值的话,这个日期字符串格式有要求的。所有的要求就是: new Date toString 打印显示的格式形式: Fri Jun 30 11:27:41 CST 2023 。
但是,我们在实际开发中,我们一般不会把 Date 当做简单类型,虽然是简单类型,但是我们一般采用的是ref 的Date 类型的属性赋值。

  • 如果把Date当做简单类型的话,日期字符串格式不能随便写。格式必须符合Date的toString()方法格式。显然这就比较鸡肋了。如果我们提供一个这样的日期字符串:2010-10-11,在这里是无法赋值给Date类型的属性的。
  • spring6之后,当注入的是URL,那么这个url字符串是会进行有效性检测的。如果是一个存在的url,那就没问题。如果不存在则报错。
测试使用: 我们要求的格式:  Fri Jun 30 11:27:41 CST 2023  进行一个 Date 的测试
  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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="testDateBean" >
  6. <?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.     <bean id="peopleBean" >
  11.     </bean>
  12. </beans><property name="date" value="2023 -05-6"></property>
  13.     </bean>
  14. </beans>
复制代码
测试:
  1. package com.rainbowsea.test;
  2. import com.rainbowsea.bean.SimpleValueType;
  3. import com.rainbowsea.bean.TestDate;
  4. import org.junit.Test;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. public class SpringStructure {
  8.     @Test
  9.     public void test() {
  10. <?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  14.     <bean id="peopleBean" >
  15.     </bean>
  16. </beans>// 获取到对应的 spring6当中的xml的,容器对象
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans>// 面向接口编程,左边为接口,
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sets.xml");
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans>// 通过 id 获取到对应的类/class
  38. <?xml version="1.0" encoding="UTF-8"?>
  39. <beans xmlns="http://www.springframework.org/schema/beans"
  40.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  42.     <bean id="peopleBean" >
  43.     </bean>
  44. </beans>TestDate testDate = applicationContext.getBean("testDateBean", TestDate.class);
  45. <?xml version="1.0" encoding="UTF-8"?>
  46. <beans xmlns="http://www.springframework.org/schema/beans"
  47.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  48.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  49.     <bean id="peopleBean" >
  50.     </bean>
  51. </beans>System.out.println(testDate);
  52.     }
  53. }
复制代码

测试将  Date 当作为复杂类型进行一个set  注入的方式:测试
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean>   
复制代码
  1. package com.rainbowsea.test;
  2. import com.rainbowsea.bean.SimpleValueType;
  3. import com.rainbowsea.bean.TestDate;
  4. import org.junit.Test;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. public class SpringStructure {
  8.     @Test
  9.     public void test() {
  10. <?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  14.     <bean id="peopleBean" >
  15.     </bean>
  16. </beans>// 获取到对应的 spring6当中的xml的,容器对象
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans>// 面向接口编程,左边为接口,
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sets.xml");
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans>// 通过 id 获取到对应的类/class
  38. <?xml version="1.0" encoding="UTF-8"?>
  39. <beans xmlns="http://www.springframework.org/schema/beans"
  40.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  42.     <bean id="peopleBean" >
  43.     </bean>
  44. </beans>TestDate testDate = applicationContext.getBean("testDateBean", TestDate.class);
  45. <?xml version="1.0" encoding="UTF-8"?>
  46. <beans xmlns="http://www.springframework.org/schema/beans"
  47.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  48.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  49.     <bean id="peopleBean" >
  50.     </bean>
  51. </beans>System.out.println(testDate);
  52.     }
  53. }
复制代码

经典案例:给数据源的属性注入值:
假设我们现在要自己手写一个数据源,我们都知道所有的数据源都要实现javax.sql.DataSource接口,并且数据源中应该有连接数据库的信息,例如:driver、url、username、password等。
  1. package com.rainbowsea.bean;import javax.sql.DataSource;import java.io.PrintWriter;import java.sql.Connection;import java.sql.SQLException;import java.sql.SQLFeatureNotSupportedException;import java.util.logging.Logger;public class MyDataSource implements DataSource {    private String driver;    private String url;    private String username;    private String password;    // set 注入必须提供 set () 方法    public void setDriver(String driver) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.driver = driver;    }    public void setUrl(String url) {<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>this.url = url;    }    public void setUsername(String username) {<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="peopleBean" >
  18.     </bean>
  19. </beans>this.username = username;    }    public void setPassword(String password) {<?xml version="1.0" encoding="UTF-8"?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  23.     <bean id="peopleBean" >
  24.     </bean>
  25. </beans>this.password = password;    }    @Override    public Connection getConnection() throws SQLException {<?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  29.     <bean id="peopleBean" >
  30.     </bean>
  31. </beans>return null;    }    @Override    public Connection getConnection(String username, String password) throws SQLException {<?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans>return null;    }    @Override    public PrintWriter getLogWriter() throws SQLException {<?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  41.     <bean id="peopleBean" >
  42.     </bean>
  43. </beans>return null;    }    @Override    public void setLogWriter(PrintWriter out) throws SQLException {    }    @Override    public void setLoginTimeout(int seconds) throws SQLException {    }    @Override    public int getLoginTimeout() throws SQLException {<?xml version="1.0" encoding="UTF-8"?>
  44. <beans xmlns="http://www.springframework.org/schema/beans"
  45.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  46.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  47.     <bean id="peopleBean" >
  48.     </bean>
  49. </beans>return 0;    }    @Override    public Logger getParentLogger() throws SQLFeatureNotSupportedException {<?xml version="1.0" encoding="UTF-8"?>
  50. <beans xmlns="http://www.springframework.org/schema/beans"
  51.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  52.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  53.     <bean id="peopleBean" >
  54.     </bean>
  55. </beans>return null;    }    @Override    public  T unwrap(Class iface) throws SQLException {<?xml version="1.0" encoding="UTF-8"?>
  56. <beans xmlns="http://www.springframework.org/schema/beans"
  57.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  58.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  59.     <bean id="peopleBean" >
  60.     </bean>
  61. </beans>return null;    }    @Override    public boolean isWrapperFor(Class iface) throws SQLException {<?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  65.     <bean id="peopleBean" >
  66.     </bean>
  67. </beans>return false;    }    @Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  68. <beans xmlns="http://www.springframework.org/schema/beans"
  69.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  70.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  71.     <bean id="peopleBean" >
  72.     </bean>
  73. </beans>return "MyDataSource{" +<?xml version="1.0" encoding="UTF-8"?>
  74. <beans xmlns="http://www.springframework.org/schema/beans"
  75.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  76.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  77.     <bean id="testDateBean" >
  78. <?xml version="1.0" encoding="UTF-8"?>
  79. <beans xmlns="http://www.springframework.org/schema/beans"
  80.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  81.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  82.     <bean id="peopleBean" >
  83.     </bean>
  84. </beans><property name="date" value="2023 -05-6"></property>
  85.     </bean>
  86. </beans>"driver='" + driver + '\'' +<?xml version="1.0" encoding="UTF-8"?>
  87. <beans xmlns="http://www.springframework.org/schema/beans"
  88.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  89.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  90.     <bean id="testDateBean" >
  91. <?xml version="1.0" encoding="UTF-8"?>
  92. <beans xmlns="http://www.springframework.org/schema/beans"
  93.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  94.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  95.     <bean id="peopleBean" >
  96.     </bean>
  97. </beans><property name="date" value="2023 -05-6"></property>
  98.     </bean>
  99. </beans>", url='" + url + '\'' +<?xml version="1.0" encoding="UTF-8"?>
  100. <beans xmlns="http://www.springframework.org/schema/beans"
  101.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  102.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  103.     <bean id="testDateBean" >
  104. <?xml version="1.0" encoding="UTF-8"?>
  105. <beans xmlns="http://www.springframework.org/schema/beans"
  106.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  107.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  108.     <bean id="peopleBean" >
  109.     </bean>
  110. </beans><property name="date" value="2023 -05-6"></property>
  111.     </bean>
  112. </beans>", username='" + username + '\'' +<?xml version="1.0" encoding="UTF-8"?>
  113. <beans xmlns="http://www.springframework.org/schema/beans"
  114.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  115.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  116.     <bean id="testDateBean" >
  117. <?xml version="1.0" encoding="UTF-8"?>
  118. <beans xmlns="http://www.springframework.org/schema/beans"
  119.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  120.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  121.     <bean id="peopleBean" >
  122.     </bean>
  123. </beans><property name="date" value="2023 -05-6"></property>
  124.     </bean>
  125. </beans>", password='" + password + '\'' +<?xml version="1.0" encoding="UTF-8"?>
  126. <beans xmlns="http://www.springframework.org/schema/beans"
  127.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  128.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  129.     <bean id="testDateBean" >
  130. <?xml version="1.0" encoding="UTF-8"?>
  131. <beans xmlns="http://www.springframework.org/schema/beans"
  132.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  133.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  134.     <bean id="peopleBean" >
  135.     </bean>
  136. </beans><property name="date" value="2023 -05-6"></property>
  137.     </bean>
  138. </beans>'}';    }}
复制代码
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.     <bean id="testDateBean" >
  21. <?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.     <bean id="peopleBean" >
  26.     </bean>
  27. </beans><property name="date" value="2023 -05-6"></property>
  28.     </bean>
  29. </beans>
复制代码
运行测试:
  1. package com.rainbowsea.test;
  2. import com.rainbowsea.bean.MyDataSource;
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. public class SpringStructure {
  7.     @Test
  8.     public void test() {
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans>// 获取到对应的 spring6当中的xml的,容器对象
  16. <?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.     <bean id="peopleBean" >
  21.     </bean>
  22. </beans>// 面向接口编程,左边为接口,
  23. <?xml version="1.0" encoding="UTF-8"?>
  24. <beans xmlns="http://www.springframework.org/schema/beans"
  25.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  26.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  27.     <bean id="peopleBean" >
  28.     </bean>
  29. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sets.xml");
  30. <?xml version="1.0" encoding="UTF-8"?>
  31. <beans xmlns="http://www.springframework.org/schema/beans"
  32.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  33.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  34.     <bean id="peopleBean" >
  35.     </bean>
  36. </beans>// 通过 id 获取到对应的类/class
  37. <?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  41.     <bean id="peopleBean" >
  42.     </bean>
  43. </beans>MyDataSource data = applicationContext.getBean("data", MyDataSource.class);
  44. <?xml version="1.0" encoding="UTF-8"?>
  45. <beans xmlns="http://www.springframework.org/schema/beans"
  46.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  47.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  48.     <bean id="peopleBean" >
  49.     </bean>
  50. </beans>System.out.println(data);
  51.     }
  52. }
复制代码

2.3.2 set 复杂类型注入的方式

格式:
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.     <bean id="peopleBean" >
  21.     </bean>
  22. </beans>
复制代码
  1. package com.rainbowsea.bean;public class User {    private String name;    public void setName(String name) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.name = name;    }    @Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>return "User{" +<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="testDateBean" >
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <beans xmlns="http://www.springframework.org/schema/beans"
  20.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  22.     <bean id="peopleBean" >
  23.     </bean>
  24. </beans><property name="date" value="2023 -05-6"></property>
  25.     </bean>
  26. </beans>"name='" + name + '\'' +<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.     <bean id="testDateBean" >
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans><property name="date" value="2023 -05-6"></property>
  38.     </bean>
  39. </beans>'}';    }}
复制代码
  1. package com.rainbowsea.bean;public class People {    private User user;    private String name;    public void setUser(User user) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.user = user;    }    @Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>return "People{" +<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="testDateBean" >
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <beans xmlns="http://www.springframework.org/schema/beans"
  20.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  22.     <bean id="peopleBean" >
  23.     </bean>
  24. </beans><property name="date" value="2023 -05-6"></property>
  25.     </bean>
  26. </beans>"user=" + user +<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.     <bean id="testDateBean" >
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans><property name="date" value="2023 -05-6"></property>
  38.     </bean>
  39. </beans>'}';    }}
复制代码
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.     <bean id="peopleBean" >
  21.     </bean>
  22. </beans>
复制代码
运行测试:
  1. package com.rainbowsea.test;
  2. import com.rainbowsea.bean.MyDataSource;
  3. import com.rainbowsea.bean.People;
  4. import org.junit.Test;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. public class SpringStructure {
  8.     @Test
  9.     public void test() {
  10. <?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  14.     <bean id="peopleBean" >
  15.     </bean>
  16. </beans>// 获取到对应的 spring6当中的xml的,容器对象
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans>// 面向接口编程,左边为接口,
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sets.xml");
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans>// 通过 id 获取到对应的类/class
  38. <?xml version="1.0" encoding="UTF-8"?>
  39. <beans xmlns="http://www.springframework.org/schema/beans"
  40.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  42.     <bean id="peopleBean" >
  43.     </bean>
  44. </beans>People peopleBean = applicationContext.getBean("peopleBean", People.class);
  45. <?xml version="1.0" encoding="UTF-8"?>
  46. <beans xmlns="http://www.springframework.org/schema/beans"
  47.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  48.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  49.     <bean id="peopleBean" >
  50.     </bean>
  51. </beans>System.out.println(peopleBean);
  52.     }
  53. }
复制代码

2.4 级联属性赋值

级联的要求:

  • 对应级联的类下的属性的赋值,必须提供 get() 方法,因为级联的底层调用的就是 get() 方法。不然无法级联到。
  • 注意:级联的上下放置的顺序,级联当中的使用的 id ,必须在级联之前先定义处理出来,不然同样无法级联到。
  1. package com.rainbowsea.bean;public class User {    private String name;    public void setName(String name) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.name = name;    }<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>@Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="peopleBean" >
  18.     </bean>
  19. </beans>return "User{" +<?xml version="1.0" encoding="UTF-8"?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  23.     <bean id="testDateBean" >
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans><property name="date" value="2023 -05-6"></property>
  31.     </bean>
  32. </beans>"name='" + name + '\'' +<?xml version="1.0" encoding="UTF-8"?>
  33. <beans xmlns="http://www.springframework.org/schema/beans"
  34.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  35.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  36.     <bean id="testDateBean" >
  37. <?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  41.     <bean id="peopleBean" >
  42.     </bean>
  43. </beans><property name="date" value="2023 -05-6"></property>
  44.     </bean>
  45. </beans>'}';    }}
复制代码
  1. package com.rainbowsea.bean;public class People {    private User user;    private String name;    public void setUser(User user) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.user = user;    }    public void setName(String name) {<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>this.name = name;    }    // 使用级联的话,必须提供其中的 get() 方法进行一个获取    // 级联的底层调用的就是 get()方法。    public User getUser() {<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="peopleBean" >
  18.     </bean>
  19. </beans>return user;    }    @Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  23.     <bean id="peopleBean" >
  24.     </bean>
  25. </beans>return "People{" +<?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  29.     <bean id="testDateBean" >
  30. <?xml version="1.0" encoding="UTF-8"?>
  31. <beans xmlns="http://www.springframework.org/schema/beans"
  32.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  33.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  34.     <bean id="peopleBean" >
  35.     </bean>
  36. </beans><property name="date" value="2023 -05-6"></property>
  37.     </bean>
  38. </beans>"user=" + user +<?xml version="1.0" encoding="UTF-8"?>
  39. <beans xmlns="http://www.springframework.org/schema/beans"
  40.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  42.     <bean id="testDateBean" >
  43. <?xml version="1.0" encoding="UTF-8"?>
  44. <beans xmlns="http://www.springframework.org/schema/beans"
  45.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  46.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  47.     <bean id="peopleBean" >
  48.     </bean>
  49. </beans><property name="date" value="2023 -05-6"></property>
  50.     </bean>
  51. </beans>", name='" + name + '\'' +<?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.     <bean id="testDateBean" >
  56. <?xml version="1.0" encoding="UTF-8"?>
  57. <beans xmlns="http://www.springframework.org/schema/beans"
  58.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  60.     <bean id="peopleBean" >
  61.     </bean>
  62. </beans><property name="date" value="2023 -05-6"></property>
  63.     </bean>
  64. </beans>'}';    }}
复制代码
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20. <?xml version="1.0" encoding="UTF-8"?>
  21. <beans xmlns="http://www.springframework.org/schema/beans"
  22.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  23.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  24.     <bean id="peopleBean" >
  25.     </bean>
  26. </beans><constructor-arg index="0" value=""></constructor-arg>
  27. <?xml version="1.0" encoding="UTF-8"?>
  28. <beans xmlns="http://www.springframework.org/schema/beans"
  29.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  30.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  31.     <bean id="peopleBean" >
  32.     </bean>
  33. </beans><constructor-arg index="1" ref=""></constructor-arg>
  34.     </bean>
  35. </beans>
复制代码
运行测试:
  1. package com.rainbowsea.test;
  2. import com.rainbowsea.bean.MyDataSource;
  3. import com.rainbowsea.bean.People;
  4. import org.junit.Test;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. public class SpringStructure {
  8.     @Test
  9.     public void test() {
  10. <?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  14.     <bean id="peopleBean" >
  15.     </bean>
  16. </beans>// 获取到对应的 spring6当中的xml的,容器对象
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans>// 面向接口编程,左边为接口,
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sets.xml");
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans>// 通过 id 获取到对应的类/class
  38. <?xml version="1.0" encoding="UTF-8"?>
  39. <beans xmlns="http://www.springframework.org/schema/beans"
  40.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  42.     <bean id="peopleBean" >
  43.     </bean>
  44. </beans>People peopleBean = applicationContext.getBean("peopleBean", People.class);
  45. <?xml version="1.0" encoding="UTF-8"?>
  46. <beans xmlns="http://www.springframework.org/schema/beans"
  47.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  48.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  49.     <bean id="peopleBean" >
  50.     </bean>
  51. </beans>System.out.println(peopleBean);
  52.     }
  53. }
复制代码

级联用的比较少,所以大家了解一下就好了。
2.5 set 注入数组类型

关于set 数组类型的注入的方式:
格式:
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean>    <bean id="" >
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans><constructor-arg name="" value=""></constructor-arg>
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans><constructor-arg name="" ref=""></constructor-arg>
  31.     </bean>    <bean id="" >
  32. <?xml version="1.0" encoding="UTF-8"?>
  33. <beans xmlns="http://www.springframework.org/schema/beans"
  34.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  35.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  36.     <bean id="peopleBean" >
  37.     </bean>
  38. </beans><constructor-arg name="" value=""></constructor-arg>
  39. <?xml version="1.0" encoding="UTF-8"?>
  40. <beans xmlns="http://www.springframework.org/schema/beans"
  41.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  42.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  43.     <bean id="peopleBean" >
  44.     </bean>
  45. </beans><constructor-arg name="" ref=""></constructor-arg>
  46.     </bean>    <bean id="" >
  47. <?xml version="1.0" encoding="UTF-8"?>
  48. <beans xmlns="http://www.springframework.org/schema/beans"
  49.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  50.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  51.     <bean id="peopleBean" >
  52.     </bean>
  53. </beans><constructor-arg name="" value=""></constructor-arg>
  54. <?xml version="1.0" encoding="UTF-8"?>
  55. <beans xmlns="http://www.springframework.org/schema/beans"
  56.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  57.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  58.     <bean id="peopleBean" >
  59.     </bean>
  60. </beans><constructor-arg name="" ref=""></constructor-arg>
  61.     </bean>   
复制代码
2.5.1 当数组中的元素是简单类型
  1. package com.rainbowsea.bean;import java.util.Arrays;public class People {    private String[] name;    public void setName(String[] name) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.name = name;    }    @Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>return "People{" +<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="testDateBean" >
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <beans xmlns="http://www.springframework.org/schema/beans"
  20.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  22.     <bean id="peopleBean" >
  23.     </bean>
  24. </beans><property name="date" value="2023 -05-6"></property>
  25.     </bean>
  26. </beans>"name=" + Arrays.toString(name) +<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.     <bean id="testDateBean" >
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans><property name="date" value="2023 -05-6"></property>
  38.     </bean>
  39. </beans>'}';    }}
复制代码
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20. <?xml version="1.0" encoding="UTF-8"?>
  21. <beans xmlns="http://www.springframework.org/schema/beans"
  22.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  23.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  24.     <bean id="peopleBean" >
  25.     </bean>
  26. </beans><constructor-arg index="0" value=""></constructor-arg>
  27. <?xml version="1.0" encoding="UTF-8"?>
  28. <beans xmlns="http://www.springframework.org/schema/beans"
  29.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  30.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  31.     <bean id="peopleBean" >
  32.     </bean>
  33. </beans><constructor-arg index="1" ref=""></constructor-arg>
  34.     </bean>
  35. </beans>张三<?xml version="1.0" encoding="UTF-8"?>
  36. <beans xmlns="http://www.springframework.org/schema/beans"
  37.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  38.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  39.     <bean id="testDateBean" >
  40. <?xml version="1.0" encoding="UTF-8"?>
  41. <beans xmlns="http://www.springframework.org/schema/beans"
  42.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  43.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  44.     <bean id="peopleBean" >
  45.     </bean>
  46. </beans><property name="date" value="2023 -05-6"></property>
  47.     </bean>
  48. </beans>李四<?xml version="1.0" encoding="UTF-8"?>
  49. <beans xmlns="http://www.springframework.org/schema/beans"
  50.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  51.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  52.     <bean id="testDateBean" >
  53. <?xml version="1.0" encoding="UTF-8"?>
  54. <beans xmlns="http://www.springframework.org/schema/beans"
  55.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  56.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  57.     <bean id="peopleBean" >
  58.     </bean>
  59. </beans><property name="date" value="2023 -05-6"></property>
  60.     </bean>
  61. </beans>王五    <bean id="" >
  62. <?xml version="1.0" encoding="UTF-8"?>
  63. <beans xmlns="http://www.springframework.org/schema/beans"
  64.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  65.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  66.     <bean id="peopleBean" >
  67.     </bean>
  68. </beans><constructor-arg name="" value=""></constructor-arg>
  69. <?xml version="1.0" encoding="UTF-8"?>
  70. <beans xmlns="http://www.springframework.org/schema/beans"
  71.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  72.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  73.     <bean id="peopleBean" >
  74.     </bean>
  75. </beans><constructor-arg name="" ref=""></constructor-arg>
  76.     </bean>
复制代码
运行测试:
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.     <bean id="testDateBean" >
  21. <?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.     <bean id="peopleBean" >
  26.     </bean>
  27. </beans><property name="date" value="2023 -05-6"></property>
  28.     </bean>
  29. </beans>张三<?xml version="1.0" encoding="UTF-8"?>
  30. <beans xmlns="http://www.springframework.org/schema/beans"
  31.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  33.     <bean id="testDateBean" >
  34. <?xml version="1.0" encoding="UTF-8"?>
  35. <beans xmlns="http://www.springframework.org/schema/beans"
  36.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  37.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  38.     <bean id="peopleBean" >
  39.     </bean>
  40. </beans><property name="date" value="2023 -05-6"></property>
  41.     </bean>
  42. </beans>李四<?xml version="1.0" encoding="UTF-8"?>
  43. <beans xmlns="http://www.springframework.org/schema/beans"
  44.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  45.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  46.     <bean id="testDateBean" >
  47. <?xml version="1.0" encoding="UTF-8"?>
  48. <beans xmlns="http://www.springframework.org/schema/beans"
  49.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  50.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  51.     <bean id="peopleBean" >
  52.     </bean>
  53. </beans><property name="date" value="2023 -05-6"></property>
  54.     </bean>
  55. </beans>王五    <bean id="" >
  56. <?xml version="1.0" encoding="UTF-8"?>
  57. <beans xmlns="http://www.springframework.org/schema/beans"
  58.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  60.     <bean id="peopleBean" >
  61.     </bean>
  62. </beans><constructor-arg name="" value=""></constructor-arg>
  63. <?xml version="1.0" encoding="UTF-8"?>
  64. <beans xmlns="http://www.springframework.org/schema/beans"
  65.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  66.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  67.     <bean id="peopleBean" >
  68.     </bean>
  69. </beans><constructor-arg name="" ref=""></constructor-arg>
  70.     </bean>
复制代码

2.5.2 当数组中的元素是复杂类型

格式:
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean>    <bean id="" >
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans><constructor-arg name="" value=""></constructor-arg>
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans><constructor-arg name="" ref=""></constructor-arg>
  31.     </bean>    <bean id="" >
  32. <?xml version="1.0" encoding="UTF-8"?>
  33. <beans xmlns="http://www.springframework.org/schema/beans"
  34.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  35.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  36.     <bean id="peopleBean" >
  37.     </bean>
  38. </beans><constructor-arg name="" value=""></constructor-arg>
  39. <?xml version="1.0" encoding="UTF-8"?>
  40. <beans xmlns="http://www.springframework.org/schema/beans"
  41.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  42.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  43.     <bean id="peopleBean" >
  44.     </bean>
  45. </beans><constructor-arg name="" ref=""></constructor-arg>
  46.     </bean>    <bean id="" >
  47. <?xml version="1.0" encoding="UTF-8"?>
  48. <beans xmlns="http://www.springframework.org/schema/beans"
  49.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  50.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  51.     <bean id="peopleBean" >
  52.     </bean>
  53. </beans><constructor-arg name="" value=""></constructor-arg>
  54. <?xml version="1.0" encoding="UTF-8"?>
  55. <beans xmlns="http://www.springframework.org/schema/beans"
  56.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  57.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  58.     <bean id="peopleBean" >
  59.     </bean>
  60. </beans><constructor-arg name="" ref=""></constructor-arg>
  61.     </bean><?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  65.     <bean id="testDateBean" >
  66. <?xml version="1.0" encoding="UTF-8"?>
  67. <beans xmlns="http://www.springframework.org/schema/beans"
  68.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  69.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  70.     <bean id="peopleBean" >
  71.     </bean>
  72. </beans><property name="date" value="2023 -05-6"></property>
  73.     </bean>
  74. </beans>
复制代码
**举例: **
  1. package com.rainbowsea.bean;public class User {    private String name;    public void setName(String name) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.name = name;    }    @Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>return "User{" +<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="testDateBean" >
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <beans xmlns="http://www.springframework.org/schema/beans"
  20.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  22.     <bean id="peopleBean" >
  23.     </bean>
  24. </beans><property name="date" value="2023 -05-6"></property>
  25.     </bean>
  26. </beans>"name='" + name + '\'' +<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.     <bean id="testDateBean" >
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans><property name="date" value="2023 -05-6"></property>
  38.     </bean>
  39. </beans>'}';    }}
复制代码
  1. package com.rainbowsea.bean;import java.util.Arrays;public class People {    private User[] users;    public void setUsers(User[] users) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.users = users;    }    @Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>return "People{" +<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="testDateBean" >
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <beans xmlns="http://www.springframework.org/schema/beans"
  20.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  22.     <bean id="peopleBean" >
  23.     </bean>
  24. </beans><property name="date" value="2023 -05-6"></property>
  25.     </bean>
  26. </beans>"users=" + Arrays.toString(users) +<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.     <bean id="testDateBean" >
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans><property name="date" value="2023 -05-6"></property>
  38.     </bean>
  39. </beans>'}';    }}
复制代码
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean>    <bean id="" >
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans><constructor-arg name="" value=""></constructor-arg>
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans><constructor-arg name="" ref=""></constructor-arg>
  31.     </bean>    <bean id="" >
  32. <?xml version="1.0" encoding="UTF-8"?>
  33. <beans xmlns="http://www.springframework.org/schema/beans"
  34.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  35.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  36.     <bean id="peopleBean" >
  37.     </bean>
  38. </beans><constructor-arg name="" value=""></constructor-arg>
  39. <?xml version="1.0" encoding="UTF-8"?>
  40. <beans xmlns="http://www.springframework.org/schema/beans"
  41.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  42.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  43.     <bean id="peopleBean" >
  44.     </bean>
  45. </beans><constructor-arg name="" ref=""></constructor-arg>
  46.     </bean>    <bean id="" >
  47. <?xml version="1.0" encoding="UTF-8"?>
  48. <beans xmlns="http://www.springframework.org/schema/beans"
  49.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  50.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  51.     <bean id="peopleBean" >
  52.     </bean>
  53. </beans><constructor-arg name="" value=""></constructor-arg>
  54. <?xml version="1.0" encoding="UTF-8"?>
  55. <beans xmlns="http://www.springframework.org/schema/beans"
  56.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  57.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  58.     <bean id="peopleBean" >
  59.     </bean>
  60. </beans><constructor-arg name="" ref=""></constructor-arg>
  61.     </bean><?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  65.     <bean id="testDateBean" >
  66. <?xml version="1.0" encoding="UTF-8"?>
  67. <beans xmlns="http://www.springframework.org/schema/beans"
  68.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  69.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  70.     <bean id="peopleBean" >
  71.     </bean>
  72. </beans><property name="date" value="2023 -05-6"></property>
  73.     </bean>
  74. </beans>
复制代码
运行测试:
  1. package com.rainbowsea.test;
  2. import com.rainbowsea.bean.MyDataSource;
  3. import com.rainbowsea.bean.People;
  4. import org.junit.Test;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. public class SpringStructure {
  8.     @Test
  9.     public void test() {
  10. <?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  14.     <bean id="peopleBean" >
  15.     </bean>
  16. </beans>// 获取到对应的 spring6当中的xml的,容器对象
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans>// 面向接口编程,左边为接口,
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sets.xml");
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans>// 通过 id 获取到对应的类/class
  38. <?xml version="1.0" encoding="UTF-8"?>
  39. <beans xmlns="http://www.springframework.org/schema/beans"
  40.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  42.     <bean id="peopleBean" >
  43.     </bean>
  44. </beans>People peopleBean = applicationContext.getBean("peopleBean", People.class);
  45. <?xml version="1.0" encoding="UTF-8"?>
  46. <beans xmlns="http://www.springframework.org/schema/beans"
  47.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  48.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  49.     <bean id="peopleBean" >
  50.     </bean>
  51. </beans>System.out.println(peopleBean);
  52.     }
  53. }
复制代码

2.6 set注入List集合类型

List集合:有序可重复
2.6.1 set 注入 List集合简单类型
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.     <bean id="testDateBean" >
  21. <?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.     <bean id="peopleBean" >
  26.     </bean>
  27. </beans><property name="date" value="2023 -05-6"></property>
  28.     </bean>
  29. </beans>张三<?xml version="1.0" encoding="UTF-8"?>
  30. <beans xmlns="http://www.springframework.org/schema/beans"
  31.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  33.     <bean id="testDateBean" >
  34. <?xml version="1.0" encoding="UTF-8"?>
  35. <beans xmlns="http://www.springframework.org/schema/beans"
  36.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  37.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  38.     <bean id="peopleBean" >
  39.     </bean>
  40. </beans><property name="date" value="2023 -05-6"></property>
  41.     </bean>
  42. </beans>李四    <bean id="" >
  43. <?xml version="1.0" encoding="UTF-8"?>
  44. <beans xmlns="http://www.springframework.org/schema/beans"
  45.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  46.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  47.     <bean id="peopleBean" >
  48.     </bean>
  49. </beans><constructor-arg name="" value=""></constructor-arg>
  50. <?xml version="1.0" encoding="UTF-8"?>
  51. <beans xmlns="http://www.springframework.org/schema/beans"
  52.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  53.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  54.     <bean id="peopleBean" >
  55.     </bean>
  56. </beans><constructor-arg name="" ref=""></constructor-arg>
  57.     </bean>
复制代码
  1. package com.rainbowsea.bean;import java.util.List;public class People { private List name;    public void setName(List name) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.name = name;    }    @Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>return "People{" +<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="testDateBean" >
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <beans xmlns="http://www.springframework.org/schema/beans"
  20.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  22.     <bean id="peopleBean" >
  23.     </bean>
  24. </beans><property name="date" value="2023 -05-6"></property>
  25.     </bean>
  26. </beans>"name=" + name +<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.     <bean id="testDateBean" >
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans><property name="date" value="2023 -05-6"></property>
  38.     </bean>
  39. </beans>'}';    }}
复制代码
运行测试:
  1. package com.rainbowsea.test;
  2. import com.rainbowsea.bean.MyDataSource;
  3. import com.rainbowsea.bean.People;
  4. import org.junit.Test;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. public class SpringStructure {
  8.     @Test
  9.     public void test() {
  10. <?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  14.     <bean id="peopleBean" >
  15.     </bean>
  16. </beans>// 获取到对应的 spring6当中的xml的,容器对象
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans>// 面向接口编程,左边为接口,
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sets.xml");
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans>// 通过 id 获取到对应的类/class
  38. <?xml version="1.0" encoding="UTF-8"?>
  39. <beans xmlns="http://www.springframework.org/schema/beans"
  40.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  42.     <bean id="peopleBean" >
  43.     </bean>
  44. </beans>People peopleBean = applicationContext.getBean("peopleBean", People.class);
  45. <?xml version="1.0" encoding="UTF-8"?>
  46. <beans xmlns="http://www.springframework.org/schema/beans"
  47.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  48.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  49.     <bean id="peopleBean" >
  50.     </bean>
  51. </beans>System.out.println(peopleBean);
  52.     }
  53. }
复制代码

2.6.2 set 注入List集合复杂类型
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean>    <bean id="" >
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans><constructor-arg name="" value=""></constructor-arg>
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans><constructor-arg name="" ref=""></constructor-arg>
  31.     </bean>    <bean id="" >
  32. <?xml version="1.0" encoding="UTF-8"?>
  33. <beans xmlns="http://www.springframework.org/schema/beans"
  34.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  35.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  36.     <bean id="peopleBean" >
  37.     </bean>
  38. </beans><constructor-arg name="" value=""></constructor-arg>
  39. <?xml version="1.0" encoding="UTF-8"?>
  40. <beans xmlns="http://www.springframework.org/schema/beans"
  41.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  42.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  43.     <bean id="peopleBean" >
  44.     </bean>
  45. </beans><constructor-arg name="" ref=""></constructor-arg>
  46.     </bean>    <bean id="" >
  47. <?xml version="1.0" encoding="UTF-8"?>
  48. <beans xmlns="http://www.springframework.org/schema/beans"
  49.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  50.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  51.     <bean id="peopleBean" >
  52.     </bean>
  53. </beans><constructor-arg name="" value=""></constructor-arg>
  54. <?xml version="1.0" encoding="UTF-8"?>
  55. <beans xmlns="http://www.springframework.org/schema/beans"
  56.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  57.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  58.     <bean id="peopleBean" >
  59.     </bean>
  60. </beans><constructor-arg name="" ref=""></constructor-arg>
  61.     </bean><?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  65.     <bean id="testDateBean" >
  66. <?xml version="1.0" encoding="UTF-8"?>
  67. <beans xmlns="http://www.springframework.org/schema/beans"
  68.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  69.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  70.     <bean id="peopleBean" >
  71.     </bean>
  72. </beans><property name="date" value="2023 -05-6"></property>
  73.     </bean>
  74. </beans>
复制代码
  1. package com.rainbowsea.bean;public class User {    private String name;    public void setName(String name) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.name = name;    }    @Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>return "User{" +<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="testDateBean" >
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <beans xmlns="http://www.springframework.org/schema/beans"
  20.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  22.     <bean id="peopleBean" >
  23.     </bean>
  24. </beans><property name="date" value="2023 -05-6"></property>
  25.     </bean>
  26. </beans>"name='" + name + '\'' +<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.     <bean id="testDateBean" >
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans><property name="date" value="2023 -05-6"></property>
  38.     </bean>
  39. </beans>'}';    }}
复制代码
  1. package com.rainbowsea.bean;import java.util.List;public class People { private List users;    public void setUsers(List users) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.users = users;    }    @Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>return "People{" +<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="testDateBean" >
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <beans xmlns="http://www.springframework.org/schema/beans"
  20.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  22.     <bean id="peopleBean" >
  23.     </bean>
  24. </beans><property name="date" value="2023 -05-6"></property>
  25.     </bean>
  26. </beans>"users=" + users +<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.     <bean id="testDateBean" >
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans><property name="date" value="2023 -05-6"></property>
  38.     </bean>
  39. </beans>'}';    }}
复制代码
运行测试:
  1. package com.rainbowsea.test;
  2. import com.rainbowsea.bean.MyDataSource;
  3. import com.rainbowsea.bean.People;
  4. import org.junit.Test;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. public class SpringStructure {
  8.     @Test
  9.     public void test() {
  10. <?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  14.     <bean id="peopleBean" >
  15.     </bean>
  16. </beans>// 获取到对应的 spring6当中的xml的,容器对象
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans>// 面向接口编程,左边为接口,
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sets.xml");
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans>// 通过 id 获取到对应的类/class
  38. <?xml version="1.0" encoding="UTF-8"?>
  39. <beans xmlns="http://www.springframework.org/schema/beans"
  40.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  42.     <bean id="peopleBean" >
  43.     </bean>
  44. </beans>People peopleBean = applicationContext.getBean("peopleBean", People.class);
  45. <?xml version="1.0" encoding="UTF-8"?>
  46. <beans xmlns="http://www.springframework.org/schema/beans"
  47.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  48.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  49.     <bean id="peopleBean" >
  50.     </bean>
  51. </beans>System.out.println(peopleBean);
  52.     }
  53. }
复制代码

注意:注入List集合的时候使用list标签,如果List集合中是简单类型使用value标签,反之使用ref标签。
2.7 set注入Set集合类型

Set集合:无序不可重复
2.7.1 set 注入 Set集合简单类型
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.     <bean id="testDateBean" >
  21. <?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.     <bean id="peopleBean" >
  26.     </bean>
  27. </beans><property name="date" value="2023 -05-6"></property>
  28.     </bean>
  29. </beans>张三<?xml version="1.0" encoding="UTF-8"?>
  30. <beans xmlns="http://www.springframework.org/schema/beans"
  31.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  33.     <bean id="testDateBean" >
  34. <?xml version="1.0" encoding="UTF-8"?>
  35. <beans xmlns="http://www.springframework.org/schema/beans"
  36.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  37.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  38.     <bean id="peopleBean" >
  39.     </bean>
  40. </beans><property name="date" value="2023 -05-6"></property>
  41.     </bean>
  42. </beans>李四    <bean id="" >
  43. <?xml version="1.0" encoding="UTF-8"?>
  44. <beans xmlns="http://www.springframework.org/schema/beans"
  45.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  46.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  47.     <bean id="peopleBean" >
  48.     </bean>
  49. </beans><constructor-arg name="" value=""></constructor-arg>
  50. <?xml version="1.0" encoding="UTF-8"?>
  51. <beans xmlns="http://www.springframework.org/schema/beans"
  52.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  53.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  54.     <bean id="peopleBean" >
  55.     </bean>
  56. </beans><constructor-arg name="" ref=""></constructor-arg>
  57.     </bean>
复制代码
  1. package com.rainbowsea.bean;import java.util.Set;public class People { private Set names;    public void setNames(Set names) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.names = names;    }    @Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>return "People{" +<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="testDateBean" >
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <beans xmlns="http://www.springframework.org/schema/beans"
  20.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  22.     <bean id="peopleBean" >
  23.     </bean>
  24. </beans><property name="date" value="2023 -05-6"></property>
  25.     </bean>
  26. </beans>"names=" + names +<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.     <bean id="testDateBean" >
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans><property name="date" value="2023 -05-6"></property>
  38.     </bean>
  39. </beans>'}';    }}
复制代码
  1. package com.rainbowsea.test;
  2. import com.rainbowsea.bean.MyDataSource;
  3. import com.rainbowsea.bean.People;
  4. import org.junit.Test;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. public class SpringStructure {
  8.     @Test
  9.     public void test() {
  10. <?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  14.     <bean id="peopleBean" >
  15.     </bean>
  16. </beans>// 获取到对应的 spring6当中的xml的,容器对象
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans>// 面向接口编程,左边为接口,
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sets.xml");
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans>// 通过 id 获取到对应的类/class
  38. <?xml version="1.0" encoding="UTF-8"?>
  39. <beans xmlns="http://www.springframework.org/schema/beans"
  40.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  42.     <bean id="peopleBean" >
  43.     </bean>
  44. </beans>People peopleBean = applicationContext.getBean("peopleBean", People.class);
  45. <?xml version="1.0" encoding="UTF-8"?>
  46. <beans xmlns="http://www.springframework.org/schema/beans"
  47.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  48.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  49.     <bean id="peopleBean" >
  50.     </bean>
  51. </beans>System.out.println(peopleBean);
  52.     }
  53. }
复制代码

2.7.2 set 注入Set集合复杂类型
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean>    <bean id="" >
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans><constructor-arg name="" value=""></constructor-arg>
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans><constructor-arg name="" ref=""></constructor-arg>
  31.     </bean>    <bean id="" >
  32. <?xml version="1.0" encoding="UTF-8"?>
  33. <beans xmlns="http://www.springframework.org/schema/beans"
  34.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  35.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  36.     <bean id="peopleBean" >
  37.     </bean>
  38. </beans><constructor-arg name="" value=""></constructor-arg>
  39. <?xml version="1.0" encoding="UTF-8"?>
  40. <beans xmlns="http://www.springframework.org/schema/beans"
  41.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  42.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  43.     <bean id="peopleBean" >
  44.     </bean>
  45. </beans><constructor-arg name="" ref=""></constructor-arg>
  46.     </bean>    <bean id="" >
  47. <?xml version="1.0" encoding="UTF-8"?>
  48. <beans xmlns="http://www.springframework.org/schema/beans"
  49.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  50.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  51.     <bean id="peopleBean" >
  52.     </bean>
  53. </beans><constructor-arg name="" value=""></constructor-arg>
  54. <?xml version="1.0" encoding="UTF-8"?>
  55. <beans xmlns="http://www.springframework.org/schema/beans"
  56.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  57.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  58.     <bean id="peopleBean" >
  59.     </bean>
  60. </beans><constructor-arg name="" ref=""></constructor-arg>
  61.     </bean><?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  65.     <bean id="testDateBean" >
  66. <?xml version="1.0" encoding="UTF-8"?>
  67. <beans xmlns="http://www.springframework.org/schema/beans"
  68.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  69.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  70.     <bean id="peopleBean" >
  71.     </bean>
  72. </beans><property name="date" value="2023 -05-6"></property>
  73.     </bean>
  74. </beans>
复制代码
  1. package com.rainbowsea.bean;public class User {    private String name;    public void setName(String name) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.name = name;    }    @Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>return "User{" +<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="testDateBean" >
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <beans xmlns="http://www.springframework.org/schema/beans"
  20.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  22.     <bean id="peopleBean" >
  23.     </bean>
  24. </beans><property name="date" value="2023 -05-6"></property>
  25.     </bean>
  26. </beans>"name='" + name + '\'' +<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.     <bean id="testDateBean" >
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans><property name="date" value="2023 -05-6"></property>
  38.     </bean>
  39. </beans>'}';    }}
复制代码
  1. package com.rainbowsea.bean;import java.util.Set;public class People { private Set users;    public void setUsers(Set users) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.users = users;    }    @Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>return "People{" +<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="testDateBean" >
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <beans xmlns="http://www.springframework.org/schema/beans"
  20.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  22.     <bean id="peopleBean" >
  23.     </bean>
  24. </beans><property name="date" value="2023 -05-6"></property>
  25.     </bean>
  26. </beans>"users=" + users +<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.     <bean id="testDateBean" >
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans><property name="date" value="2023 -05-6"></property>
  38.     </bean>
  39. </beans>'}';    }}
复制代码
运行测试:
  1. package com.rainbowsea.test;
  2. import com.rainbowsea.bean.MyDataSource;
  3. import com.rainbowsea.bean.People;
  4. import org.junit.Test;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. public class SpringStructure {
  8.     @Test
  9.     public void test() {
  10. <?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  14.     <bean id="peopleBean" >
  15.     </bean>
  16. </beans>// 获取到对应的 spring6当中的xml的,容器对象
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans>// 面向接口编程,左边为接口,
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sets.xml");
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans>// 通过 id 获取到对应的类/class
  38. <?xml version="1.0" encoding="UTF-8"?>
  39. <beans xmlns="http://www.springframework.org/schema/beans"
  40.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  42.     <bean id="peopleBean" >
  43.     </bean>
  44. </beans>People peopleBean = applicationContext.getBean("peopleBean", People.class);
  45. <?xml version="1.0" encoding="UTF-8"?>
  46. <beans xmlns="http://www.springframework.org/schema/beans"
  47.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  48.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  49.     <bean id="peopleBean" >
  50.     </bean>
  51. </beans>System.out.println(peopleBean);
  52.     }
  53. }
复制代码

2.8 set注入Map集合

2.8.1 set 注入的Map集合简单类型

要点:

  • 使用标签
  • 如果key是简单类型,使用 key 属性,反之使用 key-ref 属性。
  • 如果value是简单类型,使用 value 属性,反之使用 value-ref 属性。
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean>    <bean id="" >
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans><constructor-arg name="" value=""></constructor-arg>
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans><constructor-arg name="" ref=""></constructor-arg>
  31.     </bean>    <bean id="" >
  32. <?xml version="1.0" encoding="UTF-8"?>
  33. <beans xmlns="http://www.springframework.org/schema/beans"
  34.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  35.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  36.     <bean id="peopleBean" >
  37.     </bean>
  38. </beans><constructor-arg name="" value=""></constructor-arg>
  39. <?xml version="1.0" encoding="UTF-8"?>
  40. <beans xmlns="http://www.springframework.org/schema/beans"
  41.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  42.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  43.     <bean id="peopleBean" >
  44.     </bean>
  45. </beans><constructor-arg name="" ref=""></constructor-arg>
  46.     </bean>    <bean id="" >
  47. <?xml version="1.0" encoding="UTF-8"?>
  48. <beans xmlns="http://www.springframework.org/schema/beans"
  49.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  50.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  51.     <bean id="peopleBean" >
  52.     </bean>
  53. </beans><constructor-arg name="" value=""></constructor-arg>
  54. <?xml version="1.0" encoding="UTF-8"?>
  55. <beans xmlns="http://www.springframework.org/schema/beans"
  56.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  57.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  58.     <bean id="peopleBean" >
  59.     </bean>
  60. </beans><constructor-arg name="" ref=""></constructor-arg>
  61.     </bean>    <bean id="" >
  62. <?xml version="1.0" encoding="UTF-8"?>
  63. <beans xmlns="http://www.springframework.org/schema/beans"
  64.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  65.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  66.     <bean id="peopleBean" >
  67.     </bean>
  68. </beans><constructor-arg name="" value=""></constructor-arg>
  69. <?xml version="1.0" encoding="UTF-8"?>
  70. <beans xmlns="http://www.springframework.org/schema/beans"
  71.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  72.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  73.     <bean id="peopleBean" >
  74.     </bean>
  75. </beans><constructor-arg name="" ref=""></constructor-arg>
  76.     </bean><?xml version="1.0" encoding="UTF-8"?>
  77. <beans xmlns="http://www.springframework.org/schema/beans"
  78.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  79.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  80.     <bean id="peopleBean" >
  81.     </bean>
  82. </beans>
复制代码
  1. package com.rainbowsea.bean;import java.util.Map;public class People { private Map names;    public void setNames(Map names) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.names = names;    }    @Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>return "People{" +<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="testDateBean" >
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <beans xmlns="http://www.springframework.org/schema/beans"
  20.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  22.     <bean id="peopleBean" >
  23.     </bean>
  24. </beans><property name="date" value="2023 -05-6"></property>
  25.     </bean>
  26. </beans>"names=" + names +<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.     <bean id="testDateBean" >
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans><property name="date" value="2023 -05-6"></property>
  38.     </bean>
  39. </beans>'}';    }}
复制代码
运行测试:
  1. package com.rainbowsea.test;
  2. import com.rainbowsea.bean.People;
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. public class SpringStructure {
  7.     @Test
  8.     public void test() {
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans>// 获取到对应的 spring6当中的xml的,容器对象
  16. <?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.     <bean id="peopleBean" >
  21.     </bean>
  22. </beans>// 面向接口编程,左边为接口,
  23. <?xml version="1.0" encoding="UTF-8"?>
  24. <beans xmlns="http://www.springframework.org/schema/beans"
  25.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  26.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  27.     <bean id="peopleBean" >
  28.     </bean>
  29. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sets.xml");
  30. <?xml version="1.0" encoding="UTF-8"?>
  31. <beans xmlns="http://www.springframework.org/schema/beans"
  32.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  33.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  34.     <bean id="peopleBean" >
  35.     </bean>
  36. </beans>// 通过 id 获取到对应的类/class
  37. <?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  41.     <bean id="peopleBean" >
  42.     </bean>
  43. </beans>People peopleBean = applicationContext.getBean("peopleBean", People.class);
  44. <?xml version="1.0" encoding="UTF-8"?>
  45. <beans xmlns="http://www.springframework.org/schema/beans"
  46.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  47.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  48.     <bean id="peopleBean" >
  49.     </bean>
  50. </beans>System.out.println(peopleBean);
  51.     }
  52. }
复制代码

2.8.2 set 注入的Map集合的复杂类型
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean>    <bean id="" >
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans><constructor-arg name="" value=""></constructor-arg>
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans><constructor-arg name="" ref=""></constructor-arg>
  31.     </bean>    <bean id="" >
  32. <?xml version="1.0" encoding="UTF-8"?>
  33. <beans xmlns="http://www.springframework.org/schema/beans"
  34.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  35.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  36.     <bean id="peopleBean" >
  37.     </bean>
  38. </beans><constructor-arg name="" value=""></constructor-arg>
  39. <?xml version="1.0" encoding="UTF-8"?>
  40. <beans xmlns="http://www.springframework.org/schema/beans"
  41.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  42.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  43.     <bean id="peopleBean" >
  44.     </bean>
  45. </beans><constructor-arg name="" ref=""></constructor-arg>
  46.     </bean>    <bean id="" >
  47. <?xml version="1.0" encoding="UTF-8"?>
  48. <beans xmlns="http://www.springframework.org/schema/beans"
  49.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  50.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  51.     <bean id="peopleBean" >
  52.     </bean>
  53. </beans><constructor-arg name="" value=""></constructor-arg>
  54. <?xml version="1.0" encoding="UTF-8"?>
  55. <beans xmlns="http://www.springframework.org/schema/beans"
  56.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  57.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  58.     <bean id="peopleBean" >
  59.     </bean>
  60. </beans><constructor-arg name="" ref=""></constructor-arg>
  61.     </bean>    <bean id="" >
  62. <?xml version="1.0" encoding="UTF-8"?>
  63. <beans xmlns="http://www.springframework.org/schema/beans"
  64.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  65.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  66.     <bean id="peopleBean" >
  67.     </bean>
  68. </beans><constructor-arg name="" value=""></constructor-arg>
  69. <?xml version="1.0" encoding="UTF-8"?>
  70. <beans xmlns="http://www.springframework.org/schema/beans"
  71.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  72.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  73.     <bean id="peopleBean" >
  74.     </bean>
  75. </beans><constructor-arg name="" ref=""></constructor-arg>
  76.     </bean>    <bean id="" >
  77. <?xml version="1.0" encoding="UTF-8"?>
  78. <beans xmlns="http://www.springframework.org/schema/beans"
  79.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  80.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  81.     <bean id="peopleBean" >
  82.     </bean>
  83. </beans><constructor-arg name="" value=""></constructor-arg>
  84. <?xml version="1.0" encoding="UTF-8"?>
  85. <beans xmlns="http://www.springframework.org/schema/beans"
  86.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  87.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  88.     <bean id="peopleBean" >
  89.     </bean>
  90. </beans><constructor-arg name="" ref=""></constructor-arg>
  91.     </bean>
复制代码
  1. package com.rainbowsea.bean;public class User {    private String name;    public void setName(String name) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.name = name;    }    @Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>return "User{" +<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="testDateBean" >
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <beans xmlns="http://www.springframework.org/schema/beans"
  20.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  22.     <bean id="peopleBean" >
  23.     </bean>
  24. </beans><property name="date" value="2023 -05-6"></property>
  25.     </bean>
  26. </beans>"name='" + name + '\'' +<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.     <bean id="testDateBean" >
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans><property name="date" value="2023 -05-6"></property>
  38.     </bean>
  39. </beans>'}';    }}
复制代码
  1. package com.rainbowsea.bean;import java.util.Map;public class People { private Map user; // set 注入,必须要有 set 方法    public void setUser(Map user) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.user = user;    }    @Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>return "People{" +<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="testDateBean" >
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <beans xmlns="http://www.springframework.org/schema/beans"
  20.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  22.     <bean id="peopleBean" >
  23.     </bean>
  24. </beans><property name="date" value="2023 -05-6"></property>
  25.     </bean>
  26. </beans>"user=" + user +<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.     <bean id="testDateBean" >
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans><property name="date" value="2023 -05-6"></property>
  38.     </bean>
  39. </beans>'}';    }}
复制代码
运行测试:
  1. package com.rainbowsea.test;
  2. import com.rainbowsea.bean.People;
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. public class SpringStructure {
  7.     @Test
  8.     public void test() {
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans>// 获取到对应的 spring6当中的xml的,容器对象
  16. <?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.     <bean id="peopleBean" >
  21.     </bean>
  22. </beans>// 面向接口编程,左边为接口,
  23. <?xml version="1.0" encoding="UTF-8"?>
  24. <beans xmlns="http://www.springframework.org/schema/beans"
  25.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  26.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  27.     <bean id="peopleBean" >
  28.     </bean>
  29. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sets.xml");
  30. <?xml version="1.0" encoding="UTF-8"?>
  31. <beans xmlns="http://www.springframework.org/schema/beans"
  32.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  33.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  34.     <bean id="peopleBean" >
  35.     </bean>
  36. </beans>// 通过 id 获取到对应的类/class
  37. <?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  41.     <bean id="peopleBean" >
  42.     </bean>
  43. </beans>People peopleBean = applicationContext.getBean("peopleBean", People.class);
  44. <?xml version="1.0" encoding="UTF-8"?>
  45. <beans xmlns="http://www.springframework.org/schema/beans"
  46.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  47.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  48.     <bean id="peopleBean" >
  49.     </bean>
  50. </beans>System.out.println(peopleBean);
  51.     }
  52. }
复制代码

2.9 set注入Properties 特殊的 Map 集合

java.util.Properties继承java.util.Hashtable,所以Properties也是一个Map集合。
但是虽然 Properties 是一个 Map 集合,但是在 Spring当中,是无法使用  标签进行一个 set 注入的。
对于 properties 集合类型的,set 注入使用的是 p标签
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.     <bean id="testDateBean" >
  21. <?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.     <bean id="peopleBean" >
  26.     </bean>
  27. </beans><property name="date" value="2023 -05-6"></property>
  28.     </bean>
  29. </beans>com.mysql.cj.jdbc.Driver<?xml version="1.0" encoding="UTF-8"?>
  30. <beans xmlns="http://www.springframework.org/schema/beans"
  31.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  33.     <bean id="testDateBean" >
  34. <?xml version="1.0" encoding="UTF-8"?>
  35. <beans xmlns="http://www.springframework.org/schema/beans"
  36.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  37.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  38.     <bean id="peopleBean" >
  39.     </bean>
  40. </beans><property name="date" value="2023 -05-6"></property>
  41.     </bean>
  42. </beans>jdbc:mysql//localhost:3306/spring<?xml version="1.0" encoding="UTF-8"?>
  43. <beans xmlns="http://www.springframework.org/schema/beans"
  44.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  45.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  46.     <bean id="testDateBean" >
  47. <?xml version="1.0" encoding="UTF-8"?>
  48. <beans xmlns="http://www.springframework.org/schema/beans"
  49.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  50.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  51.     <bean id="peopleBean" >
  52.     </bean>
  53. </beans><property name="date" value="2023 -05-6"></property>
  54.     </bean>
  55. </beans>root<?xml version="1.0" encoding="UTF-8"?>
  56. <beans xmlns="http://www.springframework.org/schema/beans"
  57.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  58.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  59.     <bean id="testDateBean" >
  60. <?xml version="1.0" encoding="UTF-8"?>
  61. <beans xmlns="http://www.springframework.org/schema/beans"
  62.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  63.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  64.     <bean id="peopleBean" >
  65.     </bean>
  66. </beans><property name="date" value="2023 -05-6"></property>
  67.     </bean>
  68. </beans>123456    <bean id="" >
  69. <?xml version="1.0" encoding="UTF-8"?>
  70. <beans xmlns="http://www.springframework.org/schema/beans"
  71.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  72.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  73.     <bean id="peopleBean" >
  74.     </bean>
  75. </beans><constructor-arg name="" value=""></constructor-arg>
  76. <?xml version="1.0" encoding="UTF-8"?>
  77. <beans xmlns="http://www.springframework.org/schema/beans"
  78.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  79.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  80.     <bean id="peopleBean" >
  81.     </bean>
  82. </beans><constructor-arg name="" ref=""></constructor-arg>
  83.     </bean>
复制代码
  1. package com.rainbowsea.bean;import java.util.Properties;public class People {    private Properties properties;    // set 注入必须提供 set 方法    public void setProperties(Properties properties) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.properties = properties;    }    @Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>return "People{" +<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="testDateBean" >
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <beans xmlns="http://www.springframework.org/schema/beans"
  20.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  22.     <bean id="peopleBean" >
  23.     </bean>
  24. </beans><property name="date" value="2023 -05-6"></property>
  25.     </bean>
  26. </beans>"properties=" + properties +<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.     <bean id="testDateBean" >
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans><property name="date" value="2023 -05-6"></property>
  38.     </bean>
  39. </beans>'}';    }}
复制代码
运行测试:
  1. package com.rainbowsea.test;
  2. import com.rainbowsea.bean.People;
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. public class SpringStructure {
  7.     @Test
  8.     public void test() {
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans>// 获取到对应的 spring6当中的xml的,容器对象
  16. <?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.     <bean id="peopleBean" >
  21.     </bean>
  22. </beans>// 面向接口编程,左边为接口,
  23. <?xml version="1.0" encoding="UTF-8"?>
  24. <beans xmlns="http://www.springframework.org/schema/beans"
  25.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  26.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  27.     <bean id="peopleBean" >
  28.     </bean>
  29. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sets.xml");
  30. <?xml version="1.0" encoding="UTF-8"?>
  31. <beans xmlns="http://www.springframework.org/schema/beans"
  32.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  33.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  34.     <bean id="peopleBean" >
  35.     </bean>
  36. </beans>// 通过 id 获取到对应的类/class
  37. <?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  41.     <bean id="peopleBean" >
  42.     </bean>
  43. </beans>People peopleBean = applicationContext.getBean("peopleBean", People.class);
  44. <?xml version="1.0" encoding="UTF-8"?>
  45. <beans xmlns="http://www.springframework.org/schema/beans"
  46.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  47.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  48.     <bean id="peopleBean" >
  49.     </bean>
  50. </beans>System.out.println(peopleBean);
  51.     }
  52. }
复制代码

2.10 set 注入 null 和空字符串的方式

2.10.1  set 注入 null 值的方式

当我们想要对一个属性为一个 null 值的时候,我们应该怎么做呢?
有如下两种方式:
第一种方式:方式不给属性注入,属性的默认值就是为 null 的
测试:
  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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>
复制代码
  1. package com.rainbowsea.bean;import java.util.Properties;public class People {    private String name;    // set 注入必须提供 set 方法    public void setName(String name) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.name = name;    }    // 提供一个get 方法用于测试,是否为null的    public String getName() {<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>return name;    }    @Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="peopleBean" >
  18.     </bean>
  19. </beans>return "People{" +<?xml version="1.0" encoding="UTF-8"?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  23.     <bean id="testDateBean" >
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans><property name="date" value="2023 -05-6"></property>
  31.     </bean>
  32. </beans>"name='" + name + '\'' +<?xml version="1.0" encoding="UTF-8"?>
  33. <beans xmlns="http://www.springframework.org/schema/beans"
  34.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  35.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  36.     <bean id="testDateBean" >
  37. <?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  41.     <bean id="peopleBean" >
  42.     </bean>
  43. </beans><property name="date" value="2023 -05-6"></property>
  44.     </bean>
  45. </beans>'}';    }}
复制代码
运行测试
  1. package com.rainbowsea.test;import com.rainbowsea.bean.People;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringStructure {    @Test    public void test() {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>// 获取到对应的 spring6当中的xml的,容器对象<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>// 面向接口编程,左边为接口,<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="peopleBean" >
  18.     </bean>
  19. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sets.xml");<?xml version="1.0" encoding="UTF-8"?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  23.     <bean id="peopleBean" >
  24.     </bean>
  25. </beans>// 通过 id 获取到对应的类/class<?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  29.     <bean id="peopleBean" >
  30.     </bean>
  31. </beans>People peopleBean = applicationContext.getBean("peopleBean", People.class);<?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans>// 将小写字母转换为大写字母,如果name为空报 null 异常测试:<?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  41.     <bean id="peopleBean" >
  42.     </bean>
  43. </beans>System.out.println(peopleBean.getName().toUpperCase());    }}
复制代码

方式二:使用  的标签进行一个属性 set 注入赋值为 null 的操作
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.     <bean id="testDateBean" >
  21. <?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.     <bean id="peopleBean" >
  26.     </bean>
  27. </beans><property name="date" value="2023 -05-6"></property>
  28.     </bean>
  29. </beans>
复制代码
运行测试:
  1. package com.rainbowsea.test;import com.rainbowsea.bean.People;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringStructure {    @Test    public void test() {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>// 获取到对应的 spring6当中的xml的,容器对象<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>// 面向接口编程,左边为接口,<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="peopleBean" >
  18.     </bean>
  19. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sets.xml");<?xml version="1.0" encoding="UTF-8"?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  23.     <bean id="peopleBean" >
  24.     </bean>
  25. </beans>// 通过 id 获取到对应的类/class<?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  29.     <bean id="peopleBean" >
  30.     </bean>
  31. </beans>People peopleBean = applicationContext.getBean("peopleBean", People.class);<?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans>// 将小写字母转换为大写字母,如果name为空报 null 异常测试:<?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  41.     <bean id="peopleBean" >
  42.     </bean>
  43. </beans>System.out.println(peopleBean.getName().toUpperCase());    }}
复制代码

注意点:就是如果我们将 value = null ,set 注入的话,并不是将 value 的属性值,赋值为了null,而是赋值为了一个 null 的字符串而已。
  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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <beans xmlns="http://www.springframework.org/schema/beans"
  7.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  8.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  9.     <bean id="peopleBean" >
  10.     </bean>
  11. </beans><constructor-arg index="0" value=""></constructor-arg>
  12. <?xml version="1.0" encoding="UTF-8"?>
  13. <beans xmlns="http://www.springframework.org/schema/beans"
  14.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  15.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  16.     <bean id="peopleBean" >
  17.     </bean>
  18. </beans><constructor-arg index="1" ref=""></constructor-arg>
  19.     </bean>
  20. </beans>
复制代码

2.10.2  set 注入空字符串值的方式

同样的,如果我们想要将一个属性的值,赋值为 空字符串 该怎么做呢?
**方式一:我们可以 value="" 的方式进行一个空字符串的赋值 **
  1. package com.rainbowsea.bean;public class People {    private String name;    // set 注入必须提供 set 方法    public void setName(String name) {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>this.name = name;    }    @Override    public String toString() {<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>return "People{" +<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="testDateBean" >
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <beans xmlns="http://www.springframework.org/schema/beans"
  20.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  22.     <bean id="peopleBean" >
  23.     </bean>
  24. </beans><property name="date" value="2023 -05-6"></property>
  25.     </bean>
  26. </beans>"name='" + name + '\'' +<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.     <bean id="testDateBean" >
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans><property name="date" value="2023 -05-6"></property>
  38.     </bean>
  39. </beans>'}';    }}
复制代码
  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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="testDateBean" >
  6. <?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.     <bean id="peopleBean" >
  11.     </bean>
  12. </beans><property name="date" value="2023 -05-6"></property>
  13.     </bean>
  14. </beans>
复制代码

方式二:  使    进行一个空字符串的 set 注入
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.     <bean id="peopleBean" >
  21.     </bean>
  22. </beans>   
复制代码

2.11 set 注入的属性值当中含有特殊符号,怎么处理

注意: 当我们在 xml 文件中出现了,特殊的字符串的时候,不然不进行一个特殊的处理的话,是会报错的。
XML 中有 5 个特殊字符,分别是:、'、"、& 。以上这 5 个特殊符号在 xml 中会被特殊对待,会被当做xml语法的一部分进行解析,如果这些特殊符号直接出现注入的字符串当中,会报错的。具体体现如下:

The value of attribute "value" associated with an element type "property" must not contain the '[/code]具体如下图所示:

第四步:在spring中配置使用jdbc.properties文件。
格式如下:
  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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.         <property name="name" value="2 < 3"></property>
  7.     </bean>
  8. </beans>
复制代码

具体的如下图所示:
  1.     <bean id="" >
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="peopleBean" >
  7.     </bean>
  8. </beans><constructor-arg name="" value=""></constructor-arg>
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  13.     <bean id="peopleBean" >
  14.     </bean>
  15. </beans><constructor-arg name="" ref=""></constructor-arg>
  16.     </bean>    <bean id="" >
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  21.     <bean id="peopleBean" >
  22.     </bean>
  23. </beans><constructor-arg name="" value=""></constructor-arg>
  24. <?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.     <bean id="peopleBean" >
  29.     </bean>
  30. </beans><constructor-arg name="" ref=""></constructor-arg>
  31.     </bean><?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans>
复制代码
测试程序:
  1. package com.rainbowsea.test;import com.rainbowsea.bean.MyDataSource;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringStructure {    @Test    public void test() {<?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"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="peopleBean" >
  6.     </bean>
  7. </beans>// 获取到对应的 spring6当中的xml的,容器对象<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.     <bean id="peopleBean" >
  12.     </bean>
  13. </beans>// 面向接口编程,左边为接口,<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.     <bean id="peopleBean" >
  18.     </bean>
  19. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sets.xml");<?xml version="1.0" encoding="UTF-8"?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  23.     <bean id="peopleBean" >
  24.     </bean>
  25. </beans>// 通过 id 获取到对应的类/class<?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  29.     <bean id="peopleBean" >
  30.     </bean>
  31. </beans>MyDataSource ds = applicationContext.getBean("ds", MyDataSource.class);<?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.     <bean id="peopleBean" >
  36.     </bean>
  37. </beans>System.out.println(ds);    }}
复制代码

5. 总结:


  • 依赖注入:

    • 依赖指的是对象和对象 之间的关联关系。
    • 注入指的是一种数据传递行为,通过注入行为来让对象和对象产生关系。
      依赖注入常见的实现方式包括两种:
    • 第一种:set注入 : set 注入必须实现了其中类的 set () 方法不然,是实现 set 注入的。因为set 注入的底层调用的是 set () 方法
    • 第二种: 构造注入: 构造注入必须实现了对应参数的构造方法,不然是无法实现构造注入的。因为构造注入的底层调用的是 对应的构造方法。

  • 使用其中 set 注入的各种应用场景:

    • 简单类型的set 注入使用  value  属性操作
    • 复杂类型的 set 注入使用 ref 属性进行一个赋值操作

  • set 注入的各种类型的格式处理:

    • 数组类型
    • 集合类型: Set,List,Map, Properties 集合

  • set 注入当中 如何实现 null 指的赋值,以及空字符串 的赋值
  • xml 当中,五种特殊的字符串的处理的方式:XML 中有 5 个特殊字符,分别是:、'、"、& 。以上这 5 个特殊符号在 xml 中会被特殊对待,会被当做xml语法的一部分进行解析,如果这些特殊符号直接出现注入的字符串当中,会报错的。
  • 命名空间上的使用:简化配置信息: p命名空间,c命名空间,util 命名空间

    • p命名空间底层调用的还是  set () 方法
    • c命名空间底层调用的是 构造方法
    • util 命名空间进行一个配置信息的复用

  • Spring 当中的 xml 读取到外部的 properties 配置文件的信息的操作。
6. 最后:

“在这个最后的篇章中,我要表达我对每一位读者的感激之情。你们的关注和回复是我创作的动力源泉,我从你们身上吸取了无尽的灵感与勇气。我会将你们的鼓励留在心底,继续在其他的领域奋斗。感谢你们,我们总会在某个时刻再次相遇。”


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

吴旭华

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表