10-31 16:52
- Today
- Total
													Link
													
												
											
												
												
											
									개발하는 고라니
2021-06-07 Dependency 본문
반응형
    
    
    
  
B는 C를 사용하므로 C는 B의 부품
A는 B를 사용하므로 B는 A의 부품
Frame은 A를 사용하므로 Frame의 부품
즉, Dependency는 상대적이다.

왼쪽, 중앙 방법은 부픔(A)를 뺏다 꼇다 할 수 있는 방법이 아니므로 DI가 아니다.
오른쪽은 Setter, Constructor 로 Injection를 하는 방법이다.


스프링이 DI를 해준다. 직접 관리하는 것보다 속도는 느릴 수 있으나 변경이 유연하다
IoC Container

Dependency Injection의 방법
생성자 Injection
@Service
public class NoticeServiceImpl implements NoticeService{
    private final NoticeRepository repository;
    
    //생성자
    @Autowired
    public NoticeServiceImpl(NoticeRepository repository) {
        this.repository = repository;
    }
    ...
}# 장점
다른 초기화 로직이 등장하게 할 수 있다.
도중에 바뀔 염려가 없다.
Setter Injection
@Service
public class NoticeServiceImpl implements NoticeService{
    private final NoticeRepository repository;
    //Setter
    @Autowired
    public void setRepository(NoticeRepository repository) {
        this.repository = repository;
    }
    ....
}
Field Injection
@Service
public class NoticeServiceImpl implements NoticeService{
    //Field
    @Autowired
    private NoticeRepository repository;
    ...
}# 장점
편리하다.
반응형
    
    
    
  '잡동사니' 카테고리의 다른 글
| 첫 서비스 장애, 느낀점 (3) | 2022.01.16 | 
|---|---|
| 취업 준비를 마치며 (8) | 2021.11.02 | 
| Programmers의 선물 (4) | 2021.10.22 | 
| 국비교육을 마치며 (2) | 2021.07.17 | 
| 그룹 스터디 중 문제에 대한 풀이 (0) | 2021.03.19 | 
			  Comments
			
		
	
               
           
					
					
					
					
					
					
				 
								