IOC (DI) - collections

list처럼 여러개의 값을 가지는 객체를 주입할 때
configuration 파일을 이용한 주입

list

CollectionList

package collection.bean.di;

import java.util.List;

public class CollectionList {
	private List<String> addressList;
	
	public void setAddressList(List<String> addressList) {
		this.addressList = addressList;
	}
	
	public List<String> getAddressList(){
		return addressList;
	}
}

CollectionBeanMain

package collection.bean.di;

import java.util.List;

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

public class CollectionBeanMain {
	public static void main(String[] args) {
		AbstractApplicationContext factory = 
				new GenericXmlApplicationContext("applicationContext.xml");
		
		CollectionList list = (CollectionList)factory.getBean("collectionList");
		List<String> addressList = list.getAddressList();
		
		for(String address : addressList)
			System.out.println(address);
		
		factory.close();
	}
}

set

CollectionSet

package collection.bean.di;

import java.util.Set;

public class CollectionSet {
	private Set<String> addressSet;

	public Set<String> getAddressSet() {
		return addressSet;
	}

	public void setAddressSet(Set<String> addressSet) {
		this.addressSet = addressSet;
	}	
}


CollectionBeanMain

package collection.bean.di;

import java.util.Set;

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

public class CollectionBeanMain {
	public static void main(String[] args) {
		AbstractApplicationContext factory = 
			new GenericXmlApplicationContext("applicationContext.xml");
		
		CollectionSet set = 
				(CollectionSet)factory.getBean("collectionSet");
		Set<String> addressSet = set.getAddressSet();
		for(String address : addressSet)
			System.out.println(address);
		
		factory.close();
	}
}

map

CollectionMap

package collection.bean.di;

import java.util.Map;

public class CollectionMap {
	private Map<String, String> addressMap;

	public Map<String, String> getAddressMap() {
		return addressMap;
	}

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

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

	<bean id="collectionMap" class="collection.bean.di.CollectionMap">
		<property name="addressMap">
			<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>
				<entry>
					<key>
						<value>일지매</value>
					</key>
					<value>서울 강남 비트캠프</value>
				</entry>

			</map>
		</property>
	</bean>
</beans>

CollectionBeanMain

package collection.bean.di;

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

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

public class CollectionBeanMain {
	public static void main(String[] args) {
		AbstractApplicationContext factory = 
			new GenericXmlApplicationContext("applicationContext.xml");
		
		CollectionMap map = 
				(CollectionMap)factory.getBean("collectionMap");
		Map<String, String> addressMap = map.getAddressMap();
		for(String name : addressMap.keySet())
			System.out.println(name + " : " + addressMap.get(name));
		
		factory.close();
	}
}


properties (map과 유사)

CollectionProperties

package collection.bean.di;

import java.util.Properties;

public class CollectionProperties {
	private Properties addressProperties;

	public Properties getAddressProperties() {
		return addressProperties;
	}

	public void setAddressProperties(Properties addressProperties) {
		this.addressProperties = addressProperties;
	}
	
	
}

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

	<bean id="collectionProperties" class="collection.bean.di.CollectionProperties">
		<property name="addressProperties">
			<props>
				<prop key="홍길동">서울 신촌 비트캠프</prop>
				<prop key="임꺽정">서울 신논현 비트캠프</prop>
				<prop key="장길산">서울 종로 비트캠프</prop>
				<prop key="일지매">서울 강남 비트캠프</prop>
			</props>
		</property>
	</bean>
</beans>

CollectionBeanMain

package collection.bean.di;

import java.util.Properties;

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

public class CollectionBeanMain {
	public static void main(String[] args) {
		AbstractApplicationContext factory = 
			new GenericXmlApplicationContext("applicationContext.xml");
		
		CollectionProperties properties = 
				(CollectionProperties)factory.getBean("collectionProperties");
		Properties addressProperties = properties.getAddressProperties();
		for(Object name : addressProperties.keySet())
			System.out.println(name + " : " + addressProperties.get(name));
		
		factory.close();
	}
}

좋은 웹페이지 즐겨찾기