위임하다, 파견하다. delegate
=> The client delegates to calls to another object the responsibility of providing its dependencies.
(이거 해석 질문? its는 client라고 하면 될까요? 즉, providing its dependencies에서 dependencies는 누구의 의존성인가요?)
2번 Car Factory에서
they inject technician, car, engine, seats, and so on. 이런 거를 제공해준다. =>dependency는 helper objects랑 같은 거라고 보면 된다.
아래 그림에서 Coach object는 "dependency"(의존성)을 가지고 있을 것이다. (dependency=helper objects)
내가 코치 오브젝트와 코치 오브젝트의 의존성을 build 하기 보다는,
스프링 프레임 워크와 스프링 팩토리가 그러한 일을 다 해줄 것이다.
3번에서 너가 자동차를 드라이빙 할 준비가 되었다면 사용할 오브젝트를 get 할 것이다.
코치가 FortuneService를 이용해서 daily fortunes 제공
그러고 다음 섹션에서는 auto-write되는 annotation에 대해 알아 볼 것이다.
package com.luv2code.springdemo;
public interface FortuneService {
public String getFortune();
}
package com.luv2code.springdemo;
public class HappyFortuneService {
public String getFortune() {
return "Today is your lucky day!";
}
}
HappyFortuneService가 FortuneService(인터페이스)를 implements 한다.
constructor of BaseballCoach를 정의하는 이유는 dependency 주입을 위한 것이다. => 이는 fortune service를 accept한다. 그에 따라 private로 선언한 fortuneService
BaseballCoach.java
package com.luv2code.springdemo;
public class BaseballCoach implements Coach {
private FortuneService fortuneService;
public BaseballCoach(FortuneService theFortuneService) {
fortuneService = theFortuneService;
}
}
dependency/helper를 정의한다.
<bean id="myFortuneService"
class="com.luv2code.springdemo.HappyFortuneService">
</bean>
id로 myFortuneService를 부르고, myFortuneService interface를 implements하는 클래스인 HappyFortuneService를 써준다.
실제로 dependency/helper를 "constructor injection"을 이용해 클래스에다 주입
<bean id="myCoach"
class="com.luv2code.springdemo.BaseballCoach">
<consturctor-arg ref="myFortuneService" />
</bean>
ref는 id인 myFortuneService
뒤에서 행하는 일-스프링은 object factory가 있어서 다음과 같은 일이 뒤에서 행한다.
Now, what'll happen behind the scenes is
that when Spring makes it to this line
of code in your config file,
they'll actually look at your baseball coach,
실제 BaseballCoach를 보고
they'll ball your baseball coach's constructor
너의 BaseballCoach의 생성자를 읽고(?)
and they'll pass in a reference to the my fortune service.
myFortuneService의 reference로 갈 것이다.
스프링은 오브젝트를 생성해야 하고 의존성 주입의 책무가 있다.
여기까지가 미리보기(Overview)