spring学习之集合属性注入

spring中对象之间的相互依赖关系是通过applicationContext.xml来管理的,因此集合属性注入的重点也放在applicationContext.xml的配置之中。

下面是一个完整的各种集合属性注入的例子

目录结构:

技术分享

实体类

public class Students {

	private String sid;
	private String sname;
	private int age;
	
	public Students() {
		super();
	}
	public Students(String sid, String sname, int age) {
		super();
		this.sid = sid;
		this.sname = sname;
		this.age = age;
	}
	public String getSid() {
		return sid;
	}
	public void setSid(String sid) {
		this.sid = sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}	
}

spring提倡面向接口的编程,因此抽象出对students类的常用操作的方法。

public interface StudentsDAO {

	//保存学生
	public boolean saveStudents(Students s);
}

public class StudentsDAOImpl implements StudentsDAO {

	@Override
	public boolean saveStudents(Students s) {
		if(s!=null)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

}
service层

public class StudentsService {

	//name对应applicationContext中bean的name
	//@Resource(name="studentsDAO")
	//按照类型来匹配,因为它们的类型不同,
	@Autowired
	@Qualifier("studentsDAO")
	private StudentsDAO sDAO;
	//@Resource(name="students")
	@Autowired
	@Qualifier("students")
	private Students s;
	private List<String> citys;
	private List<Students> stus;
	private Set<String> countrys;
	private Map<Integer,String> users;
	
	public List<Students> getStus() {
		return stus;
	}

	public void setStus(List<Students> stus) {
		this.stus = stus;
	}

	public Set<String> getCountrys() {
		return countrys;
	}

	public void setCountrys(Set<String> countrys) {
		this.countrys = countrys;
	}

	public List<String> getCitys() {
		return citys;
	}

	public void setCitys(List<String> citys) {
		this.citys = citys;
	}

	public Map<Integer, String> getUsers() {
		return users;
	}

	public void setUsers(Map<Integer, String> users) {
		this.users = users;
	}

	public StudentsDAO getsDAO() {
		return sDAO;
	}

	public void setsDAO(StudentsDAO sDAO) {
		this.sDAO = sDAO;
	}
	
	public Students getS() {
		return s;
	}

	public void setS(Students s) {
		this.s = s;
	}

	//保存学生
	public boolean saveStudents(){
		if(sDAO.saveStudents(s))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}
applicationContext.xml,配置文件

其中有list<String>、set<String>、Map和list<Students>(它是一个引用类型)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
	">
	<!-- 加入对注解的支持 -->
	<context:annotation-config/>
	<bean name="students" class="com.qzp.model.Students">
		<constructor-arg index="0" type="java.lang.String" value="001"/>
        <constructor-arg index="1" value="qzp"/>
		<constructor-arg index="2" value="25"/>
	</bean>
	<bean name="studentsDAO" class="com.qzp.dao.impl.StudentsDAOImpl"></bean>
	<bean name="studentsService" class="com.qzp.service.StudentsService">
		<property name="s" ref="students"></property>  
        <property name="sDAO" ref="studentsDAO"></property>
        <property name="citys">
          <list>
        	<value>北京</value>
        	<value>南京</value>
        	<value>上海</value>
          </list>
        </property>
        <property name="stus">
          <list>
        	<bean name="stu1" class="com.qzp.model.Students">
        	 <constructor-arg index="0" type="java.lang.String" value="001"/>
        	 <constructor-arg index="1" value="qzp"/>
			 <constructor-arg index="2" value="25"/>
        	</bean>
        	<bean name="stu2" class="com.qzp.model.Students">
        	 <constructor-arg index="0" type="java.lang.String" value="002"/>
        	 <constructor-arg index="1" value="qap"/>
			 <constructor-arg index="2" value="12"/>
        	</bean>
          </list>
        </property>
        <property name="countrys">
          <set>
        	<value>中国</value>
        	<value>英国</value>
        	<value>美国</value>
          </set>
        </property>
        <property name="users">
          <map key-type="java.lang.Integer" value-type="java.lang.String">
           <entry key="0" value="张三"></entry>
           <entry key="1" value="李四"></entry>
           <entry key="2" value="王五"></entry>
          </map>
        </property>
   </bean>
</beans>

最后附上测试代码

public  class TestStudentsService extends TestCase{
	
	public void testSaveStudents(){
		ApplicationContext cxt=new ClassPathXmlApplicationContext("applicationContext.xml");
		StudentsService sService=(StudentsService)cxt.getBean("studentsService");
		List<String> citys=sService.getCitys();
		Set<String> countrys=sService.getCountrys();
		Map<Integer,String> users=sService.getUsers();
		List<Students> stus=sService.getStus();
		for(String city:citys){
			System.out.println(city);
		}
		System.out.println("---------");
		for(String country:countrys){
			System.out.println(country);
		}
		System.out.println("---------");
		//打印map类型
		for(int key:users.keySet()){
			System.out.println("key="+key+"value="+users.get(key));
		}
		System.out.println("---------");
		for(Students stu:stus){
			System.out.println("学号"+stu.getSid()+"姓名"+stu.getSname());
		}
		//使用断言,如果二者相等,通过
		Assert.assertEquals(true, sService.saveStudents());
		
	}
}

测试结果如下:

北京
南京
上海
---------
中国
英国
美国
---------
key=0value=张三
key=1value=李四
key=2value=王五
---------
学号001姓名qzp
学号002姓名qap

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。