Spring的集合配置标签如下表所示
list | 用于注入list类型的值,允许重复 |
---|---|
set | 用于注入set类型的值,不允许重复 |
map | 用于注入key-value的值,其中key-value可以为任何类型 |
props | 用于注入 key-value 的集合,其中 key-value 都是字符串类型 |
示例
JavaCollection类代码如下。
package com.wen.pojo;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class JavaCollection {
List manList;
Set manSet;
Map manMap;
Properties manProp;
public void getManList() {
System.out.println(manList);
}
public void setManList(List manList) {
this.manList = manList;
}
public void getManSet() {
System.out.println(manSet);
}
public void setManSet(Set manSet) {
this.manSet = manSet;
}
public void getManMap() {
System.out.println(manMap);
}
public void setManMap(Map manMap) {
this.manMap = manMap;
}
public void getManProp() {
System.out.println(manProp);
}
public void setManProp(Properties manProp) {
this.manProp = manProp;
}
}
beans2.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.xsd">
<bean id="javaCollection" class="com.wen.pojo.JavaCollection">
<property name="manList">
<!--允许重复值-->
<list>
<value>百度</value>
<value>谷歌</value>
<value>搜狗</value>
<value>搜狗</value>
</list>
</property>
<property name="manSet">
<!--不允许重复值-->
<set>
<value>百度</value>
<value>谷歌</value>
<value>搜狗</value>
<value>搜狗</value>
</set>
</property>
<property name="manMap">
<map>
<entry key="1" value="百度"/>
<entry key="2" value="搜狗"/>
<entry key="3" value="谷歌"/>
</map>
</property>
<property name="manProp">
<props>
<prop key="1">百度</prop>
<prop key="2">搜狗</prop>
<prop key="3">谷歌</prop>
</props>
</property>
</bean>
</beans>
MyTest类代码如下。
import com.wen.pojo.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
JavaCollection javaCollection = (JavaCollection)context.getBean("javaCollection");
javaCollection.getManList();
javaCollection.getManSet();
javaCollection.getManMap();
javaCollection.getManProp();
}
}
运行结果如下。
D:\Application\jdk\jdk14\bin\java.exe "-javaagent:D:\Application\JetBrains\IntelliJ IDEA 2020.1.3\lib\idea_rt.jar=61460:D:\Application\JetBrains\IntelliJ IDEA 2020.1.3\bin" -Dfile.encoding=UTF-8 -classpath D:\program\spring-02\target\test-classes;D:\program\spring-02\target\classes;C:\Users\27207\.m2\repository\org\springframework\spring-webmvc\5.2.0.RELEASE\spring-webmvc-5.2.0.RELEASE.jar;C:\Users\27207\.m2\repository\org\springframework\spring-aop\5.2.0.RELEASE\spring-aop-5.2.0.RELEASE.jar;C:\Users\27207\.m2\repository\org\springframework\spring-beans\5.2.0.RELEASE\spring-beans-5.2.0.RELEASE.jar;C:\Users\27207\.m2\repository\org\springframework\spring-context\5.2.0.RELEASE\spring-context-5.2.0.RELEASE.jar;C:\Users\27207\.m2\repository\org\springframework\spring-core\5.2.0.RELEASE\spring-core-5.2.0.RELEASE.jar;C:\Users\27207\.m2\repository\org\springframework\spring-jcl\5.2.0.RELEASE\spring-jcl-5.2.0.RELEASE.jar;C:\Users\27207\.m2\repository\org\springframework\spring-expression\5.2.0.RELEASE\spring-expression-5.2.0.RELEASE.jar;C:\Users\27207\.m2\repository\org\springframework\spring-web\5.2.0.RELEASE\spring-web-5.2.0.RELEASE.jar MyTest
[百度, 谷歌, 搜狗, 搜狗]
[百度, 谷歌, 搜狗]
{1=百度, 2=搜狗, 3=谷歌}
{1=百度, 2=搜狗, 3=谷歌}
Process finished with exit code 0
注入null和空字符串的值
如果需要传递 NULL 值, 元素用来处理 Null 值。
<bean id = "..." class = "exampleBean">
<property name = "email"><null/></property>
</bean>
如果需要传递一个空字符串值。
<bean id = "..." class = "exampleBean">
<property name = "email" value = ""/>
</bean>