๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

[Spring] ์Šคํ”„๋ง XML ์„ค์ • ํŒŒ์ผ ์ž‘์„ฑ ๋ฐฉ๋ฒ• ์ •๋ฆฌ

by Leica 2020. 4. 13.
๋ฐ˜์‘ํ˜•

[Spring] ์Šคํ”„๋ง XML ์„ค์ • ํŒŒ์ผ ์ž‘์„ฑ ๋ฐฉ๋ฒ• ์ •๋ฆฌ

๐Ÿ“„ ๋ชฉ์ฐจ

1. ์Šคํ”„๋ง XML ์„ค์ • ํŒŒ์ผ ํฌ๋งท
- ๊ธฐ๋ณธ ํฌ๋งท
- ์• ๋…ธํ…Œ์ด์…˜ ์„ค์ •์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํฌ๋งท

2. ๋นˆ(Bean) ์„ค์ • ์˜ˆ์‹œ
- ์ž๋™ ์ฃผ์ž… ์„ค์ • - autowire ์†์„ฑ

3. DI(Dependency Injection) ์„ค์ •
- ์ƒ์„ฑ์ž ์ฃผ์ž…
- ํ”„๋กœํผํ‹ฐ ์ฃผ์ž…
- ํ”„๋กœํผํ‹ฐ ์ฃผ์ž… - List ํƒ€์ž…
- ํ”„๋กœํผํ‹ฐ ์ฃผ์ž… - Set ํƒ€์ž…
- ํ”„๋กœํผํ‹ฐ ์ฃผ์ž… - Map ํƒ€์ž…
- ํ”„๋กœํผํ‹ฐ ์ฃผ์ž… - Properties ํƒ€์ž…

4. ApplicationContext ๊ฐ์ฒด ์ƒ์„ฑ
- ์Šคํ”„๋ง XML ์„ค์ • ํŒŒ์ผ์ด ํ•œ ๊ฐœ์ธ ๊ฒฝ์šฐ
- ์Šคํ”„๋ง XML ์„ค์ • ํŒŒ์ผ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ
- ์Šคํ”„๋ง XML ์„ค์ • ํŒŒ์ผ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ(import ํƒœ๊ทธ ์‚ฌ์šฉ)

 

1. ์Šคํ”„๋ง XML ์„ค์ • ํŒŒ์ผ ํฌ๋งท

๊ธฐ๋ณธ ํฌ๋งท

๐Ÿ‘‰ ๋นˆ ๋“ฑ๋ก, ๋นˆ ์„ค์ •์„ ๋ชจ๋‘ 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">

</beans>

์œ„์™€ ๊ฐ™์€ ๋„ค์ž„ ์ŠคํŽ˜์ด์Šค์™€ ์Šคํ‚ค๋งˆ ์†์„ฑ์„ ๊ฐ–๋Š” <beans> ํƒœ๊ทธ ์•ˆ์— ์Šคํ”„๋ง ๋นˆ ์„ค์ •์„ ์ž‘์„ฑํ•œ๋‹ค.

 

์• ๋…ธํ…Œ์ด์…˜ ์„ค์ •์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํฌ๋งท

๐Ÿ‘‰ ๋นˆ ๋“ฑ๋ก์€ XML, ๋นˆ ์„ค์ •์€ ์ž๋ฐ” ์• ๋…ธํ…Œ์ด์…˜์œผ๋กœ ํ•  ๊ฒฝ์šฐ ์ด ํฌ๋งท์„ ์‚ฌ์šฉํ•œ๋‹ค.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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 
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>
</beans>

context ๋„ค์ž„ ์ŠคํŽ˜์ด์Šค์™€ <context:annotation-config/> ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋นˆ ์„ค์ •์„ XML ํŒŒ์ผ์ด ์•„๋‹Œ ๋นˆ ํด๋ž˜์Šค์˜ ์• ๋…ธํ…Œ์ด์…˜์„ ๊ฒ€์ƒ‰ํ•ด ๋ฐ˜์˜ํ•œ๋‹ค.

 

 

2. ๋นˆ(Bean) ์„ค์ • ์˜ˆ์‹œ

<bean> ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•ด ๋นˆ์„ ์ •์˜ํ•œ๋‹ค.

<bean id="studentDao" class="ems.member.dao.StudentDao" />

<bean> ํƒœ๊ทธ์˜ ํ•„์ˆ˜ ์†์„ฑ์€ class ์†์„ฑ ํ•˜๋‚˜ ๋ฟ์ด๋‹ค.

id๋Š” ๋นˆ์˜ id๋ฅผ ํ†ตํ•ด ์ฐธ์กฐํ•  ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค.

์ž๋ฐ” ์ฝ”๋“œ์—์„œ getBean()์œผ๋กœ ๋นˆ์„ ๊ฐ€์ ธ์˜ค๋˜์ง€, ์ฃผ์ž… ์„ค์ •์—์„œ ref ์†์„ฑ์œผ๋กœ ๋นˆ์„ ์ฐธ์กฐํ•˜๋Š” ๊ฒฝ์šฐ์ด๋‹ค.

id, class ์™ธ์—๋Š” lazy-init, scope, primary, init-method, destroy-method ์†์„ฑ์ด ์žˆ๋‹ค.

 

  • id : ๋นˆ ์ด๋ฆ„(id) ์„ค์ •
  • class : ๋นˆ ํƒ€์ž… ์„ค์ •
  • scope : ๋นˆ์˜ scope ์„ค์ •. singleton/prototype
  • primary : true๋ฅผ ์ง€์ •ํ•˜์—ฌ ๊ฐ™์€ ํƒ€์ž…์˜ ๋นˆ์ด ์—ฌ๋Ÿฌ๊ฐœ ์ผ๋•Œ ์šฐ์„ ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ๋นˆ ์„ค์ •
  • lazy-init : true๋ฅผ ์ง€์ •ํ•˜์—ฌ ๋นˆ์„ ์‚ฌ์šฉํ•  ๋•Œ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋„๋ก ์„ค์ •
  • init-method : ๋นˆ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋ ๋•Œ ํ˜ธ์ถœํ•  ๋ฉ”์†Œ๋“œ ์„ค์ •
  • destroy-method : ๋นˆ ๊ฐ์ฒด๊ฐ€ ์†Œ๋ฉธ๋ ๋•Œ ํ˜ธ์ถœํ•  ๋ฉ”์†Œ๋“œ ์„ค์ •

 

์ž๋™ ์ฃผ์ž… ์„ค์ • - autowire ์†์„ฑ

ํด๋ž˜์Šค์˜ ์ฐธ์กฐ ํƒ€์ž… ํ•„๋“œ์— ํ•œํ•ด์„œ autowire ์†์„ฑ์„ ์‚ฌ์šฉํ•ด ์ž๋™ ์ฃผ์ž…๋˜๋„๋ก ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

XML ์„ค์ • ํŒŒ์ผ

<bean id='obj2' class='com.atoz_develop.beans.TestBean1' autowire="byName"/>

<bean id='data1' class='com.atoz_develop.beans.DataBean1'/>
<bean id='data2' class='com.atoz_develop.beans.DataBean1'/>

<bean>์˜ autowire ์†์„ฑ์— "byName"์„ ์„ค์ •ํ•œ๋‹ค.

 

์ž๋ฐ” ์ฝ”๋“œ

public class TestBean1 {

	private DataBean1 data1;
	private DataBean1 data2;
	
	public DataBean1 getData1() {
		return data1;
	}
	public void setData1(DataBean1 data1) {
		this.data1 = data1;
	}
	public DataBean1 getData2() {
		return data2;
	}
	public void setData2(DataBean1 data2) {
		this.data2 = data2;
	}
}

TestBean1 ํด๋ž˜์Šค์˜ ๋นˆ์„ ์ƒ์„ฑํ•  ๋•Œ DataBean1 ํƒ€์ž…์ธ ํ•„๋“œ data1, data2์— ๋ณ€์ˆ˜๋ช…๊ณผ id๊ฐ€ ๋™์ผํ•œ ๋นˆ์ด ์ž๋™์œผ๋กœ ์ฃผ์ž…๋œ๋‹ค.

๋”ฐ๋ผ์„œ <bean>์˜ id ์†์„ฑ๊ณผ ํด๋ž˜์Šค์˜ ๋ณ€์ˆ˜๋ช…์ด ๋™์ผํ•ด์•ผ ํ•˜๋ฉฐ, setter๋ฅผ ํ†ตํ•ด ์ฃผ์ž…๋˜๋ฏ€๋กœ setter๊ฐ€ ํ•„์š”ํ•˜๋‹ค.


๋‹ค์Œ์œผ๋กœ ๋นˆ์˜ ์ด๋ฆ„(id)๊ฐ€ ์•„๋‹Œ ๋นˆ์˜ ํƒ€์ž…์„ ํ†ตํ•œ ์ž๋™ ์ฃผ์ž…์„ ์•Œ์•„๋ณด์ž.

 

XML ์„ค์ • ํŒŒ์ผ

<bean id='obj3' class='com.atoz_develop.beans.TestBean2' autowire="byType"/>

<bean id='data_bean2' class='com.atoz_develop.beans.DataBean2'/>

<bean>์˜ autowire ์†์„ฑ์— "byType"์„ ์„ค์ •ํ•œ๋‹ค.

 

์ž๋ฐ” ์ฝ”๋“œ

public class TestBean2 {

	private DataBean2 data1;
	private DataBean2 data2;
	
	public DataBean2 getData1() {
		return data1;
	}
	public void setData1(DataBean2 data1) {
		this.data1 = data1;
	}
	public DataBean2 getData2() {
		return data2;
	}
	public void setData2(DataBean2 data2) {
		this.data2 = data2;
	}
}

TestBean2 ํด๋ž˜์Šค์˜ ๋นˆ์„ ์ƒ์„ฑํ•  ๋•Œ DataBean2 ํƒ€์ž…์ธ ํ•„๋“œ data1, data2์— ํƒ€์ž…์ด ๊ฐ™์€ ๋นˆ์ด ์ž๋™์œผ๋กœ ์ฃผ์ž…๋œ๋‹ค.

XML ์„ค์ •์— primary ์„ค์ • ์—†์ด ํƒ€์ž…์ด ๋™์ผํ•œ ๋นˆ์ด ๋‘ ๊ฐœ ์ด์ƒ ์ •์˜๋˜์–ด ์žˆ์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ์ด์— ์ฃผ์˜ํ•˜์ž.

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ setter๋ฅผ ํ†ตํ•ด ์ฃผ์ž…๋˜๋ฏ€๋กœ setter๊ฐ€ ํ•„์š”ํ•˜๋‹ค.


์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•œ ์ž๋™ ์ฃผ์ž…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•œ๋‹ค.

 

XML ์„ค์ • ํŒŒ์ผ

<bean id='data_bean2' class='com.atoz_develop.beans.DataBean2'/>
<bean id='obj5' class='com.atoz_develop.beans.TestBean3' autowire="constructor"/>

<bean>์˜ autowire ์†์„ฑ์— "constructor"๋ฅผ ์„ค์ •ํ•œ๋‹ค.

 

์ž๋ฐ” ์ฝ”๋“œ

public class TestBean3 {

	private int data1;
	private String data2;
	private DataBean2 data3;
	private DataBean2 data4;
	
	public TestBean3(DataBean2 data3, DataBean2 data4) {
		this.data3 = data3;
		this.data4 = data4;
	}
	
	public int getData1() {
		return data1;
	}
	public void setData1(int data1) {
		this.data1 = data1;
	}
	public String getData2() {
		return data2;
	}
	public void setData2(String data2) {
		this.data2 = data2;
	}
	public DataBean2 getData3() {
		return data3;
	}
	public void setData3(DataBean2 data3) {
		this.data3 = data3;
	}
	public DataBean2 getData4() {
		return data4;
	}
	public void setData4(DataBean2 data4) {
		this.data4 = data4;
	}
}

 

 

autowire ์†์„ฑ์„ ์ด์šฉํ•œ ์ž๋™ ์ฃผ์ž…์€ ์ฐธ์กฐ ํƒ€์ž…๋งŒ ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ๊ธฐ๋ณธ ํƒ€์ž… ํ•„๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง์ ‘ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

XML ์„ค์ • ํŒŒ์ผ

<bean id='obj7' class='com.atoz_develop.beans.TestBean3' autowire="constructor">
	<constructor-arg value='200' index='0' type='int'/>
	<constructor-arg value='๋ฌธ์ž์—ด2' index='1'/>
</bean>

 

์ž๋ฐ” ์ฝ”๋“œ

public class TestBean3 {

	private int data1;
	private String data2;
	private DataBean2 data3;
	private DataBean2 data4;
	
	public TestBean3(int data1, String data2, DataBean2 data3, DataBean2 data4) {
		this.data1 = data1;
		this.data2 = data2;
		this.data3 = data3;
		this.data4 = data4;
	}
	
	public int getData1() {
		return data1;
	}
	public void setData1(int data1) {
		this.data1 = data1;
	}
	public String getData2() {
		return data2;
	}
	public void setData2(String data2) {
		this.data2 = data2;
	}
	public DataBean2 getData3() {
		return data3;
	}
	public void setData3(DataBean2 data3) {
		this.data3 = data3;
	}
	public DataBean2 getData4() {
		return data4;
	}
	public void setData4(DataBean2 data4) {
		this.data4 = data4;
	}
}

<beans> ๋ ˆ๋ฒจ์— default-autowire ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์„œ <bean>์— autowire ์„ค์ •์ด ๋˜์–ด์žˆ์ง€ ์•Š์„ ๊ฒฝ์šฐ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ ์šฉํ•  ์ž๋™ ์ฃผ์ž… ๋ฐฉ๋ฒ•์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

<?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"
	default-autowire="byName">

์ด๋ ‡๊ฒŒ <beans>์— default-autowire ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ž๋™ ์ฃผ์ž… ์˜ต์…˜์„ ์„ค์ •ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ํŠน์ • ๋นˆ์€ ์ž๋™ ์ฃผ์ž…์„ ํ•˜์ง€ ์•Š์œผ๋ ค๋ฉด <bean>์˜ autowire ์†์„ฑ์— "no"๋ฅผ ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค.

 

<bean id='obj9' class='com.atoz_develop.beans.TestBean1' autowire="no"/>

์ด ๋นˆ์€ default-autowire์˜ ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๊ฒŒ ๋œ๋‹ค.

 

 

3. DI(Dependency Injection) ์„ค์ •

์ƒ์„ฑ์ž ์ฃผ์ž…

XML ์„ค์ • ํŒŒ์ผ

<bean id="studentDao" class="ems.member.dao.StudentDao" ></bean>

<bean id="registerService" class="ems.member.service.StudentRegisterService">
    <constructor-arg ref="studentDao" />
</bean>

 

์ž๋ฐ” ์ฝ”๋“œ

public class StudentRegisterService {

	private StudentDao studentDao;
	
	public StudentRegisterService(StudentDao studentDao) {
		this.studentDao = studentDao;
	}
}

์ƒ์„ฑ์ž๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ <constructor-arg>์— type ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์„œ ์–ด๋–ค ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋นˆ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๊ฒƒ์ธ์ง€ ๊ตฌ์ฒด์ ์œผ๋กœ ๋ช…์‹œํ•  ์ˆ˜ ์žˆ๋‹ค.

์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ String ํƒ€์ž…์˜ ์•„๊ทœ๋จผํŠธ๋ฅผ ๊ฐ–๋Š” ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ ๊ทธ ์™ธ์˜ ๊ธฐ๋ณธ ํƒ€์ž…์„ ๊ฐ–๋Š” ์ƒ์„ฑ์ž ์ฃผ์ž… ์‹œ type์„ ๋ช…์‹œํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

 

XML ์„ค์ • ํŒŒ์ผ

<bean id="obj1" class="com.atoz_develop.beans.TestBean">
	<constructor-arg value="100" type="int"/>
</bean>

<bean id="obj2" class="com.atoz_develop.beans.TestBean">
	<constructor-arg value="11.11" type="double"/>
</bean>

<bean id="obj3" class="com.atoz_develop.beans.TestBean">
	<constructor-arg value="๋ฌธ์ž์—ด" type="java.lang.String"/>
</bean>

 

์ž๋ฐ” ์ฝ”๋“œ

public class TestBean {
	
	private int data1;
	private double data2;
	private String data3;

	public TestBean(int data1) {
		this.data1 = data1;
		this.data2 = 0.0;
		this.data3 = null;
	}
	
	public TestBean(double data2) {
		this.data1 = 0;
		this.data2 = data2;
		this.data3 = null;
	}
	
	public TestBean(String data3) {
		this.data1 = 0;
		this.data2 = 0.0;
		this.data3 = data3;
	}
}

์ž๋ฐ”์˜ new ์—ฐ์‚ฐ์ž๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ• ๋•Œ์™€ ๋‹ฌ๋ฆฌ ์Šคํ”„๋ง DI์˜ ์ƒ์„ฑ์ž ์ฃผ์ž… ์„ค์ • ์‹œ์—๋Š” ์ƒ์„ฑ์ž ์•„๊ทœ๋จผํŠธ์˜ ์ˆœ์„œ๋ฅผ ๋‹ค๋ฅด๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์Šคํ”„๋ง์ด ์ˆœ์„œ๋ฅผ ์œ ์ถ”ํ•˜๊ธฐ ์–ด๋ ค์šธ ๋•Œ๋Š” type๊ณผ index ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์„œ ์ˆœ์„œ๋ฅผ ๊ตฌ์ฒด์ ์œผ๋กœ ๋ช…์‹œํ•œ๋‹ค.

 

XML ์„ค์ • ํŒŒ์ผ

<bean id="obj1" class="com.atoz_develop.beans.TestBean">
	<constructor-arg value="๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค" type="java.lang.String"/>
	<constructor-arg value="300" type="int"/>
	<constructor-arg value="33.33" type="double"/>
</bean>

<bean id="obj2" class="com.atoz_develop.beans.TestBean">
	<constructor-arg value="44.44" type="double" index="1"/>
	<constructor-arg value="44" type="int" index="0"/>
	<constructor-arg value="์•ˆ๋…•ํ•˜์„ธ์š”" type="java.lang.String" index="2"/>
</bean>

 

์ž๋ฐ” ์ฝ”๋“œ

public class TestBean {
	
	private int data1;
	private double data2;
	private String data3;
	
	public TestBean(int data1, double data2, String data3) {
		this.data1 = data1;
		this.data2 = data2;
		this.data3 = data3;
	}
}

๊ฐ์ฒด ์ฃผ์ž… ์‹œ  <constructor-arg>์— ref ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์™ธ์—๋„ <constructor-arg> ํ•˜์œ„์— <bean>์„ ์‚ฌ์šฉํ•ด์„œ ๋นˆ์„ ์ง์ ‘ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

XML ์„ค์ • ํŒŒ์ผ

<bean id="obj" class="com.atoz_develop.beans.TestBean">
	<constructor-arg>
		<bean class="com.atoz_develop.beans.DataBean"/>
	</constructor-arg>
	<constructor-arg>
		<bean class="com.atoz_develop.beans.DataBean"/>
	</constructor-arg>
</bean>

 

์ž๋ฐ” ์ฝ”๋“œ

public class TestBean {
	
	private DataBean data1;
	private DataBean data2;
	
	public TestBean(DataBean data1, DataBean data2) {
		this.data1 = data1;
		this.data2 = data2;
	}
}

 

 

ํ”„๋กœํผํ‹ฐ ์ฃผ์ž…

XML ์„ค์ • ํŒŒ์ผ

<bean id="dataBaseConnectionInfoDev" class="ems.member.DataBaseConnectionInfo">
	<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe" />
	<property name="userId" value="scott" />
	<property name="userPw" value="tiger" />
</bean>

<property>์˜ value ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์„œ ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค.

 

์ž๋ฐ” ์ฝ”๋“œ

public class DataBaseConnectionInfo {

    private String jdbcUrl;
    private String userId;
    private String userPw;

    public String getJdbcUrl() {
        return jdbcUrl;
    }

    public void setJdbcUrl(String jdbcUrl) {
        this.jdbcUrl = jdbcUrl;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserPw() {
        return userPw;
    }

    public void setUserPw(String userPw) {
        this.userPw = userPw;
    }
}

XML ์„ค์ • ํŒŒ์ผ

<bean id="informationService" class="ems.member.service.EMSInformationService">
	<property name="info">
		<value>Education Management System program was developed in 2015.</value>
	</property>
</bean>

value ๊ฐ’์ด ๊ธธ ๊ฒฝ์šฐ <property>์˜ ํ•˜์œ„์— <value> ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ’์„ ์„ค์ •ํ• ์ˆ˜๋„ ์žˆ๋‹ค.

 

์ž๋ฐ” ์ฝ”๋“œ

public class EMSInformationService {

	private String info;
	
	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}
}

ํ”„๋กœํผํ‹ฐ๋ฅผ ํ†ตํ•œ ๊ฐ์ฒด ์ฃผ์ž…์€ ์ƒ์„ฑ์ž ์ฃผ์ž…๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ <property> ํ•˜์œ„์— ๋นˆ์„ ์ง์ ‘ ์ •์˜ํ•˜๊ฑฐ๋‚˜ ref ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์„œ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

XML ์„ค์ • ํŒŒ์ผ

<bean id="testBean" class="com.atoz_develop.beans.TestBean">
	<property name="data1">
		<bean class="com.atoz_develop.beans.DataBean"/>
	</property>
	<property name="data2" ref="data_bean"/>
</bean>

<bean id="dataBean" class="com.atoz_develop.beans.DataBean"/>

 

์ž๋ฐ” ์ฝ”๋“œ

public class TestBean {

	private DataBean data1;
	private DataBean data2;

	public DataBean getData1() {
		return data1;
	}

	public void setData1(DataBean data1) {
		this.data1 = data1;
	}

	public DataBean getData2() {
		return data2;
	}

	public void setData2(DataBean data2) {
		this.data2 = data2;
	}
}

 

 

ํ”„๋กœํผํ‹ฐ ์ฃผ์ž… - List ํƒ€์ž…

XML ์„ค์ • ํŒŒ์ผ

<bean id="informationService" class="ems.member.service.EMSInformationService">
	<property name="developers">
		<list>
			<value>Cheney.</value>
			<value>Eloy.</value>
			<value>Jasper.</value>
			<value>Dillon.</value>
			<value>Kian.</value>
		</list>
	</property>
</bean>

List ํƒ€์ž…์€ <property>.<list> ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

์ž๋ฐ” ์ฝ”๋“œ

public class EMSInformationService {

	private List<String> developers;

	public List<String> getDevelopers() {
		return developers;
	}

	public void setDevelopers(List<String> developers) {
		this.developers = developers;
	}
}

 

์ถ”๊ฐ€๋กœ <value>์—๋Š” type ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์„œ ํƒ€์ž…์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

<property name="list2">
	<list>
		<value type='int'>100</value>
		<value type='int'>200</value>
		<value type='int'>300</value>
	</list>
</property>

 

value๊ฐ€ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋ผ๋ฉด type ์†์„ฑ์œผ๋กœ ํƒ€์ž…์„ ์ง€์ •ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.


์ œ๋„ค๋ฆญ์ด Class์ธ List์˜ ์ฃผ์ž…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•œ๋‹ค.

 

XML ์„ค์ • ํŒŒ์ผ

<bean id='dataBean' class='com.atoz_develop.beans.DataBean' scope='prototype'/>

<bean id='t1' class='com.atoz_develop.beans.TestBean'>
<property name="list3">
	<list>
		<bean class='com.atoz_develop.beans.DataBean'/>
		<bean class='com.atoz_develop.beans.DataBean'/>
		<ref bean='dataBean'/>
		<ref bean='dataBean'/>
	</list>
</property>
</bean>

<bean> ํƒœ๊ทธ๋กœ ๋นˆ์„ ์ง์ ‘ ์„ค์ •ํ•˜๋˜์ง€, <ref>๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ธฐ์กด์— ์„ค์ •๋œ ๋นˆ์„ ์ฐธ์กฐํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ž๋ฐ” ์ฝ”๋“œ

public class TestBean {
	
	private List<DataBean> list3;

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

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

 

ํ”„๋กœํผํ‹ฐ ์ฃผ์ž… - Set ํƒ€์ž…

XML ์„ค์ • ํŒŒ์ผ

<bean id='dataBean' class='com.atoz_develop.beans.DataBean' scope='prototype'/>

<bean id='t1' class='com.atoz_develop.beans.TestBean'>
<!-- ์ œ๋„ค๋ฆญ์ด String์ธ set -->
<property name="set1">
	<set>
		<value>๋ฌธ์ž์—ด1</value>
		<value>๋ฌธ์ž์—ด2</value>
		<value>๋ฌธ์ž์—ด3</value>
		<value>๋ฌธ์ž์—ด3</value>	<!-- Set์˜ ํŠน์„ฑ์ƒ ์ฃผ์ž…๋˜์ง€ ์•Š์Œ -->
		<value>๋ฌธ์ž์—ด3</value>	<!-- Set์˜ ํŠน์„ฑ์ƒ ์ฃผ์ž…๋˜์ง€ ์•Š์Œ -->
	</set>
</property>
<!-- ์ œ๋„ค๋ฆญ์ด Integer์ธ set -->
<property name="set2">
	<set>
		<value type='int'>100</value>
		<value type='int'>200</value>
		<value type='int'>300</value>
		<value type='int'>300</value>	<!-- Set์˜ ํŠน์„ฑ์ƒ ์ฃผ์ž…๋˜์ง€ ์•Š์Œ -->
		<value type='int'>300</value>	<!-- Set์˜ ํŠน์„ฑ์ƒ ์ฃผ์ž…๋˜์ง€ ์•Š์Œ -->
	</set>
</property>
<!-- ์ œ๋„ค๋ฆญ์ด DataBean์ธ set -->
<property name="set3">
	<set>
		<bean class='com.atoz_develop.beans.DataBean'/>
		<bean class='com.atoz_develop.beans.DataBean'/>
		<ref bean="dataBean"/>
		<ref bean="dataBean"/>	<!-- Set์˜ ํŠน์„ฑ์ƒ ์ฃผ์ž…๋˜์ง€ ์•Š์Œ -->
		<ref bean="dataBean"/>	<!-- Set์˜ ํŠน์„ฑ์ƒ ์ฃผ์ž…๋˜์ง€ ์•Š์Œ -->
		<ref bean="dataBean"/>	<!-- Set์˜ ํŠน์„ฑ์ƒ ์ฃผ์ž…๋˜์ง€ ์•Š์Œ -->
	</set>
</property>
</bean>

XML๋กœ ์ฃผ์ž… ์„ค์ •์„ ํ•  ๋•Œ Set ํƒ€์ž…์˜ ํŠน์„ฑ์ƒ ์ค‘๋ณต๋œ ๊ฐ’์„ ๋„ฃ์„ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— <value>์˜ ๊ฐ’์ด ๊ฐ™๊ฑฐ๋‚˜ <ref>์˜ bean id๊ฐ€ ๋™์ผํ•œ ๊ฒฝ์šฐ ์ฃผ์ž…๋˜์ง€ ์•Š๋Š”๋‹ค.

ํŠนํžˆ, <bean>์˜ scope ์†์„ฑ์ด prototype์ธ ๋นˆ์„ <ref>๋กœ ์„ค์ •ํ•ด๋„ ์ฃผ์ž…๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Set ํƒ€์ž…์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•ด์•ผ ํ• ๋•Œ๋Š” <bean> ํƒœ๊ทธ๋กœ ์ง์ ‘ ์ฃผ์ž…ํ•˜๋„๋ก ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.

 

์ž๋ฐ” ์ฝ”๋“œ

public class TestBean {
	
	private Set<String> set1;
	private Set<Integer> set2;
	private Set<DataBean> set3;

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

 

 

ํ”„๋กœํผํ‹ฐ ์ฃผ์ž… - Map ํƒ€์ž…

XML ์„ค์ • ํŒŒ์ผ

<bean id="informationService" class="ems.member.service.EMSInformationService">
	<property name="administrators">
		<map>
			<entry>
				<key>
					<value>Cheney</value>
				</key>
				<value>cheney@springPjt.org</value>
			</entry>
			<entry>
				<key>
					<value>Jasper</value>
				</key>
				<value>jasper@springPjt.org</value>
			</entry>
		</map>
	</property>
</bean>

Map ํƒ€์ž…์€ <property>.<map>.<entry>๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

์ž๋ฐ” ์ฝ”๋“œ

public class EMSInformationService {

	private Map<String, String> administrators;

	public Map<String, String> getAdministrators() {
		return administrators;
	}

	public void setAdministrators(Map<String, String> administrators) {
		this.administrators = administrators;
	}
}

๋‹ค์Œ์€ ์ œ๋„ค๋ฆญ ํƒ€์ž…์ด <String, Object> ์ธ Map์˜ ์ฃผ์ž… ์„ค์ • ์˜ˆ์ด๋‹ค.

 

XML ์„ค์ • ํŒŒ์ผ

<bean id='dataBean' class='com.atoz_develop.beans.DataBean' scope='prototype'/>

<!-- map -->
<bean id='t1' class='com.atoz_develop.beans.TestBean'>
<property name="map1">
	<map>
		<entry key="a1" value='๋ฌธ์ž์—ด'/>
		<entry key='a2' value='100' value-type='int'/>
		<entry key='a3'>
			<bean class='com.atoz_develop.beans.DataBean'/>
		</entry>
		<entry key='a4' value-ref="dataBean"/>
		<entry key='a5'>
			<list>
				<value>๋ฌธ์ž์—ด1</value>
				<value>๋ฌธ์ž์—ด2</value>
				<value>๋ฌธ์ž์—ด3</value>
			</list>
		</entry>
	</map>
</property>
</bean>

<entry>์˜ key, value ์†์„ฑ์œผ๋กœ ํ•œ ์ค„์— ์„ค์ •ํ• ์ˆ˜๋„ ์žˆ๋‹ค.

type์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฌธ์ž์—ด๋กœ ๋“ค์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— value-type ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์„œ ํƒ€์ž…์„ ์ง€์ •ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

์œ„ ์„ค์ •์€ "a1"์—๋Š” ๋ฌธ์ž์—ด์„, "a2"์—๋Š” ์ •์ˆ˜๋ฅผ, "a3", "a4"์—๋Š” DataBean ํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ, "a5"์—๋Š” List ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•˜๋Š” ์„ค์ •์ด๋‹ค.

 

์ž๋ฐ” ์ฝ”๋“œ

public class TestBean {

	private Map<String, Object> map1;

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

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

 

ํ”„๋กœํผํ‹ฐ ์ฃผ์ž… - Properties ํƒ€์ž…

XML ์„ค์ • ํŒŒ์ผ

<bean id='t1' class='com.atoz_develop.beans.TestBean'>
	<!-- property -->
	<property name="prop1">
		<props>
			<prop key="p1">๋ฌธ์ž์—ด1</prop>
			<prop key="p2">๋ฌธ์ž์—ด2</prop>
			<prop key="p3">๋ฌธ์ž์—ด3</prop>
		</props>
	</property>
</bean>

 

์ž๋ฐ” ์ฝ”๋“œ

public class TestBean {

	private Properties prop1;

	public Properties getProp1() {
		return prop1;
	}

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

 

4. ApplicationContext ๊ฐ์ฒด ์ƒ์„ฑ

์Šคํ”„๋ง XML ์„ค์ • ํŒŒ์ผ์ด ํ•œ ๊ฐœ์ธ ๊ฒฝ์šฐ

// GenericXmlApplicationContext ์‚ฌ์šฉ - 'classpath:' prefix ํ•„์š”
ApplicationContext context = new GenericXmlApplicationContext("classpath:applicationContext.xml");

// ClassPathXmlApplicationContext ์‚ฌ์šฉ
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

 

์Šคํ”„๋ง XML ์„ค์ • ํŒŒ์ผ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ

String[] appCtxs = {"classpath:appCtx1.xml", "classpath:appCtx2.xml", "classpath:appCtx3.xml"};
ApplicationContext context = new GenericXmlApplicationContext(appCtxs);

ApplicationContext context = new ClassPathXmlApplicationContext("appCtx1.xml", "appCtx2.xml", "appCtx3.xml");

 

์Šคํ”„๋ง XML ์„ค์ • ํŒŒ์ผ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ(import ํƒœ๊ทธ ์‚ฌ์šฉ)

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

	<import resource="classpath:appCtx2.xml"/>
	<import resource="classpath:appCtx3.xml"/>
</beans>

ApplicationContext ๊ฐ์ฒด ์ƒ์„ฑ์€ XML ์„ค์ • ํŒŒ์ผ์ด ํ•œ ๊ฐœ์ธ ๊ฒฝ์šฐ์™€ ๋™์ผํ•˜๋‹ค.

 

๋‚ด์šฉ ๋ฐ ์˜ˆ์ œ์†Œ์Šค ์ถœ์ฒ˜

์ธํ”„๋Ÿฐ - ์œค์žฌ์„ฑ์˜ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ ๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ์‹ค์Šต์„ ํ†ตํ•œ ์ž…๋ฌธ ๊ณผ์ •

https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%ED%94%84%EB%A0%88%EC%9E%84%EC%9B%8C%ED%81%AC-%EC%9E%85%EB%AC%B8

 

๊ด€๋ จ ๊ธ€

 - [Spring] ์Šคํ”„๋ง XML ์„ค์ • → ์• ๋…ธํ…Œ์ด์…˜ ์„ค์ • ๋ณ€ํ™˜ ๋ฐฉ๋ฒ•

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€