1 ๋ถ„ ์†Œ์š”

DI๋ž€?

DI ๋Š” Dependency Injection์˜ ์•ฝ์ž๋กœ, ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…์„ ์˜๋ฏธํ•œ๋‹ค.
์˜์กด๊ด€๊ณ„๋ฅผ ์ฃผ์ž…ํ•จ์œผ๋กœ์จ ๊ฐ์ฒด๊ฐ„์˜ ์˜์กด๊ด€๊ณ„๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
์ด ๋•Œ, ์Šคํ”„๋ง์€ ๋Ÿฐํƒ€์ž„ ์‹œ์— ์‚ฌ์šฉํ•  ๊ฐ์ฒด๋“ค์˜ ์˜์กด๊ด€๊ณ„๋ฅผ ๋ถ€์—ฌํ•˜์—ฌ ๊ฐ์ฒด ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถ˜๋‹ค.

DI ์„ค์ • ๋ฐฉ์‹

DI๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐฉ์‹์€ XML, JAVA, ์• ๋…ธํ…Œ์ด์…˜ ์„ค์ •๋ฐฉ์‹์œผ๋กœ 3๊ฐ€์ง€ ๋ฐฉ์‹์ด ์žˆ๋‹ค.
์ด๊ฑด DI๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐฉ์‹์ด๊ณ , DI๋ฅผ ํ•˜๋Š” ๋ฐฉ์‹(์˜์กด๊ด€๊ณ„๋ฅผ ์ฃผ์ž…ํ•˜๋Š” ๋ฐฉ์‹)์—๋Š” ์ƒ์„ฑ์ž ๊ธฐ๋ฐ˜ ์ฃผ์ž…๊ณผ ์„ค์ •์ž ๊ธฐ๋ฐ˜ ์ฃผ์ž…์˜ 2๊ฐ€์ง€ ๋ฐฉ์‹์ด ์กด์žฌํ•œ๋‹ค.

XML ์„ค์ • ๋ฐฉ์‹

๊ฐ€์žฅ ์ „ํ†ต์ ์ธ DI ์„ค์ • ๋ฐฉ์‹์œผ๋กœ, XMLํŒŒ์ผ์„ ํ†ตํ•ด ๋นˆ์„ ์ •์˜ํ•˜๊ณ , ์˜์กด๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•œ๋‹ค.

[์˜ˆ์‹œ]

<bean id="myBean" class="com.example.MyBean">
    <constructor-arg ref="myDependency"/>
</bean>
<bean id="myDependency" class="com.example.MyDependency"/>

JAVA ๊ธฐ๋ฐ˜ ์„ค์ • ๋ฐฉ์‹

XML ์„ค์ • ์ฝ”๋“œ๋ฅผ Java ์ฝ”๋“œ๋กœ ๋Œ€์ฒดํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
@Configuration ์• ๋…ธํ…Œ์ด์…˜์œผ๋กœ ์„ค์ • ํด๋ž˜์Šค๋ฅผ ์ง€์ •ํ•˜๊ณ , @Bean ์• ๋…ธํ…Œ์ด์…˜์œผ๋กœ ๋นˆ์„ ์ •์˜ ๋ฐ ์˜์กด๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•œ๋‹ค.

[์˜ˆ์‹œ]

@Configuration
public class MyConfiguration {
  @Bean
  public MyBean myBean(MyDependency myDependency) {
    return new MyBean(myDependency);
  }
  
  @Bean
  public MyDependency myDependency() {
    return new MyDependency();
  }
}

์• ๋…ธํ…Œ์ด์…˜ ๋ฐฉ์‹

์• ๋…ธํ…Œ์ด์…˜ ๋ฐฉ์‹์€ ์• ๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ด ์˜์กด๊ด€๊ณ„๋ฅผ ์ฃผ์ž…ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
์ด ๋•Œ๋Š” <context:component-scan> ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•œ๋‹ค.

[ApplicationContext.xml]

<beans xmlns="~">
...
    <context:component-scan base-package="com.example.MyBean">
    </context:component-scan>
</beans>

XML ์„ค์ •๊ณผ JAVA ์„ค์ •์„ ๋Œ€์น˜ํ•ด๋ณด์ž.

์˜์กด๊ด€๊ณ„ ์ฃผ์ž…์—๋Š” ์ƒ์„ฑ์ž ๊ธฐ๋ฐ˜ ์ฃผ์ž… ๋ฐฉ์‹๊ณผ ์„ค์ •์ž ๊ธฐ๋ฐ˜ ์ฃผ์ž… ๋ฐฉ์‹์˜ 2๊ฐ€์ง€ ๋ฐฉ์‹์ด ์žˆ๋‹ค.

์ƒ์„ฑ์ž ๊ธฐ๋ฐ˜ ์ฃผ์ž…

[applicationContext.xml]

<bean id="memberDAO" class="org.korea.di.persistence.MemberDAOImpl"></bean>
<bean id="memberService" class="org.korea.di.service.MemberServiceImpl"></bean>
    <constructor-arg ref="memberDAO">
</bean>
  • ๋นˆ์€ ํ•ด๋‹น ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด์—์„œ ๊ฐ€์ ธ์˜ด(MemberDAO๊ฐ€ ์•„๋‹Œ MemberDAOImpl)
  • ์ƒ์„ฑ์ž ๊ธฐ๋ฐ˜ ์ฃผ์ž…์€ <consturctor-arg ref="memberDAO"/>์™€ ๊ฐ™์ด <constructor-arg/>ํƒœ๊ทธ๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค.

[MemberServiceImpl.java]

public class MemberServiceImpl implements MemberService {
    private MemberDAO memberDAO;
    // ์ƒ์„ฑ์ž ์ •์˜
    public MemberServiceImpl(MemberDAO memberDAO) {
        this.memberDAO = memberDAO;
    }
    ...
}

์„ค์ •์ž(setter) ๊ธฐ๋ฐ˜ ์ฃผ์ž…

[applicationContext.xml]

<bean id="memberDAO" class="org.tukorea.di.persistence.MemberDAOImpl"></bean>
<bean id="memberService" class="org.tukorea.di.service.MemberServiceImpl">
    <property name="memberDAO" ref="memberDAO"/>
</bean>
  • ์„ค์ •์ž ๊ธฐ๋ฐ˜ ์ฃผ์ž…์€ <property name="memberDAO" ref="memberDAO"/>์™€ ๊ฐ™์ด <property>ํƒœ๊ทธ์™€ name ์†์„ฑ์ด ์‚ฌ์šฉ๋œ๋‹ค.

[MemberService.java]

public class MemberServiceImpl implements MemberService {
    private MemberDAO memberDAO;
    // ์„ค์ •์ž ์ •์˜
    public setMemberDAO(MemberDAO memberDAO) {
        this.memberDAO = memberDAO;
    }
    ...
}

ํƒœ๊ทธ: ,

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ