๐ก Dependency Injection(์์กด์ฑ ์ฃผ์ , ์ดํ DI)์ ๊ฐ์ฒด ๊ฐ์
์์กด ๊ด๊ณ๋ฅผ ๋น ์ค์ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ์ปจํ ์ด๋๊ฐ ์๋์ ์ผ๋ก ์ฐ๊ฒฐํด์ฃผ๋ ๊ฒ์ ๋งํ๋ค. ์ด๋ฅผ ํตํด ๊ฐ์ฒด ๊ฐ์๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ ์ ์์ผ๋ฉฐ ์ด๋ก ์ธํด์ ์ง๋ณด์์ฑ๊ณผ์ ์ฐ์ฑ์ด ์ฆ๊ฐํ๋ค.
public class A {
private B b = new B();
}
public class B {
}
class A ๊ฐ class B ๋ฅผ ํ๋๋ก ๊ฐ์ง ๋ A๋ B์ ์์กดํ๊ฒ ๋๋ค.
public class A {
/* ์ปดํ์ผ ์๋ฌ ๋ฐ์ */
private B b = new B();
}
/* ํด๋์ค๋ช
์ด B์์ NewB๋ก ๋ณ๊ฒฝ */
public class NewB {
}
B๊ฐ NewB ๋ก ๋ณ๊ฒฝ๋๋ฉด ํด๋น ํด๋์ค๋ฅผ ํ๋๋ก ๊ฐ์ง๊ณ ์๋ A ๋ ๋ณ๊ฒฝ๋์ด์ผ ํ ๊ฒ์ด๋ค. ์ด์ฒ๋ผ ๊ฐ์ฒด ๊ฐ์ ์์กด ๊ด๊ณ๊ฐ ๊ฐํ๊ฒ ๋ฌถ์ฌ์์ ๋ ๊ฒฐํฉ๋ ๊ฐ ๋๋ค๊ณ ํํํ๋ค. ์ด๋ก ์ธํด ์ ์ง๋ณด์์ฑ๊ณผ ์ ์ฐ์ฑ์ด ์ ํ๋ ์ ์๋ค. public class A {
/* ์์ ํ์
์ ํ๋๋ก ์ค์ */
private B b;
/* ์ง์ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ง ์๊ณ ์์ฑ์๋ฅผ ํตํด ์ ๋ฌ ๋ฐ์ */
public A(B b) {
this.b = b;
}
}
/* ์์ ํ์
์ผ๋ก ์ฌ์ฉํ ์ธํฐํ์ด์ค */
public interface B {
}
/* ์ธํฐํ์ด์ค์ ๊ตฌํ ํด๋์ค */
public class NewB implements B {
}
NewB ๋ผ๋ ๊ตฌ์ฒด์ ์ธ ๊ตฌํ์ฒด์ ํ์
์ ์ฌ์ฉํ๋ ๋์ B ๋ผ๋ ์์ ์ธํฐํ์ด์ค ํ์
์ผ๋ก ํ๋๋ฅผ ์ ์ธํ๋ค. ๋ํ ์ง์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๊ตฌ๋ฌธ๋ ์์ด์ก๊ณ ์์ฑ์๋ฅผ ํตํด ์ ๋ฌ ๋ฐ๋๋ก ํ๋ค. ์ด๋ ๊ฒ ๋ณ๊ฒฝํ๋ฉด ์ค์ ๋ก ์ฌ์ฉํ๋ ๊ตฌํ์ฒด๊ฐ NewB ์์ ๋ ๋ค๋ฅธ ํ์
์ผ๋ก ๋ณ๊ฒฝ ๋๋๋ผ๋ A ์ ์ฝ๋๋ ๋ณ๊ฒฝ ๋ ํ์๊ฐ ์๋ค. ์์กด ๊ด๊ณ๊ฐ ๋์จํด์ง๊ณ ๊ฒฐํฉ๋๊ฐ ๋ฎ์์ก๋ค๊ณ ํ ์ ์๋ค.์๋ ์ฝ๋๋ ํ
์คํธ์ ๊ณตํต์ ์ผ๋ก ์ฌ์ฉ ํ Account, PersonalAccount, MemberDTO ํด๋์ค์ด๋ค.
Account
public interface Account {
/* ์์ก ์กฐํ */
String getBalance();
/* ์
๊ธ */
String deposit(int money);
/* ์ถ๊ธ */
String withDraw(int money);
}
PersonalAccount
@Data
public class PersonalAccount implements Account {
private final int bankCode; //์ํ์ฝ๋
private final String accNo; //๊ณ์ข๋ฒํธ
private int balance; //์์ก
@Override
public String getBalance() {
return this.accNo + " ๊ณ์ข์ ํ์ฌ ์์ก์ " + this.balance + "์ ์
๋๋ค.";
}
@Override
public String deposit(int money) {
String str = "";
if(money >= 0) {
this.balance += money;
str = money + "์์ด ์
๊ธ๋์์ต๋๋ค.";
} else {
str = "๊ธ์ก์ ์๋ชป ์
๋ ฅํ์
จ์ต๋๋ค.";
}
return str;
}
@Override
public String withDraw(int money) {
String str = "";
if(this.balance >= money) {
this.balance -= money;
str = money + "์์ด ์ถ๊ธ๋์์ต๋๋ค.";
} else {
str = "์์ก์ด ๋ถ์กฑํฉ๋๋ค. ์์ก์ ํ์ธํด์ฃผ์ธ์.";
}
return str;
}
}
MemberDTO
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MemberDTO {
private int sequence; //ํ์๋ฒํธ
private String name; //์ด๋ฆ
private String phone; //ํด๋ํฐ๋ฒํธ
private String email; //์ด๋ฉ์ผ
private Account personalAccount; //๊ฐ์ธ๊ณ์ข
}
Account(๊ณ์ข) ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ PersonalAccount(๊ฐ์ธ๊ณ์ข) ํด๋์ค๊ฐ ์๊ณ MemberDTO ๋ Account ํ์
์ ํ๋๋ก ๊ฐ์ง๊ณ ์๋ค. (MemberDTO ๋ Account ํ์
์ ์์กดํ๋ค.)<bean id="account" class="com.ohgiraffers.common.PersonalAccount">
<constructor-arg index="0" value="20"/>
<constructor-arg index="1" value="110-234-567890"/>
</bean>
<bean id="member" class="com.ohgiraffers.common.MemberDTO">
<constructor-arg name="sequence" value="1"/>
<constructor-arg name="name" value="ํ๊ธธ๋"/>
<constructor-arg name="phone" value="010-1234-5678"/>
<constructor-arg name="email" value="hong123@gmail.com"/>
<constructor-arg name="personalAccount">
<ref bean="account"/>
</constructor-arg>
</bean>
bean ํ๊ทธ์ ํด๋์ค ์์ฑ์ ์ธํฐํ์ด์ค ํ์
์ด ์๋ ๊ตฌํ ํด๋์ค ํ์
์ผ๋ก ์์ฑํด์ผ ํ๋ค. ๋ฐ๋ผ์ account ๋น ๋ฑ๋ก ์ class ์์ฑ์๋ Account ์ธํฐํ์ด์ค๊ฐ ์๋ PersonalAccount ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ค.
MemberDTO ๋ Account ํ์
์ ์์กดํ๊ณ ์๊ธฐ ๋๋ฌธ์ member ๋น ๋ฑ๋ก ์ account ๋น์ ์ฐธ์กฐํ๋๋ก <constructor-arg> ํ๊ทธ์ ref ์์ฑ์ ์์ฑํ๋ค. ์์ฑ์๋ฅผ ํตํด ์์กด์ฑ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ์ฌ ์์กด์ฑ์ ์ฃผ์
ํ๊ณ ์์ผ๋ฏ๋ก ์ด๋ฅผ ์์ฑ์ ์ฃผ์
์ด๋ผ ํ๋ค.
/* XML ์ค์ ํ์ผ์ ๊ธฐ๋ฐ์ผ๋ก ApplicationContext ๊ฐ์ฒด ์์ฑ */
ApplicationContext context
= new GenericXmlApplicationContext("section01/xmlconfig/spring-context.xml");
/* MemberDTO ํ์
์ ๋น ๊ฐ์ ธ์ค๊ธฐ */
MemberDTO member = context.getBean(MemberDTO.class);
/* MemberDTO์ PersonalAccount ๊ฐ์ฒด ์ถ๋ ฅ */
System.out.println(member.getPersonalAccount());
/* 10000์ ์
๊ธ */
System.out.println(member.getPersonalAccount().deposit(10000));
/* ์์ก ์ถ๋ ฅ */
System.out.println(member.getPersonalAccount().getBalance());
/* 5000์ ์ถ๊ธ */
System.out.println(member.getPersonalAccount().withDraw(5000));
/* ์์ก ์ถ๋ ฅ */
System.out.println(member.getPersonalAccount().getBalance());
PersonalAccount(bankCode=20, accNo=110-234-567890,
balance=110-234-567890 ๊ณ์ข์ ํ์ฌ ์์ก์ 0์ ์
๋๋ค.)
110-234-567890 ๊ณ์ข์ ํ์ฌ ์์ก์ 0์ ์
๋๋ค.
10000์์ด ์
๊ธ๋์์ต๋๋ค.
110-234-567890 ๊ณ์ข์ ํ์ฌ ์์ก์ 10000์ ์
๋๋ค.
5000์์ด ์ถ๊ธ๋์์ต๋๋ค.
110-234-567890 ๊ณ์ข์ ํ์ฌ ์์ก์ 5000์ ์
๋๋ค.<bean id="account" class="com.ohgiraffers.common.PersonalAccount">
<constructor-arg index="0" value="20"/>
<constructor-arg index="1" value="110-234-567890"/>
</bean>
<bean id="member" class="com.ohgiraffers.common.MemberDTO">
<property name="sequence" value="1"/>
<property name="name" value="ํ๊ธธ๋"/>
<property name="phone" value="010-1234-5678"/>
<property name="email" value="hong123@gmail.com"/>
<property name="personalAccount" ref="account"/>
</bean>
<property> ํ๊ทธ๋ setter ๋ฉ์๋๋ฅผ ํตํด ๋น ๊ฐ์ฒด์ ๊ฐ์ ์ด๊ธฐํํ๋ ์ค์ ์ด๋ค.
name : ํ๋๋ช
value : ํ๋์ ๋ด์ ๊ฐref : ์ฐธ์กฐํ ๋น์ idMemberDTO ๋ Account ํ์
์ ์์กดํ๊ณ ์๊ธฐ ๋๋ฌธ์ member ๋น ๋ฑ๋ก ์ account ๋น์ ์ฐธ์กฐํ๋๋ก <property> ํ๊ทธ์ ref ์์ฑ์ ์์ฑํ๋ค. Setter ๋ฉ์๋๋ฅผ ํตํด ์์กด์ฑ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ์ฌ ์์กด์ฑ์ ์ฃผ์
ํ๊ณ ์์ผ๋ฏ๋ก ์ด๋ฅผ ์ธํฐ ์ฃผ์
์ด๋ผ ํ๋ค.
PersonalAccount(bankCode=20, accNo=110-234-567890,
balance=110-234-567890 ๊ณ์ข์ ํ์ฌ ์์ก์ 0์ ์
๋๋ค.)
110-234-567890 ๊ณ์ข์ ํ์ฌ ์์ก์ 0์ ์
๋๋ค.
10000์์ด ์
๊ธ๋์์ต๋๋ค.
110-234-567890 ๊ณ์ข์ ํ์ฌ ์์ก์ 10000์ ์
๋๋ค.
5000์์ด ์ถ๊ธ๋์์ต๋๋ค.
110-234-567890 ๊ณ์ข์ ํ์ฌ ์์ก์ 5000์ ์
๋๋ค.@Bean
public Account accountGenerator() {
return new PersonalAccount(20, "110-234-567890");
}
@Bean
public MemberDTO memberGenerator() {
/* MemberDTO ์์ฑ์๋ฅผ ํตํด Account๋ฅผ ์์ฑํ๋ ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ฆฌํด ๊ฐ์ ์ ๋ฌํ์ฌ bean์ ์กฐ๋ฆฝํ ์ ์๋ค. */
return new MemberDTO(1, "ํ๊ธธ๋", "010-1234-5678", "hong123@gmail.com", accountGenerator());
}
MemberDTO ๋ Account ํ์
์ ์์กดํ๊ณ ์๊ธฐ ๋๋ฌธ์ memberGenerator ๋น ๋ฑ๋ก ์ accountGenerator ๋น์ ์ฐธ์กฐํ๋๋ก MemberDTO ์์ฑ์์ ์ธ์๋ก accountGenerator ๋ฉ์๋ ํธ์ถ์ ๊ฒฐ๊ณผ(PersonalAccount bean ๊ฐ์ฒด)๋ฅผ ์ ๋ฌํ๋ค. ์์ฑ์๋ฅผ ํตํด ์์กด์ฑ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ์ฌ ์์กด์ฑ์ ์ฃผ์
ํ๊ณ ์์ผ๋ฏ๋ก ์ด๋ฅผ ์์ฑ์ ์ฃผ์
์ด๋ผ ํ๋ค./* Java ์ค์ ํ์ผ์ ๊ธฐ๋ฐ์ผ๋ก ApplicationContext ๊ฐ์ฒด ์์ฑ */
ApplicationContext context
= new AnnotationConfigApplicationContext(ContextConfiguration.class);
/* MemberDTO ํ์
์ ๋น ๊ฐ์ ธ์ค๊ธฐ */
MemberDTO member = context.getBean(MemberDTO.class);
/* MemberDTO์ PersonalAccount ๊ฐ์ฒด ์ถ๋ ฅ */
System.out.println(member.getPersonalAccount());
/* 10000์ ์
๊ธ */
System.out.println(member.getPersonalAccount().deposit(10000));
/* ์์ก ์ถ๋ ฅ */
System.out.println(member.getPersonalAccount().getBalance());
/* 5000์ ์ถ๊ธ */
System.out.println(member.getPersonalAccount().withDraw(5000));
/* ์์ก ์ถ๋ ฅ */
System.out.println(member.getPersonalAccount().getBalance());
PersonalAccount(bankCode=20, accNo=110-234-567890,
balance=110-234-567890 ๊ณ์ข์ ํ์ฌ ์์ก์ 0์ ์
๋๋ค.)
110-234-567890 ๊ณ์ข์ ํ์ฌ ์์ก์ 0์ ์
๋๋ค.
10000์์ด ์
๊ธ๋์์ต๋๋ค.
110-234-567890 ๊ณ์ข์ ํ์ฌ ์์ก์ 10000์ ์
๋๋ค.
5000์์ด ์ถ๊ธ๋์์ต๋๋ค.
110-234-567890 ๊ณ์ข์ ํ์ฌ ์์ก์ 5000์ ์
๋๋ค.@Bean
public Account accountGenerator() {
return new PersonalAccount(20, "110-234-567890");
}
@Bean
public MemberDTO memberGenerator() {
MemberDTO member = new MemberDTO();
member.setSequence(1);
member.setName("ํ๊ธธ๋");
member.setPhone("010-1234-5678");
member.setEmail("hong123@gmail.com");
/* setter๋ฅผ ํตํด Account๋ฅผ ์์ฑํ๋ ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ฆฌํด ๊ฐ์ ์ ๋ฌํ์ฌ bean์ ์กฐ๋ฆฝํ ์ ์๋ค. */
member.setPersonalAccount(accountGenerator());
return member;
}
MemberDTO ๋ Account ํ์
์ ์์กดํ๊ณ ์๊ธฐ ๋๋ฌธ์ memberGenerator ๋น ๋ฑ๋ก ์ accountGenerator ๋น์ ์ฐธ์กฐํ๋๋ก setPersonalAccount ๋ฉ์๋์ ์ธ์๋ก accountGenerator ๋ฉ์๋ ํธ์ถ์ ๊ฒฐ๊ณผ(PersonalAccount bean ๊ฐ์ฒด)๋ฅผ ์ ๋ฌํ๋ค. setter๋ฅผ ํตํด ์์กด์ฑ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ์ฌ ์์กด์ฑ์ ์ฃผ์
ํ๊ณ ์์ผ๋ฏ๋ก ์ด๋ฅผ ์ธํฐ ์ฃผ์
์ด๋ผ ํ๋ค.
์คํ ๊ฒฐ๊ณผ
PersonalAccount(bankCode=20, accNo=110-234-567890,
balance=110-234-567890 ๊ณ์ข์ ํ์ฌ ์์ก์ 0์ ์
๋๋ค.)
110-234-567890 ๊ณ์ข์ ํ์ฌ ์์ก์ 0์ ์
๋๋ค.
10000์์ด ์
๊ธ๋์์ต๋๋ค.
110-234-567890 ๊ณ์ข์ ํ์ฌ ์์ก์ 10000์ ์
๋๋ค.
5000์์ด ์ถ๊ธ๋์์ต๋๋ค.
110-234-567890 ๊ณ์ข์ ํ์ฌ ์์ก์ 5000์ ์
๋๋ค.
DI๋ ๊ฐ์ฒด ๊ฐ์ ์์กด ๊ด๊ณ๋ฅผ ๋น ์ค์ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ์ปจํ
์ด๋๊ฐ ์๋์ ์ผ๋ก ์ฐ๊ฒฐํด์ฃผ๋ ๊ฒ์ด๋ค. ์ด๋ฅผ ํตํด ๊ฐ์ฒด ๊ฐ์ ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ ์ ์์ผ๋ฉฐ ์ด๋ก ์ธํด ์ ์ง๋ณด์์ฑ๊ณผ ์ ์ฐ์ฑ์ด ์ฆ๊ฐํ๋ค.
XML ๋น ์ค์ ์์๋ <constructor-args> ๋๋ <property> ํ๊ทธ์ ref ์์ฑ์ ์์กด์ฑ ์ฃผ์
ํ bean์ ์ด๋ฆ์ ์ค์ ํ๋ค.
Java ๋น ์ค์ ์์๋ ์์ฑ์, setter ๋ฉ์๋์ ์ธ์ ๊ฐ์ผ๋ก ์์กด์ฑ ์ฃผ์
ํ bean์ ๋ฉ์๋ ํธ์ถ ๋ฐํ ๊ฐ์ ์ ๋ฌํ๋ค.