220827 DI(의존성 주입, Dependency Injection)

Jongleee·2022년 8월 27일
1

TIL

목록 보기
39/576

DI(의존성 주입, Dependency Injection)

1. Field Injection(필드 주입)

  • @Autowired 어노테이션을 사용
  • 비권장
    • 단일 책임(SRP)의 원칙 위반
    • 숨은 의존성
    • 불변성(Immutability) X
    • 순환 의존성의 가능성

2. Setter Injection(수정자 주입)

@Component
public class SampleController {
    private SampleService sampleService;
 
    @Autowired
    public void setSampleService(SampleService sampleService) {
        this.sampleService = sampleService;
    }
}

3. Constructor Injection(생성자 주입)

	private final SampleService sampleService;

의 형태로 사용함

1개의 댓글

comment-user-thumbnail
2022년 9월 1일

?

답글 달기