对于集合list, set, map和props元素的配置方式如下:
1. list
示例代码如下:
//HelloWorld.java
package com.gc.action;
import java.util.Date;
import java.util.List;
public class HelloWorld{
//该变量用来存储字符串
public List<String> msg = null;
//该变量用来存储日期
public Date date = null;
//设定变量msg的set方法
public void setMsg(List<String> msg){
this.msg = msg;
}
//获取变量msg的get方法
public List<String> getMsg(){
return this.msg;
}
//设定变量date的set方法
public void setDate(Date date){
this.date = date;
}
//获取变量date的get方法
public Date getDate(){
return this.date;
}
}
//配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 定义一个Bean -->
<bean id="HelloWorld" class="com.gc.action.HelloWorld" autowire="no">
<property name="msg">
<!-- 集合list元素的配置方式-->
<list>
<value>bbwl2</value>
<value>bbwl3</value>
<value>bbwl4</value>
</list>
</property>
<property name="date">
<ref bean="date" />
</property>
</bean>
<bean id="date" class="java.util.Date" />
</beans>
2. set
示例代码如下:
//HelloWorld.java
package com.gc.action;
import java.util.Date;
import java.util.Set;
public class HelloWorld{
//该变量用来存储字符串
public Set<String> msg = null;
//该变量用来存储日期
public Date date = null;
//设定变量msg的set方法
public void setMsg(Set<String> msg){
this.msg = msg;
}
//获取变量msg的get方法
public Set<String> getMsg(){
return this.msg;
}
//设定变量date的set方法
public void setDate(Date date){
this.date = date;
}
//获取变量date的get方法
public Date getDate(){
return this.date;
}
}
//配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 定义一个Bean -->
<bean id="HelloWorld" class="com.gc.action.HelloWorld" autowire="no">
<property name="msg">
<!-- 集合set元素的配置方式-->
<set>
<value>bbwl2</value>
<value>bbwl3</value>
<value>bbwl4</value>
</set>
</property>
<property name="date">
<ref bean="date" />
</property>
</bean>
<bean id="date" class="java.util.Date" />
</beans>
3. map
示例代码如下:
//HelloWorld.java
package com.gc.action;
import java.util.Date;
import java.util.Map;
public class HelloWorld{
//该变量用来存储字符串
public Map<String,String> msg = null;
//该变量用来存储日期
public Date date = null;
//设定变量msg的set方法
public void setMsg(Map<String,String> msg){
this.msg = msg;
}
//获取变量msg的get方法
public Map<String,String> getMsg(){
return this.msg;
}
//设定变量date的set方法
public void setDate(Date date){
this.date = date;
}
//获取变量date的get方法
public Date getDate(){
return this.date;
}
}
//配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 定义一个Bean -->
<bean id="HelloWorld" class="com.gc.action.HelloWorld" autowire="no">
<property name="msg">
<!-- 集合map元素的配置方式-->
<map>
<entry key="bbwl1">
<value>Hello1</value>
</entry>
<entry key="bbwl2">
<value>Hello2</value>
</entry>
<entry key="bbwl3">
<value>Hello3</value>
</entry>
</map>
</property>
<property name="date">
<ref bean="date" />
</property>
</bean>
<bean id="date" class="java.util.Date" />
</beans>
4. Properties
//HelloWorld.java
package com.gc.action;
import java.util.Date;
import java.util.Properties;
public class HelloWorld{
//该变量用来存储字符串
public Properties msg = null;
//该变量用来存储日期
public Date date = null;
//设定变量msg的set方法
public void setMsg(Properties msg){
this.msg = msg;
}
//获取变量msg的get方法
public Properties getMsg(){
return this.msg;
}
//设定变量date的set方法
public void setDate(Date date){
this.date = date;
}
//获取变量date的get方法
public Date getDate(){
return this.date;
}
}
//配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 定义一个Bean -->
<bean id="HelloWorld" class="com.gc.action.HelloWorld" autowire="no">
<property name="msg">
<!-- 集合Properties元素的配置方式-->
<props>
<prop key="bbwl1">Hello1</prop>
<prop key="bbwl2">Hello2</prop>
<prop key="bbwl3">Hello3</prop>
</props>
</property>
<property name="date">
<ref bean="date" />
</property>
</bean>
<bean id="date" class="java.util.Date" />
</beans>