Java spring注入多种集合类型

这篇博客详细介绍了如何在Java Spring框架中对多种集合类型进行依赖注入,包括Department和Employee类的配置,以及在applicationContext.xml中的设置。通过Test.java的执行,展示了注入效果和结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Department.java

package com;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Department {
	private String name;
	private String[] empName;
	private List<Employee> empList; // List集合
	private Set<Employee> empSets; // Set集合
	private Map<String, Employee> empMap; // map集合
	private Properties pp; // Properties的使用

	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;
	}

	public List<Employee> getEmpList() {
		return empList;
	}

	public void setEmpList(List<Employee> empList) {
		this.empList = empList;
	}

	public Set<Employee> getEmpSets() {
		return empSets;
	}

	public void setEmpSets(Set<Employee> empSets) {
		this.empSets = empSets;
	}

	public Map<String, Employee> getEmpMap() {
		return empMap;
	}

	public void setEmpMap(Map<String, Employee> empMap) {
		this.empMap = empMap;
	}

	public Properties getPp() {
		return pp;
	}

	public void setPp(Properties pp) {
		this.pp = pp;
	}

	@Override
	public String toString() {
		return "Department [name=" + name + ", empName="
				+ Arrays.toString(empName) + ", empList=" + empList
				+ ", empSets=" + empSets + ", empMap=" + empMap + ", pp=" + pp
				+ "]";
	}
	
}

Employee.java

package com;

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

	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;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + "]";
	}
	
}

applicationContext.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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

	<bean id="department" class="com.Department">
		<!-- 单变量注入 -->
		<property name="name" value="money"></property>
		<!-- 字符串数组注入 -->
		<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="empMap">
			<map>
				<entry key="1" value-ref="emp1"></entry>
				<entry key="2" value-ref="emp2"></entry>
			</map>
		</property>
		
		<property name="pp">
			<props>
				<prop key="pp1">hello</prop>
				<prop key="pp2">world</prop>
			</props>
		</property>
	</bean>
	
	<bean id="emp1" class="com.Employee">
		<property name="id" value="1"></property>
		<property name="name">
			<value>小李</value>
		</property>
	</bean>
	<bean id="emp2" class="com.Employee">
		<property name="id" value="2"></property>
		<property name="name">
			<value>小张</value>
		</property>
	</bean>
</beans>

Test.java

package cn.com;

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

import com.Department;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");

		Department d=(Department) ac.getBean("department");
		
		System.out.println(d);
	}

}

执行结果:

[INFO] 2018-08-03 11:55:46 Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5b7ee8b8: startup date [Fri Aug 03 11:55:46 CST 2018]; root of context hierarchy
[INFO] 2018-08-03 11:55:46 Loading XML bean definitions from class path resource [applicationContext.xml]
Department [name=money, empName=[小明, 小张, 小鸿], empList=[Employee [id=1, name=小李], Employee [id=2, name=小张]], empSets=[Employee [id=1, name=小李], Employee [id=2, name=小张]], empMap={1=Employee [id=1, name=小李], 2=Employee [id=2, name=小张]}, pp={pp2=world, pp1=hello}]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值