1.集合属性

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

 

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

2.配置List集合

 

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

      

 1 <bean id="car3" class="com.wzy.collection.Car">
 2         <constructor-arg value="Audi" index="0" type="java.lang.String"/>
 3         <constructor-arg value="ShangHai" index="1" type="java.lang.String"/>
 4         <constructor-arg value="400000" index="2" type="double"/>
 5     </bean>
 6 
 7     <bean id="car4" class="com.wzy.collection.Car">
 8         <constructor-arg value="baoma" index="0" type="java.lang.String"/>
 9         <constructor-arg value="Guangzhou" index="1" type="java.lang.String"/>
10         <constructor-arg value="30000000" index="2" type="double"/>
11     </bean>
12 
13     <bean id="car5" class="com.wzy.collection.Car">
14         <constructor-arg value="benchi" index="0" type="java.lang.String"/>
15         <constructor-arg value="ShenZhen" index="1" type="java.lang.String"/>
16         <constructor-arg value="2000000" index="2" type="double"/>
17     </bean>
18 
19     <!--测试如何配置集合属性-->
20     <bean class="com.wzy.collection.Person" id="person3">
21         <property name="name" value="mike"/>
22         <property name="age" value="20"/>
23         <property name="cars">
24             <list>
25                 <ref bean="car3"/>
26                 <ref bean="car4"/>
27                 <ref bean="car5"/>
28             </list>
29         </property>
30     </bean>

 

Java代码:

 

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

 

 输出结果:

Spring基础07——配置集合属性 随笔 第1张

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属性定义。

 

 

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

Java代码:

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

输出结果:

Spring基础07——配置集合属性 随笔 第2张

 

6.properties

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

 spring.xml

 1 <bean id="dataSource" class="com.wzy.collection.DataSource">
 2         <property name="properties">
 3             <props>
 4                 <prop key="url">jdbc:mysql://localhost:3306/db</prop>
 5                 <prop key="username">root</prop>
 6                 <prop key="password">123456</prop>
 7                 <prop key="driver">Driver</prop>
 8             </props>
 9         </property>
10     </bean>

Java Code:

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

输出结果:

Spring基础07——配置集合属性 随笔 第3张

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄