Spring4.0学习3--集合属性

接Spring4.0学习2

集合属性

List属性值

在 Spring中可以通过一组内置的 xml 标签(例如: <list>, <set><map>) 来配置集合属性.
配置 java.util.List 类型的属性, 需要指定 <list> 标签, 在标签里包含一些元素. 这些标签可以通过 <value> 指定简单的常量值, 通过 <ref> 指定对其他 Bean 的引用. 通过<bean> 指定内置 Bean 定义. 通过 <null/> 指定空元素. 甚至可以内嵌其他集合.
数组的定义和 List 一样, 都使用 <list>
配置 java.util.Set 需要使用 <set> 标签, 定义元素的方法与 List 一样.

新建了一个包com.wuyanzu,在该包下新建一个Person类和Main类。该包和com.lizz包属于并列关系。

applicationContext.xml新增代码:

1
2
3
4
5
6
7
8
9
10
11
12
<!-- 集合属性 -->
<bean id="person3" class="com.wuyanzu.Person">
<property name="name" value="wuyanzu"></property>
<property name="age" value="44"></property>
<property name="cars">
<!-- 使用list节点为list类型的属性赋值 -->
<list>
<ref bean="car" />
<ref bean="car2" />
</list>
</property>
</bean>

Person.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.wuyanzu;
import java.util.List;
import com.lizz.Car;
public class Person {
private String name;
private int age;
private List<Car> cars; // 因为是集合属性,所以这里的cars是集合
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
@Override
public String toString() {
return "Person3 [name=" + name + ", age=" + age + ", cars=" + cars + "]";
}
}

Main.java:

1
2
3
4
5
6
7
8
9
10
package com.wuyanzu;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person3 = (Person) ctx.getBean("person3");
System.out.println(person3);
}
}

Map属性值

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

1
2
3
4
5
6
7
8
9
10
11
12
<!-- Map属性 -->
<bean id="jay" class="com.wuyanzu.Jay">
<property name="name" value="jay"></property>
<property name="age" value="39"></property>
<property name="cars">
<!-- 使用map节点及map的entry子节点配置Map类型的成员变量 -->
<map>
<entry key="A" value-ref="car" />
<entry key="B" value-ref="car2" />
</map>
</property>
</bean>

Main.java增加的代码:

1
2
Jay jay = (Jay) ctx.getBean("jay");
System.out.println(jay);

Jay.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.wuyanzu;
import java.util.Map;
import com.lizz.Car;
public class Jay {
private String name;
private int age;
private Map<String, Car> cars;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Map<String, Car> getCars() {
return cars;
}
public void setCars(Map<String, Car> cars) {
this.cars = cars;
}
@Override
public String toString() {
return "Jay [name=" + name + ", age=" + age + ", cars=" + cars + "]";
}
}

<props>配置法
新建Chenglong.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.wuyanzu;
import java.util.Properties;
public class Chenglong {
private Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "Chenglong [properties=" + properties + "]";
}
}

applicationContext.xml新增代码:

1
2
3
4
5
6
7
8
9
10
11
12
<!-- 配置Properties属性值 -->
<bean id="chenglong" class="com.wuyanzu.Chenglong">
<property name="properties">
<!-- 使用props和prop子节点来为Properties属性赋值 -->
<props>
<prop key="username">root</prop>
<prop key="password">123456</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>

Main.java:

1
2
Chenglong chenglong = ctx.getBean(Chenglong.class);
System.out.println(chenglong.getProperties());

使用 utility scheme 定义集合

使用基本的集合标签定义集合时, 不能将集合作为独立的 Bean 定义, 导致其他 Bean 无法引用该集合, 所以无法在不同 Bean 之间共享集合.
可以使用 util schema 里的集合标签定义独立的集合 Bean. 需要注意的是, 必须在 <beans> 根元素里添加 util schema 定义

示例:
applicationContext.xml:

1
2
3
4
5
6
7
8
9
10
<!-- 配置单例的集合bean,以供多个bean进行引用,需要导入 util 命名空间 -->
<util:list id="cars">
<ref bean="car"/>
<ref bean="car2"/>
</util:list>
<bean id="person4" class="com.wuyanzu.Person">
<property name="name" value="周星驰"></property>
<property name="age" value="55"></property>
<property name="cars" ref="cars"></property>
</bean>

Main.java:

1
2
Person person4 = (Person) ctx.getBean("person4");
System.out.println(person4);

通过 p 命名空间为 bean 赋值

applicationContext.xml:

1
2
3
<!-- 通过 p 命名空间对 bean 的属性赋值,需要先导入 p 命名空间,相对于传统的配置更加简洁 -->
<bean id="person5" class="com.wuyanzu.Person" p:name="lizz" p:age="25" p:cars-ref="cars">
</bean>

Main.java:

1
2
Person person5 = (Person) ctx.getBean("person5");
System.out.println("person5:" + person5);

Newer Post

Spring4.0学习4--自动装配

XML 配置里的 Bean 自动装配 Spring IOC 容器可以自动装配 Bean. 需要做的仅仅是在 &lt;bean&gt; 的 autowire 属性里指定自动装配的模式byType(根据类型自动装配): 若 IOC 容器中有多个与目标 Bean 类型一致的 Bean. 在这种情况下, S …

继续阅读
Older Post

开发银联退款的经历

可以参考:http://blog.csdn.net/haozhuxuan/article/details/53637243https://open.unionpay.com/upload/download/%E5%B9%B3%E5%8F%B0%E6%8E%A5%E5%85%A5%E6%8E%A5%E …

继续阅读