# 2022.12.05

## 할일 및 한일&#x20;

* [x] 네트워크 시험 대비&#x20;
* [x] 스프링 강의 수강 및 정리&#x20;

## 경험 및 배움&#x20;

### 스프링 강의 수강 및 정리&#x20;

오늘은 다양한 의존 관계 주입 방법을 알아보고, 장단점등을 학습해보았다.

의존 관계를 주입하는 방법에는 **생성자를 통한 주입, 수정자(setter) 주입, 필드 주입, 일반 메서드 주입**이 있다. 보통 생성자를 통한 주입 방법을 제일 많이 권장하고, 쓰인다고 한다.

그 이유는 여러가지이다. 첫 번째로는 생성자를 통해 의존 관계를 주입하면 **불변의 특징을 갖고 있어서 외부에서 쉽게 변경하지 않게 설계**할 수 있다.

```java
@Autowired // 생성자가 한 개일 경우 생략 가능
public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy discountPolicy) {
    this.memberRepository = memberRepository;
    this.discountPolicy = discountPolicy;
}
```

예를 들어 수정자 주입을 사용하면, setter 메서드를 `public` 으로 열어두어야 하기 때문에, 외부에서 쉽게 변경할 수 있고 좋은 설계 방법이 되지 않는다.

또한 생성자에서 혹시라도 **개발자의 실수로 인해 값이 누락되거나, 오류가 있으면 컴파일 시점에 막아줘서 보다 안전한 개발이 가능하다.**

또한 필드 주입, 그리고 일반 메서드 주입은 일반적으로는 잘 사용하지 않고, 테스트 케이스를 작성하는 경우와 같은 특별한 경우가 아닌 때에는 사용하지 말라고 하였다.

이렇게 의존 관계 방법에 대해 자세히 듣고 나니 지금까지 왜 생성자 주입을 사용했는지 바로 이해갔던 것 같다.

## 개선 및 목표&#x20;

* 내일은 이동통신 프로토콜 및 데이터 베이스 시험 대비를 하자!&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jinwon-yoon.gitbook.io/jinonys-blog/daily-log/2022.12.05.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
