[置顶] Spring 集合注入 [ Collection Injection ]
本文内容纲要:[置顶] Spring 集合注入 [ Collection Injection ]
对于简单数据类型(byte,char,short,int,float,double,long )或者String的注入,一般只需写入标签
<property name="propertyName" value="simpleValue" />
或者
<property name="propertyName"> <value>simpleValue</value>
</property>
或者p模式
如果需要注入的是集合(list,set,map,pros),那该怎么做呢?
如果集合的泛型是简单数据类型,比如 List
<property name="students"> <list>
<value>student1</value>
<value>student2</value>
...
</list>
</property>
如果集合的泛型是引用类型,比如List
<property name="students"> <list>
<bean class="FullQualifiedNameClass">
<property name="name" value="zhangsan" />
<property name="age" value="20" />
</bean>
<bean class="FullQualifiedNameClass">
<property name="name" value="lisi" />
<property name="age" value="22" />
</bean>
......
</list>
</property>
下面是一个具体例子:
省份类:
public class Province { private String name; // 省份名称
private long population; // 人口
private List<String> cities; // 城市列表
private List<Official> officials; // 官员
// 标准setter 和 getter 以及 toString方法省略
}
public class Official {
private String name;
private String title;
private String age;
// 省略setter 和 getter 以及 toString方法
}
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.xsd">
<bean id="sichuan" class="org.spring.collection.Province" scope="singleton">
<property name="name">
<value>四川</value>
</property>
<property name="population" value="87225224" />
<property name="cities">
<list>
<value>成都</value>
<value>绵阳</value>
<value>德阳</value>
<value>攀枝花</value>
<value>遂宁</value>
<value>江油</value>
</list>
</property>
<property name="officials">
<list>
<bean class="org.spring.collection.Official">
<property name="name" value="zhangsan" />
<property name="age" value="45" />
<property name="title" value="mayor" />
</bean>
<bean class="org.spring.collection.Official">
<property name="name" value="lisi" />
<property name="age" value="55" />
<property name="title" value="coutrier" />
</bean>
<bean class="org.spring.collection.Official">
<property name="name" value="wangwu" />
<property name="age" value="42" />
<property name="title" value="villager" />
</bean>
</list>
</property>
</bean>
</beans>
运行它:
public class App { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"SpringConfig.xml"});
Province siChuan = (Province) context.getBean("sichuan");
System.out.println(siChuan);
}
}
打印输出:
Province [name=四川, population=87225224, cities=[成都, 绵阳, 德阳, 攀枝花, 遂宁, 江油], officials=[Official [name=zhangsan, title=mayor, age=45], Official [name=lisi, title=coutrier, age=55], Official [name=wangwu, title=villager, age=42]]]
这里只讲了List集合,Set,Map以及Properties以后再讲吧。
本文内容总结:[置顶] Spring 集合注入 [ Collection Injection ]
原文链接:https://www.cnblogs.com/javawebsoa/archive/2013/04/03/2998523.html
以上是 [置顶] Spring 集合注入 [ Collection Injection ] 的全部内容, 来源链接: utcz.com/z/296017.html