Spring 集合注入 [ Collection Injection ]
来源:互联网 发布:简易电气画图软件 编辑:程序博客网 时间:2024/04/29 21:13
对于简单数据类型(byte,char,short,int,float,double,long )或者String的注入,一般只需写入标签<value>即可。比如:
<property name="propertyName" value="simpleValue" />或者
<property name="propertyName"> <value>simpleValue</value></property>
或者p模式
如果需要注入的是集合(list,set,map,pros),那该怎么做呢?
如果集合的泛型是简单数据类型,比如 List<String>,可以这样实现:
<property name="students"> <list> <value>student1</value> <value>student2</value> ... </list></property>
如果集合的泛型是引用类型,比如List<Student>,可以利用内部bean实现
<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 ]
- Spring依赖注入(dependency injection)
- Spring笔记之二(Collection Injection)
- Spring笔记之二(Collection Injection)
- spring依赖注入(dependency injection)
- spring 集合注入方式
- spring集合注入
- Spring -------集合对象注入
- Spring注入集合
- Spring集合注入
- Spring 集合注入
- spring集合注入案例
- Spring集合注入
- Spring 集合注入
- spring之集合注入
- Spring集合注入
- Spring bean 集合注入
- Spring注入集合属性
- UVa 10905 - Children's Game
- Mouhout运行k-means算法
- ubuntu 为Apache Hadoop源码建立Eclipse项目
- css3 transition unfold(折叠效果)
- [Latex]实现文字闪烁的示例
- Spring 集合注入 [ Collection Injection ]
- LA - 5734(hdu - 4162) - Shape Number(指针扫描+贪心)
- C语言字符串操作函数
- 运行hadoop的WordCount程序——编译,打包,运行
- DotA-II,让我们关注游戏行业里消逝的东西
- 哪位大神知道,jquery中UI的dialog的buttons中的属性,比如文字和背景图片等了,憋了2天啦
- 嵌入式交叉编译环境搭建
- 红糖什么牌子好
- 网络台球游戏的网络编程(vc++)