Notice
Recent Posts
Recent Comments
Link
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

DI_9_collection : CollectionBean.JAVA/CollectionBeanClient.JAVA/applicationContext9.xml 본문

SPRING/chapter02_DI

DI_9_collection : CollectionBean.JAVA/CollectionBeanClient.JAVA/applicationContext9.xml

GAWON 2023. 6. 26. 19:11
package org.joonzis.DI_9_collection;

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

public class CollectionBean {
	//필드
	private List<String> addressList;
	private Set<String> addressSet;
	private Map<String , String> addressMap;
	
	//메소드
	public List<String> getAddressList() {
		return addressList;
	}
	public void setAddressList(List<String> addressList) {
		this.addressList = addressList;
	}
	public Set<String> getAddressSet() {
		return addressSet;
	}
	public void setAddressSet(Set<String> addressSet) {
		this.addressSet = addressSet;
	}
	public Map<String, String> getAddressMap() {
		return addressMap;
	}
	public void setAddressMap(Map<String, String> addressMap) {
		this.addressMap = addressMap;
	}

}
package org.joonzis.DI_9_collection;


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

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class CollectionBeanClient {
	public static void main(String[] args) {
		
		AbstractApplicationContext ctx = 
				   new GenericXmlApplicationContext("applicationContext9.xml");
		
		//List 객체 가져오기
		CollectionBean bean1 = ctx.getBean("cBean1", CollectionBean.class);
		List<String> list = bean1.getAddressList();
		for(String address : list) {
			System.out.println(address);
		}
		
		System.out.println("--------------");
		
		
		//Set 객체 가져오기
		CollectionBean bean2 = ctx.getBean("cBean2", CollectionBean.class);
		Set<String> set = bean2.getAddressSet();
		Iterator<String> itr = set.iterator();
		while(itr.hasNext()) {
			System.out.println(itr.next());
		}
		
		
		System.out.println("--------------");
		
		//Map 객체 가져오기
		CollectionBean bean3 = ctx.getBean("cBean3", CollectionBean.class);
		Map<String , String> map = bean3.getAddressMap();
		for(String mapKey : map.keySet()) {
			System.out.println(mapKey + "" + map.get(mapKey));
		}
		
		ctx.close();
	}
}
<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
	
	<bean id="cBean1" class="org.joonzis.DI_9_collection.CollectionBean">
		<property name="addressList">
			<list>
				<value>서울시 구로구 구로동</value>
				<value>서울시 금천구 독산동</value>
			</list>
		</property>
	</bean>
	
	<bean id="cBean2" class="org.joonzis.DI_9_collection.CollectionBean">
		<property name="addressSet">
			<set value-type="java.lang.String">
				<value>서울시 구로구 구로동</value>
				<value>서울시 금천구 독산동</value>
				<value>서울시 금천구 독산동</value>
			</set>
		</property>
	</bean>
	
	<bean id="cBean3" class="org.joonzis.DI_9_collection.CollectionBean">
		<property name="addressMap">
			<map>
				<entry>
					<key><value>조재형</value></key>
					<value>서울시 금천구 독산동</value>
				</entry>
				<entry>
					<key><value>장동완</value></key>
					<value>시흥시 신천동</value>
				</entry>
			</map>
		</property>
	</bean>
	
</beans>