ApplicationContext.xml
在上下文环境配置文件中对list, map, set, properties进行配置,以提供bean实例
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="cur" class="java.util.Date"/>
<!-- scope="singleton"意味单例工厂模式 -->
<bean id="userAction" class="cn.com.xalead.spring.UserAction" scope="singleton">
<property name="manager">
<ref bean="userManager"/>
</property>
<property name="list">
<list>
<value>2222</value>
<ref local="cur"/>
</list>
</property>
<property name="set">
<set>
<value>Hello</value>
<value>234.22</value>
<value>22222</value>
<value type="java.lang.String">true</value>
</set>
</property>
<property name="map">
<map>
<entry key="a1">
<value>afasfdsfa</value>
</entry>
<entry key="a2">
<value>34345</value>
</entry>
</map>
</property>
<property name="props">
<props>
<prop key="x1">adsfas</prop>
<prop key="x2">34534</prop>
</props>
</property>
</bean>
</beans>
UserAction.java
这个文件主要是定义变量及生成set方法
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class UserAction{
private List list = null;
private Set set = null;
private Map map = null;
private Properties props = null;
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Properties getProps() {
return props;
}
public void setProps(Properties props) {
this.props = props;
}
private UserManager manager = null;
public void setManager(UserManager manager) {
this.manager = manager;
}
}
运行测试
public static void main(String[] args) {
// BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
BeanFactory factory = new ClassPathXmlApplicationContext("app*.xml");
// BeanFactory fa1 = new XmlBeanFactory(new ClassPathResource("appliactionContext_1.xml"),factory);
UserAction ua = (UserAction)factory.getBean("userAction");
for(Object o : ua.getList()){
System.out.println(o);
}
System.out.println("--------------------------------------");
for(Object o : ua.getSet()){
System.out.println(o);
}
System.out.println("---------------------------------------");
for(Iterator iter = ua.getMap().entrySet().iterator() ; iter.hasNext(); ){
Entry entry = (Entry)iter.next();
System.out.println(entry.getKey() + ":" + entry.getValue());
}
System.out.println("---------------------------------------");
for(Iterator iter = ua.getProps().keySet().iterator(); iter.hasNext(); ){
String key = (String)iter.next();
System.out.println(key + ":" + ua.getProps().getProperty(key));
}
}
测试结果