Spring配置Bean的List Map Set Properties

1. Student实体类

package org.spring;

public class Student {
	
	private String name;
	
	public void setName(String name) {
		this.name = name;
	}

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

2. Teacher实体类

package org.spring;

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

public class Teacher {
	
	private String name;
	
	private List list;
	
	private Set<Student> set;
	
	private Map<String,Student> map;
	
	private Student[] array;
	
	private Properties properties;
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setList(List list) {
		this.list = list;
	}
	
	public void setSet(Set<Student> set) {
		this.set = set;
	}
	
	public void setMap(Map<String, Student> map) {
		this.map = map;
	}
	
	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	public String getName() {
		return name;
	}

	public List getList() {
		return list;
	}

	public Set<Student> getSet() {
		return set;
	}

	public Map<String, Student> getMap() {
		return map;
	}

	public Properties getProperties() {
		return properties;
	}

	public void setArray(Student[] array) {
		this.array = array;
	}
	
	public Student[] getArray() {
		return array;
	}
}

3. 配置Bean

<?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:util="http://www.springframework.org/schema/util"
	   xmlns:p="http://www.springframework.org/schema/p"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	<beans>
	
		<!--Utils命名空间声明可以被引用的List,通过id引用-->
		<util:list id="StudentList">
			<bean class="org.spring.Student">
				<property name="name" value="list1"></property>
			</bean>
			<ref bean="student"/>
		</util:list>
	
		<bean id="student" class="org.spring.Student">
			<property name="name" value="小明同学"></property>
		</bean>
		
		<bean id="teacher" class="org.spring.Teacher">
			<property name="name" value="老师"></property>
			
			<!-- 配置List -->
			<property name="list">
				<list>
					<ref bean="student"/>
					<bean class="org.spring.Student">
						<property name="name"><value>小红同学</value></property>
					</bean>
				</list>
			</property>
			
			<!-- 配置Set -->
			<property name="set">
				<set>
					<ref bean="student"/>
					<bean class="org.spring.Student">
						<property name="name" value="小黑同学"></property>
					</bean>
				</set>
			</property>
			
			<!-- 配置Map -->
			<property name="map">
				<map>
					<entry key="key01" value-ref="student"></entry>
					<entry key="key02">
						<bean class="org.spring.Student">
							<property name="name" value="小晓同学"></property>
						</bean>
					</entry>
				</map>
			</property>
			
			<!-- 配置 properties -->
			<property name="properties">
				<props>
					<prop key="user">root</prop>
					<prop key="passwd">1234</prop>
					<prop key="url"> jdbc:mysql://test//</prop>
					<prop key="driver">com.jdbc.mysql.driver</prop>
				</props>
			</property>
			
			<!-- 配置引用数组 -->
			<property name="array">
				<array>
					<ref bean="student"/>
					<bean class="org.spring.Student">
						<property name="name" value="张三同学"></property>
					</bean>
				</array>
			</property>
			
		</bean>
	</beans>
	
</beans>

4、获取TeacherBean中配置的Map List Set Properties属性

package org.spring;

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

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

public class Main {

	public static void main(String[] args) {
		
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		Student student = (Student)applicationContext.getBean("student");
		System.out.println(student);
		
		Teacher teacher = (Teacher)applicationContext.getBean("teacher");
		List<Student> list = teacher.getList();
		System.out.println(list);
		
		Set<Student> set = teacher.getSet();
		System.out.println(set);
		
		Map<String,Student> map = teacher.getMap();
		System.out.println(map);
		
		Properties properties = teacher.getProperties();
		System.out.println(properties);
		
		Student[] array = teacher.getArray();
		System.out.println(array);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值