您的位置:首页 > 编程语言 > Java开发

Spring之集合DI

2016-05-02 19:57 531 查看
如果一个类的成员变量为集合类型,而且这些集合需要外部提供,则需要进行DI处理,但是,集合的DI,只能通过setter注入,所以,意味着必须有默认构造函数或者存在构造函数DI。四大集合的代码如下:

package com.bean.DI2;

import java.util.List;

public class ListDI {
private List<Integer> list;
public ListDI() {
// TODO Auto-generated constructor stub
}
public void setList(List<Integer> list) {
this.list = list;
}

public void print(){
System.out.println("list di:");
for(Integer i:list){
System.out.println(i);
}
}
}
package com.bean.DI2;

import java.util.Set;

public class SetDI {
private Set<Integer> set;
public SetDI() {
// TODO Auto-generated constructor stub
}
public void setSet(Set<Integer> set) {
this.set = set;
}
public void print(){
System.out.println("set di:");
for(Integer i:set){
System.out.println(i);
}
}
}

package com.bean.DI2;

import java.util.Map;

public class MapDI {
private Map<String, String> map;

public MapDI() {
// TODO Auto-generated constructor stub
}

public void setMap(Map<String, String> map) {
this.map = map;
}

public void print(){
System.out.println("map di");
for(String key:map.keySet()){
System.out.println("key="+key+",value="+map.get(key));
}
}
}

package com.bean.DI2;

import java.util.Properties;

public class PropertyDI {
private Properties properties;
public PropertyDI() {
// TODO Auto-generated constructor stub
}
public void setProperties(Properties properties) {
this.properties = properties;
}

public void print(){
System.out.println("property di:");
System.out.println(properties);
}
}

package com.bean.DI2;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Main {
public static void main(String[] args) {

FileSystemXmlApplicationContext context2=new FileSystemXmlApplicationContext("BeanXml\\DI3.xml");
SetDI set=(SetDI)context2.getBean("setdi");
set.print();
ListDI listDI=(ListDI)context2.getBean("listdi");
listDI.print();
MapDI mapDI=(MapDI)context2.getBean("mapdi");
mapDI.print();
PropertyDI propertyDI=(PropertyDI)context2.getBean("propertydi");
propertyDI.print();
context2.close();
}
}
<?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="setdi" class="com.bean.DI2.SetDI">
<property name="set">
<set>
<value>15</value>
<value>16</value>
<value>17</value>
</set>
</property>
</bean>

<bean id="listdi" class="com.bean.DI2.ListDI">
<property name="list">
<list>
<value>5</value>
<value>6</value>
<value>7</value>
</list>
</property>
</bean>

<bean id="mapdi" class="com.bean.DI2.MapDI">
<property name="map">
<map>
<entry key="one" value="1"></entry>
<entry key="two" value="2"></entry>
<entry key="three" value="3"></entry>
</map>
</property>
</bean>

<bean id="propertydi" class="com.bean.DI2.PropertyDI">
<property name="properties">
<props>
<prop key="4">four</prop>
<prop key="5">five</prop>
<prop key="6">six</prop>
</props>
</property>
</bean>
</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: