티스토리 뷰

bean 객체는 사용의 편리성을 위해 배열이나 Map, Set 등과 같은 Collection 객체와의 맵핑을 위한 엘리먼트도 존재한다.


<Collection 객체 Mapping을 위한 태그들>

컬렉션 유형 엘리먼트
List, 배열 <list>
Set <set>
Map <map>
Properties <props>

 

각각의 예제를 통해 의존성 주입을 어떤 방식으로 하는지 확인해보자.

 

우선, 각 예제가 공통으로 사용할 CollectionBean.class 이다.

 

◎CollectionBean

package polymorphism;

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

public class CollectionBean {

	private List<String> addressList;
	private Set<String> addressSet;
	private Map<Integer, String> addressMap;
	private Properties addressProps;
	
	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<Integer, String> getAddressMap() {
		return addressMap;
	}

	public void setAddressMap(Map<Integer, String> addressMap) {
		this.addressMap = addressMap;
	}

	public Properties getAddressProps() {
		return addressProps;
	}

	public void setAddressProps(Properties addressProps) {
		this.addressProps = addressProps;
	}

	public CollectionBean() {
		
	}

}

1. List

 

@applicationContext.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
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

	<bean id = "collectionBean" class = "polymorphism.CollectionBean">
		<property name = "addressList">
			<list>
				<value>인천 연수구 청학동</value>
				<value>인천 연수구 송도 1동</value>
			</list>
		</property>
	</bean>

</beans>

 

@Main

package polymorphism;

import java.util.List;

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

public class CollectionBeanClient {

	public static void main(String[] args) {
		AbstractApplicationContext factory = new GenericXmlApplicationContext("applicationContext.xml");
		
		CollectionBean cb = (CollectionBean)factory.getBean("collectionBean");
		
		List<String> list1 = cb.getAddressList();
		
		for(String index:list1){
			System.out.println(index);
		}

	}

}

2. Set

 

@applicationContext.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
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

	<bean id = "collectionBean" class = "polymorphism.CollectionBean">
		<property name = "addressSet">
			<set value-type="java.lang.String">
				<value>인천 연구구 청학동</value>
				<value>인천 연구구 청학동</value>
				<value>인천 연구구 송도 1동</value>
			</set>
		</property>
	</bean>
 	
</beans>

 

@Main

package polymorphism;

import java.util.Iterator;
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) {
		//1. Spring Container 구동
		AbstractApplicationContext factory = new GenericXmlApplicationContext("applicationContext.xml");
		
		CollectionBean cb = (CollectionBean)factory.getBean("collectionBean");
		
	//	List<String> list1 = cb.getAddressList();
		Set<String> set1 = cb.getAddressSet();
		
		Iterator<String> itr = set1.iterator();
		
		while(itr.hasNext()){
			System.out.println(itr.next());
		}

	}

}

3. Map

 

@applicationContext.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
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

	<bean id = "collectionBean" class = "polymorphism.CollectionBean">
		<property name = "addressMap">
			<map>
				<entry>
					<key><value>1</value></key>
					<value>인천 연수구 청학동</value>
				</entry>
				<entry>
					<key><value>2</value></key>
					<value>인천 연수구 송도 1동</value>
				</entry>
			</map>
		</property>
	</bean>
</beans>

 

@Main

package polymorphism;

import java.util.Map;

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

public class CollectionBeanClient {

	public static void main(String[] args) {
		//1. Spring Container 구동
		AbstractApplicationContext factory = new GenericXmlApplicationContext("applicationContext.xml");
		
		CollectionBean cb = (CollectionBean)factory.getBean("collectionBean");
		
		//	List<String> list1 = cb.getAddressList();
		//Set<String> set1 = cb.getAddressSet();
		
		Map<Integer, String> map1 = cb.getAddressMap();
		
		for(int i = 1; i <= map1.size(); i++ ){
			System.out.println(map1.get(i));
		}

	}

}

4. Properties

 

@applicationContext.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
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

	<bean id = "collectionBean" class = "polymorphism.CollectionBean">
		<property name = "addressProps">
			<props>
				<prop key="춘햄">인천 연수구 청학동</prop>
				<prop key="뭉치">인천 연수구 송도 1동</prop>
			</props>
		</property>
	</bean>

</beans>

 

@Main

package polymorphism;

import java.util.Properties;
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) {
		//1. Spring Container 구동
		AbstractApplicationContext factory = new GenericXmlApplicationContext("applicationContext.xml");
		
		CollectionBean cb = (CollectionBean)factory.getBean("collectionBean");
		
		//	List<String> list1 = cb.getAddressList();
		//Set<String> set1 = cb.getAddressSet();
		//Map<Integer, String> map1 = cb.getAddressMap();
		Properties prop = cb.getAddressProps();
		
		System.out.println(prop.get("춘햄"));
		
	}

}

 

반응형
Comments