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

๐Ÿ’ปStudy/Spring

xml์„ ์ด์šฉํ•œ ์Šคํ”„๋ง DI ์ง€์‹œ์„œ ์ž‘์„ฑ(Spring Bean Configuration), IoC ์ปจํ…Œ์ด๋„ˆ ์‚ฌ์šฉํ•˜๊ธฐ

์•ž์„œ ์Šคํ”„๋ง ์—†์ด ์ง์ ‘ DI๋ฅผ ํ•ด๋ณธ ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ํ•œ๋ฒˆ ์‚ดํŽด๋ณด๊ฒ ๋‹ค.

 

Program.java

public class Program {

	public static void main(String[] args) {
		/* ์Šคํ”„๋ง์—๊ฒŒ ์ง€์‹œํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค.
		 
		Exam exam = new NewlecExam(); //์ƒ์„ฑ
		//ExamConsole console = new GridExamConsole(exam); //์ƒ์„ฑ์ž ์ด์šฉํ•œ ์กฐ๋ฆฝ
		ExamConsole console = new GridExamConsole();
		console.setExam(exam); //setter๋ฅผ ํ†ตํ•œ ์กฐ๋ฆฝ(๊ฒฐํ•ฉ)
		*/
	
		ExamConsole console = ?; 
		console.print();		
	}

}

 

๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ(new ์—ฐ์‚ฐ์ž ์ด์šฉ), ์กฐ๋ฆฝ(setter ํ•จ์ˆ˜ ๋˜๋Š” ์ƒ์„ฑ์ž ์ด์šฉ)ํ•˜๋Š” ๋ถ€๋ถ„์„ ์ฃผ์„ ์ฒ˜๋ฆฌํ•˜์˜€๋‹ค. ๊ฐ์ฒด ์ƒ์„ฑ-์กฐ๋ฆฝ์˜ ์—ญํ• ์„ ์ง€์‹œํ•˜๊ธฐ ์œ„ํ•œ ์ง€์‹œ์„œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด Spring Bean Configuration File์„ ์ƒ์„ฑํ•˜๋Š”๋ฐ, ์ด๋Š” xml ํŒŒ์ผ์ด๋‹ค. ์•ž์„œ ๋งํ–ˆ๋“ฏ์ด ์†Œ์Šค ์ฝ”๋“œ์˜ ์ˆ˜์ •์„ ์ตœ์†Œํ™”ํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ • ํŒŒ์ผ์„ ์™ธ๋ถ€(xml)๋กœ ๋นผ๋Š” ์ž‘์—…์ด๋‹ค.

 

 

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

	<!-- Exam exam = new NewlecExam(); //์ƒ์„ฑ -->
	<bean id="exam" class="spring.di.entity.NewlecExam"/>
	
	<!-- ExamConsole console = new GridExamConsole(exam); -->
	<bean id="console" class="spring.di.ui.GridExamConsole">
		<!-- console.setExam(exam); //์กฐ๋ฆฝ -->
		<property name="exam" ref="exam"></property> <!-- ๊ฐ’์ผ ๋•Œ๋Š” val, ์ฐธ์กฐ๊ฐ’์ผ๋•Œ๋Š” ref ์‚ฌ์šฉ -->
	</bean>

</beans>

 

bean์ด๋ž€ ์Šคํ”„๋ง์—์„œ ๊ฐ์ฒด๋ฅผ ์˜๋ฏธํ•œ๋‹ค. id๋ฅผ ํ†ตํ•ด ๋นˆ ๊ณ ์œ ์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•˜๊ณ , ์ด ์ด๋ฆ„์œผ๋กœ ๋นˆ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์กฐ๋ฆฝ ๊ณผ์ •์—์„œ property ํƒœ๊ทธ๋ฅผ ํ†ตํ•ด setter ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ์ ์€, ํ”„๋กœํผํ‹ฐ์˜ name์ด ๊ฐ€๋ฆฌํ‚ค๋Š” exam์€ setExam์ด๋ผ๋Š” ์„ธํ„ฐ ํ•จ์ˆ˜๋ผ๋Š” ๊ฒƒ์ด๋‹ค. setExam์„ ๋ฌต์‹œ์ ์œผ๋กœ exam์ด๋ผ๊ณ  ํ‘œ๊ธฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ์ฒด์— setExam์ด๋ผ๋Š” ์„ธํ„ฐ ํ•จ์ˆ˜๊ฐ€ ์—†๋‹ค๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚  ๊ฒƒ์ด๋‹ค. ๋˜ํ•œ ref๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒƒ์€ ์œ„์—์„œ ๋งŒ๋“  bean id="exam"์ด๋‹ค. ์œ„์—์„œ ์ƒ์„ฑํ•œ exam์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ์„ธํ„ฐ๋ฅผ ํ†ตํ•ด ์ฃผ์ž…ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.

 

(์ด ๋ถ€๋ถ„์—์„œ ์ž๊พธ ๋„ ํฌ์ธํ„ฐ ์—๋Ÿฌ๊ฐ€ ๋‚˜์„œ ํ•œ์ฐธ ๊ณ ์ƒํ–ˆ๋Š”๋ฐ, setExam ํ•จ์ˆ˜์— this.exam=exam์ด๋ผ๋Š” ์ฃผ์ž… ๋ถ€๋ถ„์ด ์—†์—ˆ๋‹ค. ์ด๊ฑธ ๋ชป๋ณด๊ณ  ํ•œ์ฐธ ์‚ฝ์งˆํ–ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค๋ฉด ์„ธํ„ฐ ํ•จ์ˆ˜๋กœ ์ฃผ์ž…ํ•˜๋Š” ๊ณผ์ •์— ์—๋Ÿฌ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ž˜ ์‚ดํŽด๋ณด์ž.)

 

=> <property name="exam" ref="exam"></property> ์ด ๋ถ€๋ถ„์ด '๋ถ€ํ’ˆ ์กฐ๋ฆฝ', ์ฆ‰ ์ฃผ์ž…์˜ ๊ณผ์ •์ด๋‹ค.

 

์ด๋ ‡๊ฒŒ ์ง€์‹œ์„œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์กฐ๋ฆฝํ•˜์˜€์œผ๋ฉด, ์ด๋Ÿฌํ•œ ์„ค์ • ๋ถ€๋ถ„์„ ๊ฐ์ฒดํ™”ํ•ด์„œ ๋‚˜์—๊ฒŒ ์ „๋‹ฌํ•ด ์ฃผ๋Š” ์กด์žฌ๊ฐ€ ํ•„์š”ํ•  ๊ฒƒ์ด๋‹ค. ApplicationContext ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๊ทธ ์—ญํ• ์„ ํ•˜๊ฒŒ ๋œ๋‹ค. ApplicationContext ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค ๊ฐ์ฒด, ์ด๊ฒƒ์ด ๋ฐ”๋กœ IoC ์ปจํ…Œ์ด๋„ˆ์ด๋‹ค. ๋ชจ๋“  ์Šคํ”„๋ง ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ์ตœ์†Œ ํ•˜๋‚˜ ์ด์ƒ์˜ IoC ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

 

Program.java์— ์ฝ”๋“œ ํ•œ ์ค„์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

 

ApplicationContext context = new ClassPathXmlApplicationContext("spring/di/setting.xml");
//src ์ดํ•˜์˜ ๊ฒฝ๋กœ ์ž…๋ ฅ

 

์ฝ”๋“œ์— ๋นจ๊ฐ„์ค„์ด ๋ณด์ด๋Š”๋ฐ, ์Šคํ”„๋ง์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ •์„ ์•„์ง ์•ˆ ํ•ด์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ”„๋กœ์ ํŠธ ์šฐํด๋ฆญ - Configure - Convert to Maven Project๋ฅผ ๋ˆ„๋ฅด๋ฉด ๋ฉ”์ด๋ธ ํ”„๋กœ์ ํŠธ๋กœ ๋ณ€๊ฒฝ์ด ๋˜๊ณ  pom.xml ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ๋‹ค. ๋””ํŽœ๋˜์‹œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ <dependencies> ํƒœ๊ทธ๋ฅผ ์—ด๊ณ , ๊ทธ ์•ˆ์— Maven Repository(mvnrepository.com/)์—์„œ ๋ณต์‚ฌํ•ด ์˜จ spring-context๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. 

 

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.newlecture</groupId>
  <artifactId>Spring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-context</artifactId>
	    <version>5.1.9.RELEASE</version>
	</dependency>
  </dependencies>
  
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

 

์‹ ๊ธฐํ•œ ์ ์€ spring-context๋งŒ ์ถ”๊ฐ€ํ–ˆ์„ ๋ฟ์ธ๋ฐ Dependency Hierarchy ํƒญ์„ ํด๋ฆญํ•˜์—ฌ ์‚ดํŽด๋ณด๋ฉด ๊ทธ์— ๊ด€๋ จ๋œ ๋‹ค๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋„ ์ถ”๊ฐ€๊ฐ€ ๋˜์—ˆ๋‹ค๋Š” ์ ์ด๋‹ค.

 

์ด์ œ ์ง€์‹œ์„œ ์ž‘์„ฑ, ์ง€์‹œ์„œ๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ-์กฐ๋ฆฝํ•  ์ค€๋น„๊ฐ€ ๋๋‚ฌ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ƒ์„ฑํ•œ ๊ฐ์ฒด๋ฅผ ๊บผ๋‚ด์˜ค๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

Program.java

public class Program {

	public static void main(String[] args) {

		ApplicationContext context = 
				new ClassPathXmlApplicationContext("spring/di/setting.xml");
		//src ์ดํ•˜์˜ ๊ฒฝ๋กœ๋ฅผ ์ž…๋ ฅ
		
		/** ๊ฐ์ฒด๋ฅผ ๊บผ๋‚ด๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• */
		//1. xml์— ๋ช…์‹œ๋œ id ์‚ฌ์šฉ
		ExamConsole console = (ExamConsole) context.getBean("console");
		//object ํ˜•์œผ๋กœ ๊บผ๋‚ด๊ธฐ ๋•Œ๋ฌธ์— ํ˜• ๋ณ€ํ™˜ ํ•„์š”
		
		//2. ํด๋ž˜์Šค๋ช… ์‚ฌ์šฉ
//		ExamConsole console = context.getBean(ExamConsole.class);
		//ํ˜• ๋ณ€ํ™˜์ด ํ•„์š”์—†์ด ๋•Œ๋ฌธ์— ๋” ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค
        
		console.print();		
	}

}

 

 

 

 

* DI์˜ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•

1. setter ์ด์šฉ

setter ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๊ธฐ ๋•Œ๋ฌธ์—, setter ํ•จ์ˆ˜๊ฐ€ ์„ ์–ธ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ๋ผ๋ฉด ๋‹น์—ฐํžˆ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค.

 

<bean id="exam" class="spring.di.entity.NewlecExam">
	<property name="kor" value="10"></property>
	<property name="eng" value="10"></property>
	<property name="math" value="10"></property>
	<property name="com">
    	<value="10"> <!-- ์ด๋ ‡๊ฒŒ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ -->
    </property>
</bean>	

 

 

 

2. ์ƒ์„ฑ์ž ์ด์šฉ

์ƒ์„ฑ์ž์˜ ์ˆœ์„œ๋ฅผ index์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ˜น์€ name ํƒœ๊ทธ๋กœ ์ปฌ๋Ÿผ๋ช…์„ ์ง€์ •ํ•˜๊ฑฐ๋‚˜, type ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

<bean id="exam" class="spring.di.entity.NewlecExam">
	<constructor-arg index="0" value="10"/>
	<constructor-arg index="1" value="10"/>
	<constructor-arg index="2" value="10"/>
	<constructor-arg index="3" value="10"/>
</bean>	

 

 

<bean id="exam" class="spring.di.entity.NewlecExam">
	<constructor-arg name="kor" value="10"/>
	<constructor-arg name="eng" value="10"/>
	<constructor-arg name="math" value="10"/>
	<constructor-arg name="com" value="10"/>
</bean>	

 

 

 

* ์ฝœ๋ ‰์…˜ ์ƒ์„ฑ๊ณผ ๋ชฉ๋ก DI

 

Program.java

List<Exam> exams = (List<Exam>) context.getBean("exams"); 

 

์ด์ฒ˜๋Ÿผ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ์ฒด๋ฅผ ๊บผ๋‚ด์˜ฌ ์ˆ˜๋„ ์žˆ๋‹ค. ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ ์‹œ xml ์ง€์‹œ์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ํ•œ๋‹ค.

 

 

setting.xml

<bean id="exams" class="java.util.ArrayList">
	<constructor-arg>
		<list>
			<bean class="spring.di.entity.NewlecExam" p:kor="1" p:eng="2" p:math="2" p:com="2">
			<!-- namespace์—์„œ p ์ฒดํฌ -->
			<ref bean="exam"/>
		</list>
	</constructor-arg>
</bean>

 

๋˜๋Š” 

 

<util:list id="exams" list-class="java.util.ArrayList">
	<bean class="spring.di.entity.NewlecExam" p:kor="1" p:eng="2" p:math="2" p:com="2"/>
	<ref bean="exam"/>
</util:list>

<!-- namespace์—์„œ util ์ฒดํฌ -->

 

p ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Namespaces ํƒญ์—์„œ p ํƒœ๊ทธ๋ฅผ ์ฒดํฌํ•ด์•ผ ํ•œ๋‹ค. (์ด ํƒœ๊ทธ๊ฐ€ ์•ˆ ๋ณด์ด๊ณ , Namespaces ํƒญ์ด ์•ˆ ๋ณด์—ฌ์„œ ์ดํด๋ฆฝ์Šค๋ฅผ ๋‹ค์‹œ ๊น”๊ณ  ๋ณ„ ์ง“์„ ๋‹ค ํ–ˆ์ง€๋งŒ.... ํ—ˆ๋ฌดํ•˜๊ฒŒ๋„ ๊ทธ๋ƒฅ ์ผ๋‹จ ์ฝ”๋“œ์— p ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์ €์žฅํ–ˆ๋”๋‹ˆ Namespaces์— ๋‹ค๋ฅธ ํƒœ๊ทธ๋“ค์ด ์ƒ๊ฒผ๋‹ค...)

 

 

 


๋‰ด๋ ‰์ฒ˜ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ์ •๋ฆฌํ•œ ๊ฒŒ์‹œ๊ธ€์ž…๋‹ˆ๋‹ค.

www.youtube.com/user/newlec1