DI(Dependency Injection) 미리보기

리팩토링 벨로그·2022년 9월 30일
0

위임하다, 파견하다. delegate

DI(Dependency Injection) 미리보기

Dependency Injection이란? Client가 XOR code, injector, 의존성의 제공하는 의무를 위임하는 것을 의미한다.

=> The client delegates to calls to another object the responsibility of providing its dependencies.
(이거 해석 질문? its는 client라고 하면 될까요? 즉, providing its dependencies에서 dependencies는 누구의 의존성인가요?)

Client가 의존성 제공의 책임을 다른 오브젝트에게 요청하는 것을 Dependency Injection.

2번 Car Factory에서
they inject technician, car, engine, seats, and so on. 이런 거를 제공해준다. =>dependency는 helper objects랑 같은 거라고 보면 된다.

아래 그림에서 Coach object는 "dependency"(의존성)을 가지고 있을 것이다. (dependency=helper objects)

내가 코치 오브젝트와 코치 오브젝트의 의존성을 build 하기 보다는,
스프링 프레임 워크와 스프링 팩토리가 그러한 일을 다 해줄 것이다.

3번에서 너가 자동차를 드라이빙 할 준비가 되었다면 사용할 오브젝트를 get 할 것이다.

Spring Container

Primary functions

Create and mange obejects(Inversion of Control)

Inject object's dependencies(Dependency Injection)

Demo Example

우리 Coach는 이미 daily workdout을 제공했어

지금 그래서 daily fortunes를 제공할 거야.

New helper:FortuneService //

코치가 FortuneService를 이용해서 daily fortunes 제공

이게 dependency(의존성)

즉, dependency = helper라고 할 수 있다.

Injection Types

2가지 type

Construction Injection

Setter Injection

그러고 다음 섹션에서는 auto-write되는 annotation에 대해 알아 볼 것이다.

Construction Injection의 경우

step 1. dependency interface와 class 정의

FortuneService.java
package com.luv2code.springdemo;

public interface FortuneService {

	public String getFortune();
}
HappyFortuneService.java
package com.luv2code.springdemo;

public class HappyFortuneService {
	public String getFortune() {
		return "Today is your lucky day!";
	}

}

HappyFortuneService가 FortuneService(인터페이스)를 implements 한다.

step 2. 클래스에서 injection<(의존성) 주입)> 을 위한 생성자 만들기

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;
	}

}

step 3. Spring config file에서 dependency injection(의존성 주입)을 주입해라.

dependency/helper를 정의한다.

<bean id="myFortuneService"
 		class="com.luv2code.springdemo.HappyFortuneService">	
</bean>

id로 myFortuneService를 부르고, myFortuneService interface를 implements하는 클래스인 HappyFortuneService를 써준다.

이게 dependency

실제로 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)

profile
글 다시 씁니다.

0개의 댓글