-
DI 주입 방법(3가지)SPRING 2020. 3. 23. 22:34
1. 생성자를 이용한 의존성 주입 방법
(스프링 버전 4.3이상에서는 생성자를 이용한 Bean 주입시 @Autowired 생략 가능)
-> 스프링 공식 문서에서 가장 추천하는 방식임.
12345public OwnerController(OwnerRepository clinicService, VisitRepository visits) {this.owners = clinicService;this.visits = visits;}http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter2. 필드에서 의존성 주입 방법
-> 실무에서는 보통 이러한 방식으로 사용
12@Autowiredprivate OwnerRepository owners;3. Setter로 의존성 주입 방법
12345@Autowiredpublic void setOwners(OwnerRepository owners) {this.owners = owners;}http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter'SPRING' 카테고리의 다른 글
Component와 ComponentScan 정리 (0) 2020.04.10 @Autowired 심화 학습(자료 : 백기선) (0) 2020.04.06 AOP (Aspect Oriented Programming) (자료: 백기선) (0) 2020.03.23 IOC 란? (자료 : 백기선) (0) 2020.03.22 ObjectMapper로 Map-> Json 데이터 변환하기 (0) 2020.03.22