Spring 中如何为Bean注入集合呢?
下文讲述Spring中为Bean注入集合的方法分享,如下所示:
常见的集合类型有:
list、Set、Map 和 properties
标签
例:
List Elements :[java265爱好者, 百度, java265中文站, java265中文站]
Set Elements :[java265爱好者, 百度, java265中文站]
Map Elements :{1=java265爱好者, 2=百度, 3=java265中文站, 4=java265中文站}
Property Elements :{two=百度, one=java265爱好者, three=java265中文站, four=java265中文站}
注入Bean引用
也可以在集合元素中注入 Bean
当我们需传入一个null值,需采用以下写法
常见的集合类型有:
list、Set、Map 和 properties
标签
| 集合名称 | 说明 |
| <list> | 用于注入 list 类型的值,允许重复 |
| <set> | 用于注入 set 类型的值,不允许重复 |
| <map> | 用于注入 key-value 的集合,其中 key-value 可以是任意类型 |
| <props> | 用于注入 key-value 的集合,其中 key-value 都是字符串类型 |
- 创建SpringDemo 项目
- 在src目录下创建 com.java265 包
- 添加相应的 jar 包,可以查看我的第一个Spring程序
- 在 com.java265 包下创建 JavaCollection、Man 和 MainApp 类
- 在 src 目录下创建 Spring 配置文件 Beans.xml
- 运行 SpringDemo 项目
package com.java265;
import java.util.*;
public class JavaCollection {
List manList;
Set manSet;
Map manMap;
Properties manProp;
public void setManList(List manList) {
this.manList = manList;
}
public List getManList() {
System.out.println("List Elements :" + manList);
return manList;
}
public void setManSet(Set manSet) {
this.manSet = manSet;
}
public Set getManSet() {
System.out.println("Set Elements :" + manSet);
return manSet;
}
public void setManMap(Map manMap) {
this.manMap = manMap;
}
public Map getManMap() {
System.out.println("Map Elements :" + manMap);
return manMap;
}
public void setManProp(Properties manProp) {
this.manProp = manProp;
}
public Properties getManProp() {
System.out.println("Property Elements :" + manProp);
return manProp;
}
}
MainApp 类
package com.java265;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
JavaCollection jc = (JavaCollection) context.getBean("javaCollection");
jc.getManList();
jc.getManSet();
jc.getManMap();
jc.getManProp();
}
}
Beans.xml 配置文件
<?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="javaCollection" class="com.java265.JavaCollection"> <property name="manList"> <list> <value>java265爱好者</value> <value>百度</value> <value>java265中文站</value> <value>java265中文站</value> </list> </property> <property name="manSet"> <set> <value>java265爱好者</value> <value>百度</value> <value>java265中文站</value> <value>java265中文站</value> </set> </property> <property name="manMap"> <map> <entry key="1" value="java265爱好者" /> <entry key="2" value="百度" /> <entry key="3" value="java265中文站" /> <entry key="4" value="java265中文站" /> </map> </property> <property name="manProp"> <props> <prop key="one">java265爱好者</prop> <prop key="one">java265爱好者</prop> <prop key="two">百度</prop> <prop key="three">java265中文站</prop> <prop key="four">java265中文站</prop> </props> </property> </bean> </beans>运行结果-----
List Elements :[java265爱好者, 百度, java265中文站, java265中文站]
Set Elements :[java265爱好者, 百度, java265中文站]
Map Elements :{1=java265爱好者, 2=百度, 3=java265中文站, 4=java265中文站}
Property Elements :{two=百度, one=java265爱好者, three=java265中文站, four=java265中文站}
注入Bean引用
也可以在集合元素中注入 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 class="..." id="..."> <property name="manList"> <list> <ref bean="man1"> <ref bean="man2"> <value>java265爱好者</value> </ref></ref></list> </property> <property name="manSet"> <set> <ref bean="man1"> <ref bean="man2"> <value>java265爱好者</value> </ref></ref></set> </property> <property name="manMap"><map> <entry key="one" value="java265爱好者"> <entry key="two" value-ref="man1"> <entry key="three" value-ref="man2"> </entry></entry></entry></map> </property> </bean> </beans>
注入null和空字符串的值
Spring 会把属性的空参数直接当成空字符串来处理当我们需传入一个null值,需采用以下写法
<bean class="exampleBean" id="...">
<property name="email" value="">
</property></bean>
等同于以下set代码
exampleBean.setEmail("")
当需放入NULL到属性值上时,此时<null>元素用于传入Null值
<bean class="exampleBean" id="...">
<property name="email"><null></null></property>
</bean>
等同于以下set代码
exampleBean.setEmail(null)
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


