1.集合属性

在Spring中可以通过一组内置的xml标签(例如<list>,<set>或<map>)来配置集合属性。

2.配置List集合

配置java.util.List类型的属性,需要指定<list>标签,在标签中包含一些元素,这些标签可以通过<value>指定简单的常量值,通过<ref>指定对其他Bean的引用,通过<bean>指定内置Bean定义,通过<null/>指定空元素,甚至可以内嵌其他集合。

 <bean id="car3" class="com.wzy.collection.Car">
<constructor-arg value="Audi" index="0" type="java.lang.String"/>
<constructor-arg value="ShangHai" index="1" type="java.lang.String"/>
<constructor-arg value="400000" index="2" type="double"/>
</bean> <bean id="car4" class="com.wzy.collection.Car">
<constructor-arg value="baoma" index="0" type="java.lang.String"/>
<constructor-arg value="Guangzhou" index="1" type="java.lang.String"/>
<constructor-arg value="30000000" index="2" type="double"/>
</bean> <bean id="car5" class="com.wzy.collection.Car">
<constructor-arg value="benchi" index="0" type="java.lang.String"/>
<constructor-arg value="ShenZhen" index="1" type="java.lang.String"/>
<constructor-arg value="2000000" index="2" type="double"/>
</bean> <!--测试如何配置集合属性-->
<bean class="com.wzy.collection.Person" id="person3">
<property name="name" value="mike"/>
<property name="age" value="20"/>
<property name="cars">
<list>
<ref bean="car3"/>
<ref bean="car4"/>
<ref bean="car5"/>
</list>
</property>
</bean>

Java代码:

 private static void testCollection1() {
//测试集合注入
//1.获取IOC容器ApplicationContext
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
//2.通过容器获取对象
Person person = (Person) ctx.getBean("person3");
//3.输出person
System.out.println(person);
}

输出结果:

3.数组

数组的定义和List一样,都使用<list>。

4.Set集合

配置java.util.Set需要使用<set>标签,定义元素的方法与List一样。

5.Map集合

java.util.Map通过<map>标签定义,<map>标签里可以使用多个<entry>作为子标签,每个条目包含一个键和一个值。必须在<Key>标签定义键值因为键和值的类型没有限制,所以可以自由地为它们指定<value>、<ref>、<bean>、<null>元素。可以将Map的键和值作为<entry>的属性定义:简单常量使用key和value来定义;bean引用通过key-ref和value-ref属性定义。

 <bean id="newPerson" class="com.wzy.collection.NewPerson">
<property name="name" value="wzy"/>
<property name="age" value="24"/>
<property name="cars">
<!--使用map节点及map的entry子节点配置Map类型的成员变量-->
<map>
<entry key="1" value-ref="car3"/>
<entry key="2" value-ref="car4"/>
<entry key="3" value-ref="car5"/>
</map>
</property>
</bean>

Java代码:

 private static void testCollection2() {
//1.获取IOC容器ApplicationContext
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
//2.通过容器获取对象
NewPerson newPerson = (NewPerson) ctx.getBean("newPerson");
//3.输出newPerson
System.out.println(newPerson);
}

输出结果:

6.properties

使用<props>定义java.utils.Properties, 该标签使用多个<prop>作为子标签,每个<prop>标签必须定义key属性。

spring.xml

 <bean id="dataSource" class="com.wzy.collection.DataSource">
<property name="properties">
<props>
<prop key="url">jdbc:mysql://localhost:3306/db</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
<prop key="driver">Driver</prop>
</props>
</property>
</bean>

Java Code:

 private static void testCollection3() {
//1.获取IOC容器ApplicationContext
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
//2.通过容器获取对象
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
//3.输出dataSource
System.out.println(dataSource);
}

输出结果:

7.使用utility scheme定义集合

在某些情况下,我们需要多个bean同时引用同一个集合元素,那么我们就需要将这些集合元素抽取出来,这样集合元素就可以在不同Bean之间共享集合。将集合元素抽取出来,可以使用util schema里的集合标签定义独立的集合Bean,需要注意的是必须在<beans>根元素里添加util schema定义。

这里首先需要引入util的命名空间

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

通过util标签定义公共的集合,通过属性注入中的ref属性可以直接进行引用

 <!--配置独立的集合bean以供多个bean进行引用-->
<util:list id="cars">
<ref bean="car3"/>
<ref bean="car4"/>
<ref bean="car5"/>
</util:list> <bean id="person4" class="com.wzy.collection.Person">
<property name="name" value="wzy"/>
<property name="age" value="24"/>
<property name="cars" ref="cars"/>
</bean>

最新文章

  1. 网站开启https后加密协议始终是TLS1.0如何配置成TLS1.2?
  2. 通过ipv6访问 g o o g l e
  3. 使用ViewModel来实现多个Model传送至视图
  4. matlab求解相关系数
  5. 关于NGUI与原生2D混用相互遮盖的问题心得
  6. DOM(六)事件类型
  7. characterCustomezition的资源打包代码分析
  8. sdf
  9. iq 格式分析
  10. Android中G-Sensor相关流程
  11. Android数据库高手秘籍(五)——LitePal的存储操作
  12. 无法打开登录 &#39;ASPState&#39; 中请求的数据库。登录失败。
  13. IntelliJ IDEA创建java项目
  14. hdu4148 Length of S(n)
  15. Vlan ---虚拟局域网
  16. KindEditor 4.1.2版本,在上传图片的时候 设置为绝对路径
  17. SDN 第二次上机作业
  18. Kubernetes HPA
  19. Java中取两位小数
  20. CSS深入理解之overflow(HTML/CSS)

热门文章

  1. BZOJ 2731 Luogu P3219 [HNOI2012]三角形覆盖问题 (扫描线)
  2. Spring 4.2.2以上版本和swagger集成方案和踩过的坑
  3. aws常用命令
  4. wannalfy 挑战赛7 E 珂朵莉与GCD (离线+线段树/树状数组)
  5. [nginx] CORS配置多域名
  6. 当遇到npm ERR! Unexpected end of JSON input while parsing near……时的解决办法
  7. Java第一次学习总结
  8. @清晰掉 Sizeof与字符串
  9. 使用Map接收返回数据库的数据
  10. Lombok 注解简介