spring-向collection注值

beans.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"
		xmlns:context="http://www.springframework.org/schema/context"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


<bean id="department" class="com.collection.Department">
	<property name="name" value="财务部"/>
	<!-- 给数组注值 -->
	<property name="empName">
		<list>
			<value>小明</value>
			<value>大明</value>
			<value>大大明</value>
		</list>
	</property>
	<!-- 给List注值(有序) -->
	<property name="emplist">
		<list>
			<ref bean="emp1"/>
			<ref bean="emp2"/>
		</list>
	</property>
	
	<!-- 给Set注值 -->
	<property name="empsets">
		<set>
			<ref bean="emp1"/>
			<ref bean="emp2"/>
		</set>
	</property>
	
	<!-- 给Map注值 -->
	<property name="empMaps">
		<map>
			<entry key="1" value-ref="emp1"/>
			<entry key="2" value-ref="emp2"/>
		</map>
	</property>
</bean>



<!-- 给list注入值 -->
<bean id="emp1" class="com.collection.Employee">
	<property name="name" value="北京"/>
	<property name="id" value="1"/>
</bean>
<bean id="emp2" class="com.collection.Employee">
	<property name="name" value="天津"/>
	<property name="id" value="2"/>
</bean>







</beans>
Department.java:
package com.collection;

import java.util.*;

public class Department {
	private String name;
	private String []empName;
	private List<Employee> emplist;
	private Set<Employee> empsets;
	private Map<String,Employee> empMaps;
	
	
	public Map<String, Employee> getEmpMaps() {
		return empMaps;
	}
	public void setEmpMaps(Map<String, Employee> empMaps) {
		this.empMaps = empMaps;
	}
	public Set<Employee> getEmpsets() {
		return empsets;
	}
	public void setEmpsets(Set<Employee> empsets) {
		this.empsets = empsets;
	}
	public List<Employee> getEmplist() {
		return emplist;
	}
	public void setEmplist(List<Employee> emplist) {
		this.emplist = emplist;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String[] getEmpName() {
		return empName;
	}
	public void setEmpName(String[] empName) {
		this.empName = empName;
	}
}
Employee.java:
package com.collection;

public class Employee {
	private String name;
	private int id;

	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
test.java:
package com.collection;

import java.util.Map.Entry;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationContext ac=new ClassPathXmlApplicationContext("com/collection/beans.xml");
		Department a=(Department) ac.getBean("department");
		String []empName=a.getEmpName();
		for(int q=0;q<empName.length;q++){
			System.out.println(empName[q]);
		}
		System.out.println("*****************************************");
		for(Employee e:a.getEmplist()){
			System.out.println(e.getName());
		}
		System.out.println("*****************************************");
		for(Employee e:a.getEmpsets() 	){
			System.out.println(e.getName());
		}
		System.out.println("*****************************************");
		for(Entry<String,Employee> e:a.getEmpMaps().entrySet()	){
			System.out.println(e.getKey()+" "+e.getValue().getName());
		}
	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值