본문 바로가기
Spring

230130 Spring bean list와 map

List나 map으로 주입하는 것도 가능하다.
마찬가지로 사용할 일은 없을 것.
외부파일이 이런 형식일 때도 있다. 해석하는 방법을 알아야 함.
외우기보단 차라리 사용할 때 구글링하는 방식을 추천.

 

 

문자열일 경우 value라는 태그 사용.
map은 (키=값)을 묶어 Entry라 한다.


collection-context.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- list of map의 의존성 주입 -->
	<bean id="customer" class="ex03.Customer">
		<!-- list안에 문자열 -->
		<property name="lists">
			<list>
				<value>홍길동</value>
				<value>이순신</value>
				<value>홍길자</value>
			</list>
		</property>

		<!-- list안에 object -->
		<property name="persons">
			<list>
				<bean class="ex03.Person">
					<property name="name" value="홍길동" />
					<property name="id" value="aaa123" />
				</bean>
				<bean class="ex03.Person">
					<property name="name" value="이순신" />
					<property name="id" value="bbb123" />
				</bean>
				<bean class="ex03.Person">
					<property name="name" value="홍길자" />
					<property name="id" value="ccc123" />
				</bean>
			</list>
		</property>

		<!-- map안에 object -->
		<property name="maps">
			<map>
				<entry key="k1" value="문자열"/>
				<entry key="k2">
					<bean class="ex03.Person">
						<property name="name" value="홍길동" />
						<property name="id" value="aaa123" />
					</bean>
				</entry>
			</map>
		</property>
	</bean>
</beans>

List방식

lists, persons.

persons는 list 안에 Object.

 

Customer.java

package ex03;

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

public class Customer {
	
	//list or map의 의존성 주입
	private List<String> lists;
	private List<Person> Persons;
	private Map<String, Object> maps; 
	
	//getter, setter
	public List<String> getLists() {
		return lists;
	}

	public void setLists(List<String> lists) {
		this.lists = lists;
	}

	public List<Person> getPersons() {
		return Persons;
	}

	public void setPersons(List<Person> persons) {
		Persons = persons;
	}

	public Map<String, Object> getMaps() {
		return maps;
	}

	public void setMaps(Map<String, Object> maps) {
		this.maps = maps;
	}
}

 

 

Person.java

package ex03;

public class Person {

	private String name;
	private String id;
	
	//getter setter
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	
}

 


Map방식

위의 Customer.java의 maps.

private Map<String, Object> maps; 

public Map<String, Object> getMaps() {
		return maps;
	}

	public void setMaps(Map<String, Object> maps) {
		this.maps = maps;
	}

부분.

 

 

 

MainClass.java

package ex03;

import java.util.Map;

import org.springframework.context.support.GenericXmlApplicationContext;

public class MainClass {
	public static void main(String[] args) {
		
		GenericXmlApplicationContext ctx = 
				new GenericXmlApplicationContext("collection-context.xml");
		
		Customer customer=ctx.getBean("customer",Customer.class);
		Map<String, Object> maps=customer.getMaps();
		System.out.println(maps.toString());
		
		
		
	}
}

 

 

maps 콘솔출력문

1월 30, 2023 8:03:17 오후 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [collection-context.xml]
1월 30, 2023 8:03:17 오후 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericXmlApplicationContext@402a079c: startup date [Mon Jan 30 20:03:17 KST 2023]; root of context hierarchy
{k1=문자열, k2=ex03.Person@306f16f3}