[Spring] 의존성 주입(XML - Map객체)

김승현·2022년 1월 11일
0

Map 타입 매핑


MapContext.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-4.3.xsd">

<bean id="mapBeanFactory" class="kr.or.iei.person.model.vo.PersonMapBeanFactory">
	<property name="map">
		<map>
			<entry>
				<key>
					<value>홍길동</value>
				</key>
					<value>경기도 부천</value>
			</entry>
			
			<entry>
				<key>
					<value>김말똥</value>
				</key>
				<value>서울시 양천</value>
			</entry>
			
			<entry>
				<key>
					<value>고길똥</value>
				</key>
				<value>부산시 서면</value>
			</entry>
		</map>
	</property>
</bean>


</beans>

MapServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	AbstractApplicationContext context = new GenericXmlApplicationContext("/personBeanMapContext.xml");
	PersonMapBeanFactory mapBeanFactory = (PersonMapBeanFactory)context.getBean("mapBeanFactory");
		
	HashMap<String, String> map = mapBeanFactory.getMap();
	
	System.out.println(map.get("홍길동"));
	System.out.println(map.get("김말똥"));
	System.out.println(map.get("고길똥"));
		
	}

PersonMapBeanFactory.java

public class PersonMapBeanFactory {

	private HashMap<String, String> map;

	public PersonMapBeanFactory() {
		super();
	}

	public PersonMapBeanFactory(HashMap<String, String> map) {
		super();
		this.map = map;
	}

	public HashMap<String, String> getMap() {
		return map;
	}

	public void setMap(HashMap<String, String> map) {
		this.map = map;
	}

}

profile
개발자로 매일 한 걸음

0개의 댓글