一、说明
传递类似于Java Collection类型的值,使用如下标签:
标签 | 说明 |
---|---|
注入list类型的值,允许重复 | |
注入set类型的值,不允许重复 | |
key-value集合,key-value可以是任意类型 | |
key-value集合,key-value是字符串类型 |
二、实例
2.1、四种类型Collection注入
public class JavaCollection {
List list;
Set set;
Map map;
Properties prop;
public List getList() {
System.out.println("List Elements:"+list);
return list;
}
public void setList(List list) {
this.list = list;
}
public Set getSet() {
System.out.println("Set Elements:"+set);
return set;
}
public void setSet(Set set) {
this.set = set;
}
public Map getMap() {
System.out.println("Map Elements:"+map);
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Properties getProp() {
System.out.println("Properties Elements:"+prop);
return prop;
}
public void setProp(Properties prop) {
this.prop = prop;
}
}
<?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.jsonliu.bean.JavaCollection">
<property name="list">
<list>
<value>金苹果</value>
<value>银苹果</value>
<value>铜苹果</value>
<value>铜苹果</value>
</list>
</property>
<property name="set">
<set>
<value>金苹果</value>
<value>银苹果</value>
<value>铜苹果</value>
<value>铜苹果</value>
</set>
</property>
<property name="map">
<map>
<entry key="1" value="金苹果"/>
<entry key="2" value="银苹果"/>
<entry key="3" value="铜苹果"/>
<entry key="4" value="铜苹果"/>
</map>
</property>
<property name="prop">
<props>
<prop key="one">金苹果</prop>
<prop key="two">银苹果</prop>
<prop key="three">铜苹果</prop>
<prop key="four">铜苹果</prop>
</props>
</property>
</bean>
</beans>
public class MainApp2 {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("Beans2.xml");
JavaCollection javaCollection = (JavaCollection) context.getBean("javaCollection");
javaCollection.getList();
javaCollection.getSet();
javaCollection.getMap();
javaCollection.getProp();
}
}
运行结果:
2.2、注入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="man1" class="com.jsonliu.bean.Man"/>
<bean id="man2" class="com.jsonliu.bean.Man"/>
<bean id="javaCollection" class="com.jsonliu.bean.JavaCollection">
<property name="list">
<list>
<ref bean="man1" />
<ref bean="man2" />
<value>铜苹果</value>
</list>
</property>
<property name="set">
<set>
<ref bean="man1" />
<ref bean="man2" />
<value>铜苹果</value>
</set>
</property>
<property name="map">
<map>
<entry key="1" value-ref="man1"/>
<entry key="2" value-ref="man2"/>
<entry key="3" value="铜苹果"/>
</map>
</property>
</bean>
</beans>
2.3、注入null和空字符串值
<?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="man" class="com.jsonliu.bean.Man">
<property name="age"><null/></property>
<property name="name" value=""/>
</bean>
</beans>