실습 2일차_Injection_Collection(list & set & map &props)

 

https://docs.spring.io/spring-framework/docs/4.3.0.RELEASE/spring-framework-reference/html/beans.html#beans-collection-elements

 

 Spring Injection_Collection_list

MainClass.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.Cat;
import com.dto.Person;

public class MainClass {
	public static void main(String[] args) {
		
		// IoC Container
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/test.xml");

		Person p1 = ctx.getBean("p1", Person.class);
		
		
		System.out.printf("이름:%s 나이:%s 주소:%s \n", p1.getUsername(), p1.getUserage(), p1.getAddress()); // printformat
		
		List<String> emailList = p1.getEmailList();
		for (String email : emailList) {
			System.out.println("email:\n" + email);
		}
		
		List<Cat> catList = p1.getCatList();
		for ( Cat c : catList) {
			System.out.printf("고양이 이름:%s 나이:%s 성별:%s \n", c.getName(), c.getAge(), c.getSex());
		}
			
	}

}

Cat.java

package com.dto;

public class Cat {
	String name;
	int age;
	String sex;
	
	/* test.xml
	 * <bean id="p1" class="com.dto.Person">
		<constructor-arg name="username" value="홍길동" />
		<constructor-arg name="userage" value="30" />
		<constructor-arg name="address" value="서울" />
		<constructor-arg name="cat" ref="cat" />
	</bean>
	 * constructor가 아닌 property는 기본 생성자가 있어야함
	 */
	
	public Cat() {
		
	}

	public Cat(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Cat [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
	
}

Person.java

package com.dto;
import java.util.List;

public class Person {
	
	String username;
	int userage;
	String address;
	
	// 이메일
	List<String> emailList;
	
	// 여러개의 Cat 관리
	List<Cat> catList;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getUserage() {
		return userage;
	}

	public void setUserage(int userage) {
		this.userage = userage;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public List<String> getEmailList() {
		return emailList;
	}

	public void setEmailList(List<String> emailList) {
		this.emailList = emailList;
	}

	public List<Cat> getCatList() {
		return catList;
	}

	public void setCatList(List<Cat> catList) {
		this.catList = catList;
	}

	@Override
	public String toString() {
		return "Person [username=" + username + ", userage=" + userage + ", address=" + address + ", emailList="
				+ emailList + ", catList=" + catList + "]";
	}
	
}

test.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="cat1" class="com.dto.Cat"> <!-- 생성자로 주입  -->
		<property name="name" value="나비" />
		<property name="age" value="3" />
		<property name="sex" value="암컷" />
	</bean>
	
	<bean id="cat2" class="com.dto.Cat"> <!-- 생성자로 주입  -->
		<property name="name" value="나비2" />
		<property name="age" value="3" />
		<property name="sex" value="숫컷" />
	</bean>
	
	<bean id="p1" class="com.dto.Person">
		<property name="username" value="고길동" />
		<property name="userage" value="30" />
		<property name="address" value="서울" />
		<property name="catList">
			<list>
				<ref bean="cat1" /> <!-- 0번째 -->
				<ref bean="cat2" /> <!-- 1번째 -->
			</list>
		</property>
		
		<property name="emailList">
			<list>
				<value>aaa@daum.net</value>
				<value>aaa@naver.com</value>
			</list>
		</property>
	</bean>
	
	<!-- https://docs.spring.io/spring-framework/docs/4.3.0.RELEASE/spring-framework-reference/html/beans.html#beans-collection-elements -->

</beans>

* 출력 화면 

 

 Spring Injection_Collection_list_util

MainClass.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.Cat;
import com.dto.Person;

public class MainClass {
	public static void main(String[] args) {
		
		// IoC Container
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/test.xml");


		Person p1 = ctx.getBean("p1", Person.class);
		
		
		System.out.printf("이름:%s 나이:%s 주소:%s \n", p1.getUsername(), p1.getUserage(), p1.getAddress()); // printformat
		
		List<String> emailList = p1.getEmailList();
		for ( String email : emailList) {
			System.out.println("email:\n" + email);
		}
		
		List<Cat> catList = p1.getCatList();
		for ( Cat c : catList) {
			System.out.printf("고양이 이름:%s 나이:%s 성별:%s \n", c.getName(), c.getAge(), c.getSex());
		}
		
	}
}

Cat.java

package com.dto;

public class Cat {
	String name;
	int age;
	String sex;
	
	/* test.xml 일부
	 * <bean id="p1" class="com.dto.Person">
		<constructor-arg name="username" value="홍길동" />
		<constructor-arg name="userage" value="30" />
		<constructor-arg name="address" value="서울" />
		<constructor-arg name="cat" ref="cat" />
	</bean>
	 * constructor가 아닌 property는 기본 생성자가 있어야함
	 */
	
	public Cat() {
		
	}

	public Cat(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Cat [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
	
}

Person.java

package com.dto;
import java.util.List;

public class Person {
	
	String username;
	int userage;
	String address;
	
	// 이메일
	List<String> emailList;
	
	// 여러개의 Cat 관리
	List<Cat> catList;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getUserage() {
		return userage;
	}

	public void setUserage(int userage) {
		this.userage = userage;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public List<String> getEmailList() {
		return emailList;
	}

	public void setEmailList(List<String> emailList) {
		this.emailList = emailList;
	}

	public List<Cat> getCatList() {
		return catList;
	}

	public void setCatList(List<Cat> catList) {
		this.catList = catList;
	}

	@Override
	public String toString() {
		return "Person [username=" + username + ", userage=" + userage + ", address=" + address + ", emailList="
				+ emailList + ", catList=" + catList + "]";
	}

}

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

	<bean id="cat1" class="com.dto.Cat"> <!-- 생성자로 주입  -->
		<property name="name" value="나비" />
		<property name="age" value="3" />
		<property name="sex" value="암컷" />
	</bean>
	
	<bean id="cat2" class="com.dto.Cat"> <!-- 생성자로 주입  -->
		<property name="name" value="나비2" />
		<property name="age" value="3" />
		<property name="sex" value="숫컷" />
	</bean>
	
	<util:list id="globalCatList">
		<ref bean="cat1" /> <!-- 0번째 -->
		<ref bean="cat2" /> <!-- 1번째 -->
		<ref bean="cat1" /> <!-- 중복허용 -->
		<ref bean="cat1" /> <!-- 중복허용 -->
	</util:list>
	
	<util:list id="globalEmailList">
		<value>aaa@daum.net</value>
		<value>aaa@naver.com</value>
	</util:list>
	
	<bean id="p1" class="com.dto.Person">
		<property name="username" value="고길동" />
		<property name="userage" value="30" />
		<property name="address" value="서울" />
		<property name="catList" ref="globalCatList" />
		<property name="emailList" ref="globalEmailList" />
	</bean>
	
	<!-- https://docs.spring.io/spring-framework/docs/4.3.0.RELEASE/spring-framework-reference/html/beans.html#beans-collection-elements -->

</beans>

* 출력 화면 

* spring link : https://docs.spring.io/spring-framework/docs/4.3.0.RELEASE/spring-framework-reference/html/beans.html#beans-collection-elements

 

 Spring Injection_Collection_set

MainClass.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.Cat;
import com.dto.Person;

public class MainClass {
	public static void main(String[] args) {
		
		// IoC Container
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/test.xml");

		Person p1 = ctx.getBean("p1", Person.class);
		
		System.out.printf("이름:%s 나이:%s 주소:%s \n", p1.getUsername(), p1.getUserage(), p1.getAddress()); // printformat
		
		List<String> emailList = p1.getEmailSet();
		for ( String email : emailList) {
			System.out.println("email:\n" + email);
		}
		
		List<Cat> catList = p1.getCatSet();
		for ( Cat c : catList) {
			System.out.printf("고양이 이름:%s 나이:%s 성별:%s \n", c.getName(), c.getAge(), c.getSex());
		}
			
	}
	
}

Cat.java

package com.dto;

public class Cat {
	String name;
	int age;
	String sex;
	
	/* test.xml 일부
	 * <bean id="p1" class="com.dto.Person">
		<constructor-arg name="username" value="홍길동" />
		<constructor-arg name="userage" value="30" />
		<constructor-arg name="address" value="서울" />
		<constructor-arg name="cat" ref="cat" />
	</bean>
	 * constructor가 아닌 property는 기본 생성자가 있어야함
	 */
	
	public Cat() {
		// TODO Auto-generated constructor stub
	}

	public Cat(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Cat [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
	
}

Person.java

package com.dto;

import java.util.List;

public class Person {
	
	String username;
	int userage;
	String address;
	
	// 이메일
	List<String> emailSet;
	
	// 여러개의 Cat 관리
	List<Cat> catSet;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getUserage() {
		return userage;
	}

	public void setUserage(int userage) {
		this.userage = userage;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public List<String> getEmailSet() {
		return emailSet;
	}

	public void setEmailSet(List<String> emailSet) {
		this.emailSet = emailSet;
	}

	public List<Cat> getCatSet() {
		return catSet;
	}

	public void setCatSet(List<Cat> catSet) {
		this.catSet = catSet;
	}

	@Override
	public String toString() {
		return "Person [username=" + username + ", userage=" + userage + ", address=" + address + ", emailSet="
				+ emailSet + ", catSet=" + catSet + "]";
	}

}

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

	<bean id="cat1" class="com.dto.Cat"> <!-- 생성자로 주입  -->
		<property name="name" value="나비" />
		<property name="age" value="3" />
		<property name="sex" value="암컷" />
	</bean>
	
	<bean id="cat2" class="com.dto.Cat"> <!-- 생성자로 주입  -->
		<property name="name" value="나비2" />
		<property name="age" value="3" />
		<property name="sex" value="숫컷" />
	</bean>
	
	<util:list id="globalCatSet">
		<ref bean="cat1" /> <!-- 0번째 -->
		<ref bean="cat2" /> <!-- 1번째 -->
		<ref bean="cat1" /> <!-- 중복허용 -->
		<ref bean="cat1" /> <!-- 중복허용 -->
	</util:list>
	
	<util:list id="globalEmailSet">
		<value>aaa@daum.net</value>
		<value>aaa@naver.com</value>
	</util:list>
	
	<bean id="p1" class="com.dto.Person">
		<property name="username" value="고길동" />
		<property name="userage" value="30" />
		<property name="address" value="서울" />
		<property name="catSet" ref="globalCatSet" />
		<property name="emailSet" ref="globalEmailSet" />
	</bean>
	
	<!-- https://docs.spring.io/spring-framework/docs/4.3.0.RELEASE/spring-framework-reference/html/beans.html#beans-collection-elements -->

</beans>

* 출력 화면 

 

 Spring Injection_Collection_set_util

MainClass.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.Cat;
import com.dto.Person;

public class MainClass {
	public static void main(String[] args) {
		
		// IoC Container
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/test.xml");

		Person p1 = ctx.getBean("p1", Person.class);
		
		
		System.out.printf("이름:%s 나이:%s 주소:%s \n", p1.getUsername(), p1.getUserage(), p1.getAddress()); // printformat
		
		List<String> emailList = p1.getEmailSet();
		for ( String email : emailList) {
			System.out.println("email:\n" + email);
		}
		
		List<Cat> catList = p1.getCatSet();
		for ( Cat c : catList) {
			System.out.printf("고양이 이름:%s 나이:%s 성별:%s \n", c.getName(), c.getAge(), c.getSex());
		}
				
	}

}

Cat.java

package com.dto;

public class Cat {
	String name;
	int age;
	String sex;
	
	/* test.xml
	 * <bean id="p1" class="com.dto.Person">
		<constructor-arg name="username" value="홍길동" />
		<constructor-arg name="userage" value="30" />
		<constructor-arg name="address" value="서울" />
		<constructor-arg name="cat" ref="cat" />
	</bean>
	 * constructor가 아닌 property는 기본 생성자가 있어야함
	 */
	
	public Cat() {
		
	}

	public Cat(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Cat [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
	
}

Person.java

package com.dto;

import java.util.List;

public class Person {
	
	String username;
	int userage;
	String address;
	
	// 이메일
	List<String> emailSet;
	
	// 여러개의 Cat 관리
	List<Cat> catSet;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getUserage() {
		return userage;
	}

	public void setUserage(int userage) {
		this.userage = userage;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public List<String> getEmailSet() {
		return emailSet;
	}

	public void setEmailSet(List<String> emailSet) {
		this.emailSet = emailSet;
	}

	public List<Cat> getCatSet() {
		return catSet;
	}

	public void setCatSet(List<Cat> catSet) {
		this.catSet = catSet;
	}

	@Override
	public String toString() {
		return "Person [username=" + username + ", userage=" + userage + ", address=" + address + ", emailSet="
				+ emailSet + ", catSet=" + catSet + "]";
	}

}

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

	<bean id="cat1" class="com.dto.Cat"> <!-- 생성자로 주입  -->
		<property name="name" value="나비" />
		<property name="age" value="3" />
		<property name="sex" value="암컷" />
	</bean>
	
	<bean id="cat2" class="com.dto.Cat"> <!-- 생성자로 주입  -->
		<property name="name" value="나비2" />
		<property name="age" value="3" />
		<property name="sex" value="숫컷" />
	</bean>
	
	<util:set id="globalCatSet">
		<ref bean="cat1" /> <!-- 0번째 -->
		<ref bean="cat2" /> <!-- 1번째 -->
		<ref bean="cat1" /> <!-- 중복허용 -->
		<ref bean="cat1" /> <!-- 중복허용 -->
	</util:set>
	
	<util:set id="globalEmailSet">
		<value>aaa@daum.net</value>
		<value>aaa@naver.com</value>
	</util:set>
	
	<bean id="p1" class="com.dto.Person">
		<property name="username" value="고길동" />
		<property name="userage" value="30" />
		<property name="address" value="서울" />
		<property name="catSet" ref="globalCatSet" />
		<property name="emailSet" ref="globalEmailSet" />
	</bean>
	
	<!-- https://docs.spring.io/spring-framework/docs/4.3.0.RELEASE/spring-framework-reference/html/beans.html#beans-collection-elements -->

</beans>

* 출력 화면 

 

 Spring Injection_Collection_map

MainClass.java

import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.Cat;
import com.dto.Person;

public class MainClass {
	public static void main(String[] args) {
		
		// IoC Container
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/test.xml");

		Person p1 = ctx.getBean("p1", Person.class);
			
		System.out.printf("이름:%s 나이:%s 주소:%s \n", p1.getUsername(), p1.getUserage(), p1.getAddress()); // printformat
		
		Map<String, String> emailMap = p1.getEmailMap();
		Set<String> keys = emailMap.keySet();
		for (String key : keys) {
			System.out.printf("이메일:%s", emailMap.get(key));
		}
		
		Map<String, Cat> catMap = p1.getCatMap();
		Set<String> keys2 = catMap.keySet();
		for (String key : keys2) {
			Cat c = catMap.get(key);
			System.out.printf("고양이 이름:%s 나이:%d 성별:%s \n", c.getName(), c.getAge(), c.getSex());
		}
				
	}

}

Cat.java

package com.dto;

public class Cat {
	String name;
	int age;
	String sex;
	
	/* test.xml 일부
	 * <bean id="p1" class="com.dto.Person">
		<constructor-arg name="username" value="홍길동" />
		<constructor-arg name="userage" value="30" />
		<constructor-arg name="address" value="서울" />
		<constructor-arg name="cat" ref="cat" />
	</bean>
	 * constructor가 아닌 property는 기본 생성자가 있어야함
	 */
	
	public Cat() {

	}

	public Cat(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Cat [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
	
}

Person.java

package com.dto;
import java.util.List;
import java.util.Map;

public class Person {
	
	String username;
	int userage;
	String address;
	
	// 이메일
	Map<String, String> emailMap;
	
	// 여러개의 Cat 관리
	Map<String, Cat> catMap;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getUserage() {
		return userage;
	}

	public void setUserage(int userage) {
		this.userage = userage;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Map<String, String> getEmailMap() {
		return emailMap;
	}

	public void setEmailMap(Map<String, String> emailMap) {
		this.emailMap = emailMap;
	}

	public Map<String, Cat> getCatMap() {
		return catMap;
	}

	public void setCatMap(Map<String, Cat> catMap) {
		this.catMap = catMap;
	}

	@Override
	public String toString() {
		return "Person [username=" + username + ", userage=" + userage + ", address=" + address + ", emailMap="
				+ emailMap + ", catMap=" + catMap + "]";
	}

}

test.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="cat1" class="com.dto.Cat"> <!-- 생성자로 주입  -->
		<property name="name" value="나비" />
		<property name="age" value="3" />
		<property name="sex" value="암컷" />
	</bean>
	
	<bean id="cat2" class="com.dto.Cat"> <!-- 생성자로 주입  -->
		<property name="name" value="나비2" />
		<property name="age" value="3" />
		<property name="sex" value="숫컷" />
	</bean>
	
	<bean id="p1" class="com.dto.Person">
		<property name="username" value="고길동" />
		<property name="userage" value="30" />
		<property name="address" value="서울" />
		<property name="catMap">
			<map>
				<entry key="c1" value-ref="cat1" />
				<entry key="c2">
					<ref bean="cat2"/>
				</entry>
			</map>
		</property>
		
		<property name="emailMap">
			<map>
				<entry key="e1" value="aaa@daum.net" />
				<entry key="e2">
					<value>aaa@naver.com</value>
				</entry>
			</map>
		</property>
	</bean>
	
	<!-- https://docs.spring.io/spring-framework/docs/4.3.0.RELEASE/spring-framework-reference/html/beans.html#beans-collection-elements -->

</beans>

* 출력 화면 

 

 Spring Injection_Collection_map_util

MainClass.java

import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.Cat;
import com.dto.Person;

public class MainClass {
	public static void main(String[] args) {
		
		// IoC Container
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/test.xml");


		Person p1 = ctx.getBean("p1", Person.class);
		
		System.out.printf("이름:%s 나이:%s 주소:%s \n", p1.getUsername(), p1.getUserage(), p1.getAddress()); // printformat
		
		Map<String, String> emailMap = p1.getEmailMap();
		Set<String> keys = emailMap.keySet();
		for (String key : keys) {
			System.out.printf("이메일:%s \n", emailMap.get(key));
		}
		
		Map<String, Cat> catMap = p1.getCatMap();
		Set<String> keys2 = catMap.keySet();
		for (String key : keys2) {
			Cat c = catMap.get(key);
			System.out.printf("고양이 이름:%s 나이:%d 성별:%s \n", c.getName(), c.getAge(), c.getSex());
		}
		
	}

}

Cat.java

package com.dto;

public class Cat {
	String name;
	int age;
	String sex;
	
	/* test.xml 일부
	 * <bean id="p1" class="com.dto.Person">
		<constructor-arg name="username" value="홍길동" />
		<constructor-arg name="userage" value="30" />
		<constructor-arg name="address" value="서울" />
		<constructor-arg name="cat" ref="cat" />
	</bean>
	 * constructor가 아닌 property는 기본 생성자가 있어야함
	 */
	
	public Cat() {
		// TODO Auto-generated constructor stub
	}

	public Cat(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Cat [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
	
}

Person.java

package com.dto;
import java.util.List;
import java.util.Map;

public class Person {
	
	String username;
	int userage;
	String address;
	
	// 이메일
	Map<String, String> emailMap;
	
	// 여러개의 Cat 관리
	Map<String, Cat> catMap;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getUserage() {
		return userage;
	}

	public void setUserage(int userage) {
		this.userage = userage;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Map<String, String> getEmailMap() {
		return emailMap;
	}

	public void setEmailMap(Map<String, String> emailMap) {
		this.emailMap = emailMap;
	}

	public Map<String, Cat> getCatMap() {
		return catMap;
	}

	public void setCatMap(Map<String, Cat> catMap) {
		this.catMap = catMap;
	}

	@Override
	public String toString() {
		return "Person [username=" + username + ", userage=" + userage + ", address=" + address + ", emailMap="
				+ emailMap + ", catMap=" + catMap + "]";
	}

}

test.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:c="http://www.springframework.org/schema/c"
	xmlns:util="http://www.springframework.org/schema/util"
	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
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<bean id="cat1" class="com.dto.Cat"> <!-- 생성자로 주입  -->
		<property name="name" value="나비" />
		<property name="age" value="3" />
		<property name="sex" value="암컷" />
	</bean>
	
	<bean id="cat2" class="com.dto.Cat"> <!-- 생성자로 주입  -->
		<property name="name" value="나비2" />
		<property name="age" value="3" />
		<property name="sex" value="숫컷" />
	</bean>
	
	<util:map id="globalCatMap">
		<entry key="c1" value-ref="cat1" />
		<entry key="c2">
			<ref bean="cat2" />
		</entry>
	</util:map>
	
	<util:map id="globalEmailMap">
		<entry key="e1" value="aaa@daum.net" />
		<entry key="e2">
			<value>aaa2@naver.com</value>
		</entry>
	</util:map>
	
	<bean id="p1" class="com.dto.Person">
		<property name="username" value="고길동" />
		<property name="userage" value="30" />
		<property name="address" value="서울" />
		<property name="catMap" ref="globalCatMap" />	
		<property name="emailMap" ref="globalEmailMap" />
			
	</bean>
	
	<!-- https://docs.spring.io/spring-framework/docs/4.3.0.RELEASE/spring-framework-reference/html/beans.html#beans-collection-elements -->

</beans>

* 출력 화면 

 

 Spring Injection_Collection_props

MainClass.java

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.Cat;
import com.dto.Person;

public class MainClass {
	public static void main(String[] args) {
		
		// IoC Container
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/test.xml");

		Person p1 = ctx.getBean("p1", Person.class);
		
		
		System.out.printf("이름:%s 나이:%s 주소:%s", p1.getUsername(), p1.getUserage(), p1.getAddress()); // printformat
		
		
		Properties emailMap = p1.getEmailMap();
		Set<String> keys  = emailMap.stringPropertyNames();
		for(String key : keys) {
			System.out.printf("이메일:%s \n", emailMap.getProperty(key));
		}		
		
		Map<String, Cat> catMap = p1.getCatMap();
		Set<String> keys2 = catMap.keySet();
		for (String key : keys2) {
			Cat c = catMap.get(key);
			System.out.printf("고양이 이름:%s 나이:%d 성별:%s \n", c.getName(), c.getAge(), c.getSex());
		}

	}
}

Cat.java

package com.dto;

public class Cat {
	String name;
	int age;
	String sex;
	
	/* test.xml 일부
	 * <bean id="p1" class="com.dto.Person">
		<constructor-arg name="username" value="홍길동" />
		<constructor-arg name="userage" value="30" />
		<constructor-arg name="address" value="서울" />
		<constructor-arg name="cat" ref="cat" />
	</bean>
	 * constructor가 아닌 property는 기본 생성자가 있어야함
	 */
	
	public Cat() {
	
	}

	public Cat(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Cat [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
	
}

Person.java

package com.dto;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class Person {
	
	String username;
	int userage;
	String address;
	
	// 이메일
	//Map<String, String> emailMap;
	Properties emailMap;
	
	// 여러개의 Cat 관리
	Map<String, Cat> catMap;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getUserage() {
		return userage;
	}

	public void setUserage(int userage) {
		this.userage = userage;
	}


	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Properties getEmailMap() {
		return emailMap;
	}

	public void setEmailMap(Properties emailMap) {
		this.emailMap = emailMap;
	}

	public Map<String, Cat> getCatMap() {
		return catMap;
	}

	public void setCatMap(Map<String, Cat> catMap) {
		this.catMap = catMap;
	}

	@Override
	public String toString() {
		return "Person [username=" + username + ", userage=" + userage + ", address=" + address + ", emailMap="
				+ emailMap + ", catMap=" + catMap + "]";
	}
	
}

test.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="cat1" class="com.dto.Cat"> <!-- 생성자로 주입  -->
		<property name="name" value="나비" />
		<property name="age" value="3" />
		<property name="sex" value="암컷" />
	</bean>
	
	<bean id="cat2" class="com.dto.Cat"> <!-- 생성자로 주입  -->
		<property name="name" value="나비2" />
		<property name="age" value="3" />
		<property name="sex" value="숫컷" />
	</bean>
	
	<bean id="p1" class="com.dto.Person">
		<property name="username" value="홍길동" />
		<property name="userage" value="30" />
		<property name="address" value="서울" />
		<property name="catMap">
			<map>
				<entry key="c1" value-ref="cat1" />
				<entry key="c2">
					<ref bean="cat2"/>
				</entry>
			</map>
		</property>
		
		<property name="emailMap">
		
			<props>
				<prop key="e1">aaa@daum.net</prop>
				<prop key="e2">aaa2@naver.com</prop>
			</props>	
		</property>
	</bean>
	
	<!-- https://docs.spring.io/spring-framework/docs/4.3.0.RELEASE/spring-framework-reference/html/beans.html#beans-collection-elements -->
</beans>

* 출력 화면 

 

 Spring Injection_Collection_props_util

MainClass.java

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.Cat;
import com.dto.Person;

public class MainClass {
	public static void main(String[] args) {
		
		// IoC Container
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/test.xml");

		Person p1 = ctx.getBean("p1", Person.class);
		
		
		System.out.printf("이름:%s 나이:%s 주소:%s \n", p1.getUsername(), p1.getUserage(), p1.getAddress()); // printformat		
		
		Properties emailMap = p1.getEmailMap();
		Set<String> keys  = emailMap.stringPropertyNames();
		for(String key : keys) {
			System.out.printf("이메일:%s \n", emailMap.getProperty(key));
		}
	
		Map<String, Cat> catMap = p1.getCatMap();
		Set<String> keys2 = catMap.keySet();
		for (String key : keys2) {
			Cat c = catMap.get(key);
			System.out.printf("고양이 이름:%s 나이:%d 성별:%s \n", c.getName(), c.getAge(), c.getSex());
		}
			
	}

}

Cat.java

package com.dto;

public class Cat {
	String name;
	int age;
	String sex;
	
	/* test.xml 일부
	 * <bean id="p1" class="com.dto.Person">
		<constructor-arg name="username" value="홍길동" />
		<constructor-arg name="userage" value="30" />
		<constructor-arg name="address" value="서울" />
		<constructor-arg name="cat" ref="cat" />
	</bean>
	 * constructor가 아닌 property는 기본 생성자가 있어야함
	 */
	
	public Cat() {

	}

	public Cat(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Cat [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
	
}

Person.java

package com.dto;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class Person {
	
	String username;
	int userage;
	String address;
	
	// 이메일
	//Map<String, String> emailMap;
	Properties emailMap;
	
	// 여러개의 Cat 관리
	Map<String, Cat> catMap;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getUserage() {
		return userage;
	}

	public void setUserage(int userage) {
		this.userage = userage;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Properties getEmailMap() {
		return emailMap;
	}

	public void setEmailMap(Properties emailMap) {
		this.emailMap = emailMap;
	}

	public Map<String, Cat> getCatMap() {
		return catMap;
	}

	public void setCatMap(Map<String, Cat> catMap) {
		this.catMap = catMap;
	}

	@Override
	public String toString() {
		return "Person [username=" + username + ", userage=" + userage + ", address=" + address + ", emailMap="
				+ emailMap + ", catMap=" + catMap + "]";
	}

}

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

	<bean id="cat1" class="com.dto.Cat"> <!-- 생성자로 주입  -->
		<property name="name" value="나비" />
		<property name="age" value="3" />
		<property name="sex" value="암컷" />
	</bean>
	
	<bean id="cat2" class="com.dto.Cat"> <!-- 생성자로 주입  -->
		<property name="name" value="나비2" />
		<property name="age" value="3" />
		<property name="sex" value="숫컷" />
	</bean>
	
	<util:properties id="globalEmailProps">
		<prop key="e1">aaa@daum.net</prop>
		<prop key="e2">aaa2@naver.com</prop>
	</util:properties>
	
	<bean id="p1" class="com.dto.Person">
		<property name="username" value="고길동" />
		<property name="userage" value="30" />
		<property name="address" value="서울" />
		<property name="catMap">
			<map>
				<entry key="c1" value-ref="cat1" />
				<entry key="c2">
					<ref bean="cat2"/>
				</entry>
			</map>
		</property>
		<property name="emailMap" ref="globalEmailProps" />

	</bean>
	
	<!-- https://docs.spring.io/spring-framework/docs/4.3.0.RELEASE/spring-framework-reference/html/beans.html#beans-collection-elements -->

</beans>

* 출력 화면 

 

 출처

 

+ 강의 교재

+ Recent posts