250x250
Notice
Recent Posts
Recent Comments
«   2024/11   »
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
Tags more
Archives
Today
Total
관리 메뉴

개린이 개발노트

[Spring] 스프링 Spring Framework 컬렉션 주입 본문

스프링Spring

[Spring] 스프링 Spring Framework 컬렉션 주입

개린이9999 2023. 1. 15. 16:45
728x90

컬렉션 주입

Bean을 정의할 때 주입해야 하는 멤버가 컬렉션인 경우 컬렉션이 관리할 객체를 초기에 설정할 수 있다.

 

여기에 List, Map, Set, Property 사용

 

package sanghoon.main;


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

import org.springframework.context.support.ClassPathXmlApplicationContext;

import sanghoon.beans.DataBean;
import sanghoon.beans.TestBean;

public class MainClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub 
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("sanghoon/config/beans.xml");
	
		TestBean t1 = ctx.getBean("t1",TestBean.class);
		
		List<String> list1 = t1.getList1();
		for(String str: list1) {
			System.out.printf("list1:%s\n",str);
		}
		
		
		
		List<Integer> list2 = t1.getList2();
		for(int value: list2) {
			System.out.printf("list2:%d\n",value);
		}
		
		List<DataBean> list3 = t1.getList3();
		for(DataBean obj: list3) {
			System.out.printf("list3:%s\n",obj);
		}
		
		System.out.println("-----------------");
		
		Set<String> set1 = t1.getSet1();
		Set<Integer> set2 = t1.getSet2();
		Set<DataBean> set3 = t1.getSet3();
		
		for(String str: set1) {
			System.out.printf("set1:%s\n",str);
		}
		
		for(int value: set2) {
			System.out.printf("set2:%d\n",value);
		}
		
		for(DataBean obj: set3) {
			System.out.printf("set3:%s\n",obj);
		}
		
		System.out.println("---------------------");
		
		Map<String, Object> map1 =t1.getMap1();
		
		String a1=(String)map1.get("a1");
		int a2 = (Integer)map1.get("a2");
		DataBean a3 = (DataBean)map1.get("a3");
		DataBean a4 = (DataBean)map1.get("a4");
		List<String> a5= (List<String>)map1.get("a5");
		
		System.out.printf("a1:%s\n", a1);
		System.out.printf("a2:%d\n", a2);
		System.out.printf("a3:%s\n", a3);
		System.out.printf("a4:%s\n", a4);
		
		
		for(String str : a5) {
			System.out.printf("a5:%s\n",str);
		}
		
		System.out.println("---------------------");

		
		ctx.close();
	}
  
}
package sanghoon.beans;

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


public class TestBean {
	
	private List<String> list1;
	private List<Integer> list2;
	private List<DataBean> list3;
	
	private Set<String> set1;
	private Set<Integer> set2;
	private Set<DataBean> set3;
	
	private Map<String, Object> map1;
	
	private Properties prop1;
	
	public List<String> getList1() {
		return list1;
	}

	public List<Integer> getList2() {
		return list2;
	}

	public void setList2(List<Integer> list2) {
		this.list2 = list2;
	}

	public void setList1(List<String> list1) {
		this.list1 = list1;
	}

	public List<DataBean> getList3() {
		return list3;
	}

	public void setList3(List<DataBean> list3) {
		this.list3 = list3;
	}

	public Set<String> getSet1() {
		return set1;
	}

	public void setSet1(Set<String> set1) {
		this.set1 = set1;
	}

	public Set<Integer> getSet2() {
		return set2;
	}

	public void setSet2(Set<Integer> set2) {
		this.set2 = set2;
	}

	public Set<DataBean> getSet3() {
		return set3;
	}

	public void setSet3(Set<DataBean> set3) {
		this.set3 = set3;
	}

	public Map<String, Object> getMap1() {
		return map1;
	}

	public void setMap1(Map<String, Object> map1) {
		this.map1 = map1;
	}

	public Properties getProp1() {
		return prop1;
	}

	public void setProp1(Properties prop1) {
		this.prop1 = prop1;
	}

	 
}
<?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">

	<bean id='t1' class='sanghoon.beans.TestBean'>
		<!-- 제네릭이 String인 List -->
		<property name="list1">
			<list>
				<value>문자열1</value> 
				<value>문자열2</value> 
				<value>문자열3</value>
			</list>
		</property>
		<!-- 제네릭이 Integer인 List -->
		<property name="list2">
			<list>
				<value type='int'>100</value>
				<value type='int'>200</value>
				<value type='int'>300</value>
			</list>
		</property>
			<!-- 제네릭이 DataBean인 List -->
		<property name="list3">
			<list>
				<bean class='sanghoon.beans.DataBean'/>
				<bean class='sanghoon.beans.DataBean'/>
				<ref bean = 'data_bean'/>
				<ref bean = 'data_bean'/>
			</list>
		</property>
		
		<!-- 제네릭이 String인 set -->
		<property name="set1">
			<set>
				<value>문자열1</value> 
			    <value>문자열2</value>				
				<value>문자열3</value>
			</set>
		</property>
		<!-- 제네릭이 Integer인 set -->
		<property name="set2">
			<set>
				<value type='int'>100</value>
				<value type='int'>200</value>
				<value type='int'>300</value>
			</set>
		</property>
				
				<!-- 제네릭이 DataBean인 set -->
		<property name="set3">
			<set>
				<bean class='sanghoon.beans.DataBean'/>
				<bean class='sanghoon.beans.DataBean'/>
				<ref bean = 'data_bean'/>
				<ref bean = 'data_bean'/>
			</set>
		</property>

				<!-- map -->
		<property name="map1">
			<map>
				<entry key = "a1" value='문자열'/>
				<entry key = "a2" value='100' value-type='int'/>
				<entry key= 'a3'>
					<bean class='sanghoon.beans.DataBean'/>
				</entry>
				<entry key='a4' value-ref="data_bean"/>
				<entry key='a5'>
					<list>
					<value>문자열1</value>
				    <value>문자열2</value>
				    <value>문자열3</value>
					</list>
				</entry>
			</map>
		</property>
	
	
			<!-- property -->
		<property name="prop1">
			<props>
			<prop key="p1">문자열1</prop>
						<prop key="p2">문자열2</prop>
						<prop key="p3">문자열3</prop>
			
			</props>
		</property>
		
		</bean>
	<bean id='data_bean' class='sanghoon.beans.DataBean' scope = 'prototype'/>
</beans>
package sanghoon.beans;

public class DataBean {

}

Bean 객체를 생성할 때 주입할 멤버가 컬렉션이라면 컬렉션이 관리할 객체를 주입할 수 있다.

728x90