Spring学习(4)---集合属性的注入(List集合、Set集合和Map集合)

1、创建一个Car类

package com.springdemo;

public class Car {
	private String name;
	private String color;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	@Override
	public String toString() {
		return "Car [name=" + name + ", color=" + color + "]";
	}
	
}

2、创建一个Person类

package com.springdemo;

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

public class Person {
	private int id;
	private String name; 
	private Car car;
	private List cars;
	private List myList;
	private Set mySet;
	private Map maps;
	private Map myMap;
	
	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}
	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 Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
	public List getCars() {
		return cars;
	}
	public void setCars(List cars) {
		this.cars = cars;
	}
	public List getMyList() {
		return myList;
	}
	public void setMyList(List myList) {
		this.myList = myList;
	}
	public Set getMySet() {
		return mySet;
	}
	public void setMySet(Set mySet) {
		this.mySet = mySet;
	}
	public Map getMaps() {
		return maps;
	}
	public void setMaps(Map maps) {
		this.maps = maps;
	}
	public Map getMyMap() {
		return myMap;
	}
	public void setMyMap(Map myMap) {
		this.myMap = myMap;
	}
	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", car=" + car + ", cars=" + cars + ", myList=" + myList
				+ ", mySet=" + mySet + ", maps=" + maps + ", myMap=" + myMap + "]";
	}

}

3、List集合、Set集合和Map集合属性的注入



    
    	
    	
    
    
    
    	
		
		
		
	 
		
    
    
		
			
				
			
		
		
		
	
     
     	
     		11
     		李四
     	
     
     
     
     
     
     	
     		22
     		隔壁老王
     		打酱油
     	
     
     
     
     
     
     	
     		
     		
     	
     
     
     
     
     
     	
     		
     		
     			嘻嘻嘻
     		
     	
     
     
	

    
	
        
 

4、运行结果

Person [id=1, name=张三,

car=Car [name=法拉利, color=红色],

cars=[Car [name=法拉利, color=红色]],

myList=[11, 李四],

mySet=[22, 隔壁老王, 打酱油],

maps={a=Car [name=法拉利, color=红色], b=Car [name=法拉利, color=红色]},

myMap={a=哈哈哈, b=嘻嘻嘻}]

你可能感兴趣的:(spring学习笔记)