2021.08.17 TIL

새롭게 배운 것 Done

운동

독서

알고리즘

코딩

IoC(Inversion of Control) - 제어의 역전

class VoucherController {
  private VoucherRepository repository = new VoucherRepository();
}
class VoucherController {
  private VoucherRepository repository;
  
  public VoucherController(VoucherRepository repository) {
    this.repository = repository;
  }

  ///

}

class VoucherControllerTest {
  @Test
  public void create() {
    VoucherRepository repository = new VoucherRepository();
    VoucherController voucherController = new VoucherController(repository)
  }
}

Spring IoC Container

image

Bean이란?

의존성 주입(Dependecy Injection)

public class MemberRegisterService {
  private MemberDao memberDao;
  
  public MemberRegisterService(MemberDao memberDao) {
    this.memberDao = memberDao;
  }
}

//의존객체를 생성자를 통해 주입
MemberDao dao = new MemberDao();
MemberRegisterService service = new MemberRegisterService(dao);

@Autowired / @Injection를 어디에 붙일까? - DI 방식

public class MemberRegisterService {
  private final MemberDao memberDao;
  
  public MemberRegisterService(MemberDao memberDao) {
    this.memberDao = memberDao;
  }
}
@Autowired
private MemberDao memberDao;
public class MemberRegisterService {
  private MemberDao memberDao;
  
  @Autowired
  public void setMemberRegisterService(MemberDao memberDao) {
    this.memberDao = memberDao;
  }
}

참고: 책 - 스프링5 프로그래밍 입문 - 최범균 강의 - 예제로 배우는 스프링 입문 - 백기선



깨달은 점 FEELING