Inversion of Control
제어권이 뒤바꼈다고?
일반적인 (의존성에 대한) 제어권: “내가 사용할 의존성은 내가 만든다.”
class OwnerController {
private OwnerRepository repository = new OwnerRepository();
}
IoC: “내가 사용할 의존성을 누군가 알아서 주겠지”
- 내가 사용할 의존성의 타입(또는 인터페이스)만 맞으면 어떤거든 상관없다.
- 그래야 내 코드 테스트 하기도 편하지.
class OwnerController {
private OwnerRepository repo;
public OwnerController(OwnerRepository repo) {
this.repo = repo;
}
// repo를 사용합니다.
}
class OwnerControllerTest {
@Test
public void create() {
OwnerRepository repo = new OwnerRepository();
OwnerController controller = new OwnerController(repo);
}
}
Reference
Inversion of Control Containers and the Dependency Injection pattern
Explaining the Dependency Injection pattern, by contrasting it with Service Locator. The choice between them is less important than the principle of separating configuration from use.
martinfowler.com
내용정리
Dependency Injection(의존성 주입)
어떤 Instance를 생성할 때 필요한 객체등을 주입하는 것을 의존성 주입이라 하는데
통상적으로는 내부에서 생성하여 주입하게 된다. ex) 생성자, setter, field 등
IOC(Inversion of Control)
DI를 개발자가 신경써서 해왔다면 IOC는 SpringFramework의 힘을 빌려 DI등의 관리에서 벗어나게 해준다.
SpringFramework의 IOC Container에서 어노테이션 등을 지표로 객체 생성에 필요한 관리를 맡아서 해준다.
https://github.com/devjun63/spring-petclinic/commit/c43cc8077a7eabc11743a89a1a1b894f736542db
Inversion of Control basic · devjun63/spring-petclinic@c43cc80
Permalink This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. Browse files Inversion of Control basic Loading branch information Showing 3 changed files with 35 additions and 0 deletions. +14
github.com
'BackEnd > Spring & Springboot Study' 카테고리의 다른 글
예제로 배우는 스프링 프레임워크 입문 - Spring Bean (0) | 2021.11.26 |
---|---|
예제로 배우는 스프링 프레임워크 입문 - IOC Container (0) | 2021.11.25 |
예제로 배우는 스프링 프레임워크 입문 - 프로젝트 살펴보기 & 과제풀이 (0) | 2021.11.23 |
예제로 배우는 스프링 프레임워크 입문 (0) | 2021.11.23 |
[스프링 웹 개발 기초] - 06. 정적 컨텐츠 (0) | 2021.04.09 |