개발자의 공부방/스프링
스프링 프레임워크] Collection List, Set, Map, Property 사용법
- -
728x90
반응형
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
<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
private SetCollectionBeanClient.javaaddressList; public Set getAddressList() { return addressList; } public void setAddressList(Set addressList) { this.addressList = addressList; }
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
private MapCollectionBeanClient.javaaddressList; public Map getAddressList() { return addressList; } public void setAddressList(Map addressList) { this.addressList = addressList; }
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
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>
반응형
'개발자의 공부방 > 스프링' 카테고리의 다른 글
스프링 기초] DI (Dependency Injection)란? (0) | 2019.05.21 |
---|---|
스프링 기초] 컨테이너와 IoC (Inversion of Control, 제어의 역전) 란? (1) | 2019.05.21 |
스프링 기초] 스프링 프레임워크 Request Life Cycle (1) | 2019.05.21 |
Simple Project와 Spring MVC Project 차이점? 그리고 Spring Project와 Spring Legacy Project 차이점은?? (0) | 2019.03.19 |
스프링이란?? (0) | 2019.03.18 |
Contents
소중한 공감 감사합니다