在Spring中注入List,Map集合

使用xml的方式进行Spring配置,对于内部元素为String的List和Map属性的注入一般为如下方式:

如果内部元素为Bean,则将value替换为value-ref或元素即可

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

	
<!-- 	Author中List和Map注入 -->
	<bean class="com.rjs.entity.Author" id="authorTwo">
		<property name="id" value="1"></property>
		<property name="name" value="饶吉盛"></property>
		<property name="listBlog">
			<list>
				<ref bean="blog1"></ref>
				<ref bean="blog2"></ref>
			</list>
		</property>
		<property name="map">
			<map>
				<entry key="1" value-ref="blog3"></entry>
				<entry key="1" value-ref="blog4"></entry>
			</map>
		</property>
		
	</bean>
	<bean class="com.rjs.entity.Blog" id="blog1"  >
		<!-- 属性赋值 ,在bean类中有setter -->
		<property name="id" value="1"></property>
		<property name="title" value="美国德特里克堡生物实验室"></property>
		<property name="content" value="美国德特里克堡生物实验室....."></property>
	</bean>
	<bean class="com.rjs.entity.Blog" id="blog2"  >
		<!-- 属性赋值 ,在bean类中有setter -->
		<property name="id" value="1"></property>
		<property name="title" value="美国德特里克堡生物实验室"></property>
		<property name="content" value="美国德特里克堡生物实验室....."></property>
	</bean>
	<bean class="com.rjs.entity.Blog" id="blog3" >
		<!-- 属性赋值 ,在bean类中有setter -->
		<property name="id" value="1"></property>
		<property name="title" value="美国德特里克堡生物实验室"></property>
		<property name="content" value="美国德特里克堡生物实验室....."></property>
	</bean><bean class="com.rjs.entity.Blog" id="blog4"   >
		<!-- 属性赋值 ,在bean类中有setter -->
		<property name="id" value="1"></property>
		<property name="title" value="美国德特里克堡生物实验室"></property>
		<property name="content" value="美国德特里克堡生物实验室....."></property>
	</bean>


</beans>

实体类Blog.java

/*
 * 以前Blog blog=new Blog()
 * 现在不是,而是找spring拿,application.xml里
 */
public class Blog {
	private int id;
	private String title;
	private String content;
	private Author author;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		System.out.println("执行了setid方法....");
		this.id = id;
	}

	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	
	public Author getAuthor() {
		return author;
	}
	public void setAuthor(Author author) {
		this.author = author;
	}
	
	public Blog(int id, String title, String content, Author author) {
		super();
		this.id = id;
		this.title = title;
		this.content = content;
		this.author = author;
	}
	public Blog() {
		super();
		System.out.println("执行构造方法...");
		// TODO Auto-generated constructor stub
	}
	

}

Author.java

package com.rjs.entity;

import java.util.List;
import java.util.Map;

public class Author {
	private int id;
	private String name;
	//这里注入一个Blog集合List
	private List<Blog> listBlog;
	private Map<Integer,Blog> map;
	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;
	}
	
	public Map<Integer, Blog> getMap() {
		return map;
	}
	public void setMap(Map<Integer, Blog> map) {
		this.map = map;
	}
	

	public List<Blog> getListBlog() {
		return listBlog;
	}
	public void setListBlog(List<Blog> listBlog) {
		this.listBlog = listBlog;
	}
	
	public Author(int id, String name, List<Blog> listBlog, Map<Integer, Blog> map) {
		super();
		this.id = id;
		this.name = name;
		this.listBlog = listBlog;
		this.map = map;
	}
	public Author() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	

}

测试类:BeanTestDI.java

public class BeanTestDI {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//F4(接口实现类)
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext2.xml");
		//Blog blog=(Blog) context.getBean("blog");
		Author author=(Author) context.getBean("authorTwo");
		//调用方法
		System.out.println("author===="+author.getId());
		System.out.println(author.getName());
		List<Blog> listBlog=author.getListBlog();
		Map<Integer,Blog> map=author.getMap();
		Iterator it=map.keySet().iterator();
		for(Blog blog:listBlog) {
			System.out.println("list===["+blog.getId()+"  "+blog.getTitle()+"  "+blog.getContent()+"]");
		}
		while(it.hasNext()){
			Integer key=(Integer) it.next();
			Blog blog=map.get(key);
			System.out.println("map====["+blog.getId()+","+blog.getTitle()+"]");
		}

		
	}

}

结果:

猜你喜欢

转载自blog.csdn.net/qingxiao__123456789/article/details/112920159
今日推荐