본문 바로가기
웹 프로그래밍(풀스택-->java)/웹프로그래밍(백엔드-->java)

57. 컬렉션 주입

by 백엔드개발자0107 2021. 12. 22.

자 여기까지가 기본 주입에 관련된 내용이 마무리가 된다.

 

 

MainClass.java

 

 

package kr.co.softcampus.main;

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

import org.springframework.context.support.ClassPathXmlApplicationContext;

import kr.co.softcampus.beans.DataBean;
import kr.co.softcampus.beans.TestBean;

public class MainClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("kr/co/softcampus/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: %d\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);
			
		}
		
		Map<String,Object>map1 = t1.getMap1();
		
		String a1 = (String)map1.get("a1");
		
		ctx.close();
	
	}
}

 

 

TestBean.java

 

package kr.co.softcampus.beans;

import java.util.List;
import java.util.Map;
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;
	
	
	public List<String> getList1() {
		return list1;
	}

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

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

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

	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;
	}
	
	
	
	
}

 

 

beans.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">

	<bean id="t1" class="kr.co.softcampus.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='kr.co.softcampus.beans.DataBean'></bean>
			<bean class='kr.co.softcampus.beans.DataBean'></bean>
			<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>
			<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="kr.co.sofcampus.beans.DataBean"></bean>
			<bean class='kr.co.softcampus.beans.DataBean'></bean>
			<ref bean = 'data_bean'/>
			<ref bean = 'data_bean'/>
			<!--스프링을 통해서 객체를 주입할떄 아이디가 똑같은 빈을 두개 이상 주입할떄 하나만 객체가 만들어진다.
			즉 위 	<bean class="kr.co.sofcampus.beans.DataBean"></bean> 이 태그는 두개다 객체가 생성되지만
			아래 ref bean은 하나만 생성이 된다. 
			 -->
			
			</set>
			</property>
			
			<!-- map -->
			<property name="map1">
			<map>
			<entry key="a1" value="문자열"></entry>
			<entry key="a2" value="100" value-type="int"></entry>
			<entry key="a3" >
			<bean class="kr.co.softcampus.beans.DataBean"/>
			</entry>
		<entry key="o4" value-ref="data_bean"/>			
			<entry key="a5">
			<list>
			<value>문자열1</value>
			<value>문자열2</value>
			</list>
			</entry>
			</map>
			</property>
	</bean>
	
	<bean id = "data_bean" class="kr.co.softcampus.beans.DataBean" scope="prototype"/>
<!-- scope="prototype"을 설정하면, 빈객체를 생성할떄마다 새로운 객체가 생성이 된다.  -->
</beans>