Spring框架中集合属性为对象的注入方法

Spring框架中集合属性为对象的注入方法

前言

在集合的属性注入中,如果注入属性为普通类型(Stringint)等,则直接按照《集合属性注入》进行配置即可。

当集合中的值为对象(MapCLASS)时,就需要使用新的注入方式。

创建基础类

首先是建立一个基础类,用作携带集合内容为类的属性值。下面的代码包含了普通属性集合和内容为对象的集合。

package com.action.spring.collectiontype;

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

public class Stud {
    
    
	
	private String[] str;	
	private List<String> lists;	
	private Map<String, String> maps;
	private Set<String> sets;
	private List<Course> courseList;
	
	public void setStr(String[] str) {
    
    
		this.str = str;
	}
	public void setLists(List<String> lists) {
    
    
		this.lists = lists;
	}
	public void setMaps(Map<String, String> maps) {
    
    
		this.maps = maps;
	}
	public void setSets(Set<String> sets) {
    
    
		this.sets = sets;
	}
	
	public void setCourseList(List<Course> courseList) {
    
    
		this.courseList = courseList;
	}
	
	public void collectiontest() {
    
    
		
		System.out.println(Arrays.toString(str));
		System.out.println(lists);
		System.out.println(maps);
		System.out.println(sets);
		System.out.println(courseList);
		
	}

}

其中private List<Course> courseList;即为类属性中List集合中包含内容为Course对象。

创建Course

创建集合List中包含的Course对象的类,并重写toString()方法

package com.action.spring.collectiontype;

public class Course {
    
    
	
	private String cname;

	public void setCname(String cname) {
    
    
		this.cname = cname;
	}

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

}

其中重写toString()方法是为了能够使用println()将对象输出。

编写XML配置文件

其中包含的<!-- 注入外部bean -->为参考写法。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:util="http://www.springframework.org/schema/util"
	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
						http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

	
	<!-- 注入外部bean -->
	<bean id="userService" class="com.action.spring.service.UserService">
		<property name="userDao" ref="userDaoImpl"></property>
	</bean>
	<bean id="userDaoImpl" class="com.action.spring.dao.UserDaoImpl"></bean>
	
	<!-- 集合属性注入 -->
	<bean id="stud" class="com.action.spring.collectiontype.Stud">
		<property name="str">
			<array>
				<value>办公室</value>
				<value>图书馆</value>
			</array>
		</property>
		<property name="lists">
			<list>
				<value>list1</value>
				<value>list2</value>
			</list>
		</property>
		<property name="maps">
			<map>
				<entry key="信息部" value="4楼"></entry>
				<entry key="人资部" value="7楼"></entry>
			</map>
		</property>
		<property name="sets">
			<set>
				<value>sets1</value>
				<value>sets2</value>
			</set>
		</property>
		<property name="courseList">
			<list>
				<ref bean="course1"></ref>
				<ref bean="course2"></ref>
			</list>
		</property>
		
	</bean>
	
	<bean id="course1" class="com.action.spring.collectiontype.Course">
		<property name="cname" value="Spring5学习"></property>
	</bean>
	<bean id="course2" class="com.action.spring.collectiontype.Course">
		<property name="cname" value="MyBatise学习"></property>
	</bean>
		
</beans>

此时需要结合外部注入bean的方式完成类的属性注入。原本的注入方法是用<value>标签:

<property name="lists">
	<list>
		<value>list1</value>
		<value>list2</value>
	</list>
</property>

当集合中内容为对象时,采用<ref>标签映入外部bean

<property name="courseList">
	<list>
		<ref bean="course1"></ref>
		<ref bean="course2"></ref>
	</list>
</property>

然后对应的外部bean创建并注入属性即可:

<bean id="course1" class="com.action.spring.collectiontype.Course">
	<property name="cname" value="Spring5学习"></property>
</bean>
<bean id="course2" class="com.action.spring.collectiontype.Course">
	<property name="cname" value="MyBatise学习"></property>
</bean>

创建测试类

创建一个测试类,用于调用xml配置文件并调用方法。

package testdemo;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.action.spring.Book;
import com.action.spring.Orders;
import com.action.spring.User;
import com.action.spring.collectiontype.Stud;
import com.action.spring.service.UserService;

public class testSpring5 {
    
    
	
	@Test
	public void testAdd() throws Exception {
    
    
		
		ApplicationContext context =
				new FileSystemXmlApplicationContext("src/config/bean1.xml");
		
		Stud stud = context.getBean("stud", Stud.class);
		stud.collectiontest();

	}

}

执行结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/jerrygaoling/article/details/108829308
今日推荐