Replies: 1 comment
-
1. 필드 주입@Autowired
private ReservationService reservationService;필드 주입은 간편하게 클래스의 필드에 직접 의존성을 주입할 수 있지만 몇 가지 단점이 존재합니다.
2. setter 주입 public void setReservationService(ReservationService reservationService) {
this.reservationService = reservationService;
}객체가 생성된 이후 setter를 통해 의존성을 주입 받습니다. 하지만 단점 또한 존재합니다.
3. 생성자 주입 public ReservationApiController(ReservationService reservationService) {
this.reservationService = reservationService;
}생성자 주입은 필드주입과 setter주입의 단점을 보안할 수 있습니다 생성자 주입은 객체 생성 시점에 강제적으로 의존성을 주입받기 때문에 즉 필드 주입은 주입 받으려는 객체를 먼저 만들고, 그 후에 의존하는 객체을 필드에 넣는 방식이고 그래서 생성자 주입은 생성자를 통해 의존하는 객체를 미리 생성하여 의존성을 주입하는 형태라 스프링 컨테이너의 도움 없이 테스트가 가능합니다
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
키워드
Beta Was this translation helpful? Give feedback.
All reactions