Inject collections into beans

Inject collections into beans

 

Use the value attribute and use the ref attribute of the <property> tag to refer to the object in your bean configuration file, both cases can handle single value to a bean, if you want to pass multiple values, such as Java Collection types List, Set , Map and Properties. To handle this situation, Spring provides four types of configuration elements as follows:



 

 

When you need to inject elements into the collection elements in the bean, you can use the following methods, one is constructor injection, the other is setter injection

import java.util.List;
import java.util.Map;
import java.util.Set;
//A normal JavaBean
public class Person {
	//person's name
	private String name;
	//A person can sing N songs, the song name cannot be repeated
	private Set<String> set;
	//A person's age corresponds to which period of a student's life
	private Map<Integer,String> map;
	//The meal that a person eats in the morning, noon and evening (the value can be repeated)
	private List<String> meal;
	//getter method
	public String getName() {
		return name;
	}
	//setter method
	public void setName(String name) {
		this.name = name;
	}
	//getter method
	public Set<String> getSet() {
		return set;
	}
	//setter method
	public void setSet(Set<String> set) {
		this.set = set;
	}
	//getter method
	public Map<Integer, String> getMap() {
		return map;
	}
	//setter method
	public void setMap(Map<Integer, String> map) {
		this.map = map;
	}
	//getter method
	public List<String> getMeal() {
		return meal;
	}
	//setter method
	public void setMeal(List<String> meal) {
		this.meal = meal;
	}
	// a constructor
	public Person(String S_name, Set<String> set, Map<Integer, String> map,
			List<String> meal) {
		super();
		this.name = S_name;
		this.set = set;
		this.map = map;
		this.meal = meal;
	}
	//default constructor
	public Person() {
		super();
	}
}

 

You can set the collection when configuring the bean in 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.0.xsd">
	<bean id="person" class="com.pp.Person">
	<!-- Pass a value to the first parameter of the constructor -->
	<constructor-arg value="pptest"></constructor-arg>
	<!-- Pass a value to the second parameter of the constructor -->
	<constructor-arg>
	<set>
		<value>I am Chinese</value>
		<value>哈哈</value>
		<!-- Due to the nature of the set, this cannot be added -->
		<value>哈哈</value>
	</set>
	</constructor-arg>
	<!-- Pass a value to the third parameter of the constructor -->
	<constructor-arg>
	<map>
		<entry key="5" value="小学"></entry>
		<entry key="11" value="初中"></entry>
		<entry key="14" value="高中"></entry>
		<entry key="17" value="大学"></entry>
	</map>
	</constructor-arg>
	<!-- Pass the fourth parameter to the constructor -->
	<constructor-arg>
	<list>
		<value>粥</value>
		<value>粥</value>
		<value>粥</value>
	</list>
	</constructor-arg>
	</bean>
</beans>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326535934&siteId=291194637