본문 바로가기
개발자의 공부방/스프링

스프링 프레임워크] Collection List, Set, Map, Property 사용법

by 쌈빡한 쥬니준 2019. 2. 21.

https://devbox.tistory.com/entry/Java-%EC%BB%AC%EB%A0%89%EC%85%98-%ED%94%84%EB%A0%88%EC%9E%84%EC%9B%8C%ED%81%AC




List



applicationContext.xml
<bean id="collectionBean" class="org.green.dbtest.injection.CollectionBean">
	
		<property name="addressList">
			<list>
				<value>경기도 수원시 장안구 연무동</value>
				<value>서울시 강남구 대치동</value>
				<value>서울시 성북구 성북동</value>
				<value>서울시 중랑구 중화동</value>
			</list>
		</property>
		</bean>
CollectionBean.java
package org.green.dbtest.injection;

import java.util.Properties;

public class CollectionBean {
	private List<String> addressList;
	public List<String> getAddressList() {
		return addressList;
	}

	public void setAddressList(List<String> addressList) {
		this.addressList = addressList;
	}
}
CollectionBeanClient.java
package org.green.dbtest.injection;

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 bean = (CollectionBean)factory.getBean("collectionBean");
		
		List<String>addressList = bean.getAddressList();		
		for(String address: addressList) {
			System.out.println(address.toString());
		}
	factory.close();
	}
}




Set


CollectionBean.java
private Set addressList;

	public Set getAddressList() {
		return addressList;
	}

	public void setAddressList(Set addressList) {
		this.addressList = addressList;
	}
CollectionBeanClient.java
package org.green.dbtest.injection;

//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 bean = (CollectionBean)factory.getBean("collectionBean");
		
		Set<String>addressList = bean.getAddressList();		
		for(String address: addressList) {
			System.out.println(address.toString());
		}
	factory.close();
	}
}
applicationContext.xml
<bean id="collectionBean" class="org.green.dbtest.injection.CollectionBean">
	
		<property name="addressList">
			<set>
				<value>경기도 수원시 장안구 연무동</value>
				<value>서울시 강남구 대치동</value>
				<value>서울시 성북구 성북동</value>
				<value>서울시 중랑구 중화동</value>
			</set>
		</property>
		</bean>



Map


CollectionBean.java
private Map addressList;

	public Map getAddressList() {
		return addressList;
	}

	public void setAddressList(Map addressList) {
		this.addressList = addressList;
	}
CollectionBeanClient.java
package org.green.dbtest.injection;

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) {

		AbstractApplicationContext factory = new GenericXmlApplicationContext("applicationContext.xml");
		
		CollectionBean bean = (CollectionBean)factory.getBean("collectionBean");
		
		Map<String, String>addressList = bean.getAddressList();		
		for(String key: addressList.keySet()) {
			System.out.println(String.foramt("키:%s, 값:%s", key, addressList.get(key)));
		}
	factory.close();
	}
}
applicationContext.xml
<bean id="collectionBean" class="org.green.dbtest.injection.CollectionBean">
	
		<property name="addressList">
			<map>
                         <entry>
                           <key>
				<value>홍길동</value>
                           </key>
				<value>서울시 강남구 대치동</value>
                           </entry>
			</map>
		</property>
		</bean>



Property


CollectionBean.java
private Properties addressList;

	public Properties getAddressList() {
		return addressList;
	}

	public void setAddressList(Properties addressList) {
		this.addressList = addressList;
	}
}
CollectionBeanClient.java
package org.green.dbtest.injection;

//import java.util.Map;
import java.util.Properties;

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 bean = (CollectionBean)factory.getBean("collectionBean");

			Properties addressList = bean.getAddressList();
		
		for(String key: addressList.stringPropertyNames()) {
			System.out.println(String.format("키:%s, 값:%s", key, addressList.get(key)));
			//서식
		}
		factory.close();
	}
}
applicationContext.xml
<bean id="collectionBean" class="org.green.dbtest.injection.CollectionBean">
	<property name="addressList">
		<props>
			<prop key="홍길동"> 서울시 강남구 대치동<prop>
			<prop key="고길동"> 서울시 중랑구 중화동<prop>
		</props>

		</property>
		</bean>
<context:component-scan base-package="org.green.dbtest.injection"></context:component-scan>

</beans>



댓글0