์์ ์คํ๋ง ์์ด ์ง์ 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์ ๋ค๋ฅธ ํ๊ทธ๋ค์ด ์๊ฒผ๋ค...)
๋ด๋ ์ฒ ์คํ๋ง ํ๋ ์์ํฌ ๊ฐ์๋ฅผ ๋ฃ๊ณ ์ ๋ฆฌํ ๊ฒ์๊ธ์ ๋๋ค.
'๐ปStudy > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ด๋ ธํ ์ด์ ์ ์ด์ฉํ ๊ฐ์ฒด ์์ฑ(@Component) (0) | 2021.02.12 |
---|---|
์ด๋ ธํ ์ด์ ์ ์ด์ฉํ DI (@Autowired, @Qualifier) (0) | 2021.02.12 |
์คํ๋ง ์์ด DI ํด๋ณด๊ธฐ (0) | 2021.02.11 |
DI์ IoC ์ปจํ ์ด๋ (0) | 2021.02.11 |
์คํ๋ง ํ๋ ์์ํฌ ์๊ฐ (0) | 2021.02.11 |