Categories

Tags

IoC Concept

객체의 생성, 생명주기 관리등의 객체 제어권을 컨테이너가 갖는 것

public class Controller {
  private TestService testService = new TestService();
  ...
}

public class Controller {
  public Controller(TestErvice testService) {
    this.testService = testService;
  }
}
  • 객체 생성을 직접 하지 않고, 외부의 어디선가 받아오기 때문에 IoC라 부르는 것
  • TODO : 지금까지 써오던 방식이 상당히 편협하고 일부 잘못된 부분이 있는 것 같음. 다양한 예제들을 접해봐야 함

DI - Dependency Injection

Spring 환경의 IoC 패턴에서 객체를 연결해주는 것.
일반적으로 인터페이스를 통해 구현체와 사용주체를 연결해줌.
순환참조가 발생하지 않게 구조를 잡는 것이 중요함.

@Autowired Injection
class Controller {
  @Autowired
  private TestService testService;
}
Setter Injection
class Controller {
  private TestService testService;
  @Autowired
  public void setTestService(TestService testService) {
    this.testService = testService;
  }
}
Constructor Injection
class Controller {
  private TestService testService;
  public Controller (TestService testService, ... ) {
    this.testService = testService;
    ...
  }
}