spring 注入集合

List集合

1.创建ListTestBean类:

package com.spring.pojo;
import java.util.List;
//注入List类型
public class ListTestBean {
	private List<String> values;
	public List<String> getValues() {
		return values;
	}
	public void setValues(List<String> values) {
		this.values = values;
	}
	@Override
	public String toString() {
		return "ListTestBean [values=" + values + "]";
	}
}

2.创建配置spring-listInject.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="listBean" class="com.spring.pojo.ListTestBean">
<!-- List类型需要<list>标签来配置注入  -->
<property name="values">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
</bean>
</beans>

3.创建ListInjectTest测试类:

package com.spring.test;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.pojo.ListTestBean;
@SuppressWarnings("deprecation")
public class ListInjectTest {
@Test
public void testListInject(){
	@SuppressWarnings("resource")
	ApplicationContext act=new ClassPathXmlApplicationContext("spring-listInject.xml");
	ListTestBean listBean=act.getBean(ListTestBean.class);
    System.out.println(listBean.toString());
    System.out.println(listBean.getValues().size());
    Assert.assertEquals(3, listBean.getValues().size());
}
}

 4.测试结果:

ListTestBean [values=[1, 2, 3]]
3

 Set集合

1.创建CollectionTestBean类:

package com.spring.pojo;
import java.util.Collection;
//Set类型
public class CollectionTestBean {
private Collection<String> values;
public Collection<String> getValues() {
	return values;
}
public void setValues(Collection<String> values) {
	this.values = values;
}
@Override
public String toString() {
	return "CollectionTestBean [values=" + values + "]";
}
}

2.创建配置spring-setInject.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">
<!-- Set类型 需要<set>标签来配置注入 -->
<bean id="setBean" class="com.spring.pojo.CollectionTestBean">
<property name="values">
<set>
<value>1</value>
<value>2</value>
<value>3</value>
</set>
</property>
</bean>
</beans>

 3.创建SetInject测试类:

package com.spring.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.pojo.CollectionTestBean;
public class SetInject {
	@Test
	public void setInject(){
		ApplicationContext act=new ClassPathXmlApplicationContext("spring-SetInject.xml");
		CollectionTestBean setInject=	act.getBean("setBean",CollectionTestBean.class);
	 System.out.println(setInject.toString());
	 System.out.println(setInject.getValues().size());
	}
}

 4.输出结果:

CollectionTestBean [values=[1, 2, 3]]
3

因为Collection类型是Set和List类型的基类型,所以使用<set>或<list>标签都可以进行注入,配置方式完全和以上配置方式一样,只是将测试类属性改成“Collection”类型

 

你可能感兴趣的:(spring)