반응형
05-14 05:47
Today
Total
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
관리 메뉴

개발하는 고라니

2021-06-07 Dependency 본문

잡동사니

2021-06-07 Dependency

조용한고라니 2021. 6. 7. 13:02
반응형

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
취업 준비를 마치며  (7) 2021.11.02
Programmers의 선물  (4) 2021.10.22
국비교육을 마치며  (2) 2021.07.17
그룹 스터디 중 문제에 대한 풀이  (0) 2021.03.19
Comments