DI(Dependency injection)๋ ํด๋์ค๊ฐ ์์ ์ด ํ์ํ ์์กด ๊ฐ์ฒด(์ฆ, ๋ค๋ฅธ ํด๋์ค์ ์ธ์คํด์ค)๋ฅผ ์ค์ค๋ก ์์ฑํ์ง ์๊ณ ์ธ๋ถ์์ ์ ๊ณต๋ฐ๋ ๋ฐฉ์์ ๋๋ค. ์ฆ, ์์กด์ฑ(๋ค๋ฅธ ๊ฐ์ฒด)์ ์ฃผ์ ํด์ค์ผ๋ก์จ ๊ฐ์ฒด ๊ฐ์ ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ๊ณ , ๊ฐ์ฒด์ ์์ฑ๊ณผ ๊ด๋ฆฌ์ ์ฑ ์์ ์ธ๋ถ์ ์์ํฉ๋๋ค.
1. ์คํ๋ง ๋ถํธ์์ DI ์ค์ ๊ธฐ๋ณธ ๊ตฌ์กฐ
์คํ๋ง ๋ถํธ์์ DI๋ฅผ ์ฌ์ฉํ๋ ๊ณผ์ ์ ํฌ๊ฒ ๋ ๊ฐ์ง ์ฃผ์ ๋จ๊ณ๋ก ๋๋ ์ ์์ต๋๋ค:
- ์ปดํฌ๋ํธ ์ค์บ: @Component ๋ฐ ๊ทธ ํ์ ์ด๋ ธํ ์ด์ (@Service, @Repository, @Controller ๋ฑ)์ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ์คํ๋ง ์ปจํ ์ด๋์ ๋ฑ๋ก.
- ์๋ ์์กด์ฑ ์ฃผ์ : @Autowired๋ฅผ ์ฌ์ฉํ์ฌ ์์กด์ฑ ๊ฐ์ฒด๋ฅผ ์ฃผ์ ๋ฐ๊ธฐ.
์คํ๋ง ๋ถํธ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ด๋ ธํ ์ด์ ๊ธฐ๋ฐ์ ์ค์ ์ ์ฌ์ฉํ๋ฉฐ, ์๋ ์ปดํฌ๋ํธ ์ค์บ์ด ํ์ฑํ๋์ด ์๊ธฐ ๋๋ฌธ์, ํน๋ณํ ์ค์ ์์ด๋ ๋น ๋ฑ๋ก ๋ฐ ์์กด์ฑ ์ฃผ์ ์ด ๊ฐ๋ฅํฉ๋๋ค.
2. ์์กด์ฑ ์ฃผ์ (DI) ๋ ๊ฐ์ง ๋ฐฉ๋ฒ
(1) ์์ฑ์ ์ฃผ์ (Constructor Injection)
์์ฑ์ ์ฃผ์ ์ ์์กดํ๋ ๊ฐ์ฒด๊ฐ ์์ฑ์ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ๋๋ ๋ฐฉ์์ ๋๋ค. ์ด๋ ๋ถ๋ณ ๊ฐ์ฒด๋ฅผ ๋ณด์ฅํ๊ณ , ์์กด์ฑ์ด ์์ผ๋ฉด ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์๊ธฐ ๋๋ฌธ์ ์ปดํ์ผ ํ์์ ์์กด์ฑ์ ๊ฐ์ ๋ก ํ์ธํ ์ ์์ต๋๋ค.
@Component
public class MyService {
private final MyRepository myRepository;
// ์์ฑ์ ์ฃผ์
@Autowired
public MyService(MyRepository myRepository) {
this.myRepository = myRepository;
}
public void performService() {
myRepository.doSomething();
}
}
@Component
public class MyRepository {
public void doSomething() {
System.out.println("Doing something!");
}
}
- @Autowired: ์์ฑ์์ @Autowired ์ ๋ํ ์ด์ ์ ๋ถ์ฌ ์คํ๋ง์ด ์๋์ผ๋ก ์์กด์ฑ ๊ฐ์ฒด๋ฅผ ์ฃผ์ ํ๋๋ก ํฉ๋๋ค. ์์ฑ์๊ฐ ํ๋์ผ ๊ฒฝ์ฐ @Autowired๋ ์๋ตํ ์ ์์ต๋๋ค.
(2) ์ธํฐ ์ฃผ์ (Setter Injection)
์ธํฐ ์ฃผ์ ์ ๊ฐ์ฒด์ ์ธํฐ ๋ฉ์๋๋ฅผ ํตํด ์์กด์ฑ์ ์ฃผ์ ํ๋ ๋ฐฉ์์ ๋๋ค. ์ด ๋ฐฉ์์ ๊ฐ์ฒด์ ์ํ๋ฅผ ์์ ํ ์ ์๋ ์ ์ฐ์ฑ์ ์ ๊ณตํ์ง๋ง, ๊ฐ์ฒด๊ฐ ์์ฑ๋ ํ ์์กด์ฑ์ ์ฃผ์ ํ ์ ์๊ธฐ ๋๋ฌธ์ ๋ถ๋ณ์ฑ์ ๋ณด์ฅํ ์ ์์ต๋๋ค.
@Component
public class MyService {
private MyRepository myRepository;
// ์ธํฐ ์ฃผ์
@Autowired
public void setMyRepository(MyRepository myRepository) {
this.myRepository = myRepository;
}
public void performService() {
myRepository.doSomething();
}
}
3. ์์ฑ์ ์ฃผ์ ์ ๊ถ์ฅํ๋ ์ด์
์์ฑ์ ์ฃผ์ ์ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ถ๋ณ์ฑ์ ๋ณด์ฅํ๊ณ ํ ์คํธ ์ฉ์ด์ฑ์ ๋์ด๊ธฐ ๋๋ฌธ์ ๊ฐ์ฅ ๊ถ์ฅ๋ฉ๋๋ค.
4. Lombok์์ ์ ๊ณตํ๋ ์์ฑ์ ๊ด๋ จ ์ด๋ ธํ ์ด์
@AllArgsConstructor , @RequiredArgsConstructor ์ฐจ์ด
(1) @AllArgsConstructor
@AllArgsConstructor๋ ํด๋์ค์ ์ ์๋ ๋ชจ๋ ํ๋๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๊ฐ๋ ์์ฑ์๋ฅผ ์๋์ผ๋ก ์์ฑํ๋ Lombok ์ด๋ ธํ ์ด์ ์ ๋๋ค. ์ฆ, ๋ชจ๋ ํ๋๋ฅผ ์ฃผ์ ํ ์ ์๋ ์์ฑ์๋ฅผ ๋ง๋ญ๋๋ค.
- ๋ชจ๋ ํ๋๊ฐ ์ฃผ์ ๊ฐ๋ฅํ ์์ฑ์๊ฐ ๋ง๋ค์ด์ง๋๋ค.
- **final**์ด ๋ถ์ ํ๋๋ ํฌํจ๋๋ฉฐ, ํ๋์ ํ์ ์ ๊ด๊ณ์์ด ๋ชจ๋ ํ๋๋ฅผ ์์ฑ์ ๋งค๊ฐ๋ณ์๋ก ์ฌ์ฉํฉ๋๋ค.
import lombok.AllArgsConstructor;
@AllArgsConstructor
public class MyService {
private final MyRepository myRepository;
private MyOtherRepository myOtherRepository;
public void performService() {
myRepository.doSomething();
myOtherRepository.doSomethingElse();
}
}
final์ด ๋ถ๋ ์๋ถ๋ ์๊ด ์์. @AllArgsConstructor๋ ๋ชจ๋ ํ๋ ์์ฑ์ ์ฃผ์
myRepository์ myOtherRepository ์์ฑ์ ์ฃผ์ ๋จ
(2) @RequiredArgsConstructor
@RequiredArgsConstructor๋ final ํ๋์ @NonNull ์ด๋ ธํ ์ด์ ์ด ๋ถ์ ํ๋์ ๋ํด์๋ง ์์ฑ์๋ฅผ ์๋์ผ๋ก ์์ฑํ๋ Lombok ์ด๋ ธํ ์ด์ ์ ๋๋ค.
- final ํ๋ ๋๋ **@NonNull**์ด ๋ถ์ ํ๋๋ค๋ง ์์ฑ์ ๋งค๊ฐ๋ณ์๋ก ํฌํจ๋ฉ๋๋ค.
- ์ฆ, ์ฃผ์ ์ด ํ์ํ ํ๋๋ค๋ง ์์ฑ์์ ๋งค๊ฐ๋ณ์๋ก ํฌํจ๋๋ฉฐ, final ํ๋๊ฐ ์๋ @NonNull ํ๋๋ NULL์ด ์๋์ด์ผ ํ๋ค๋ ์ ์ฝ์ ์ถ๊ฐํ ๋ ์ฌ์ฉ๋ฉ๋๋ค.
import lombok.RequiredArgsConstructor;
import lombok.NonNull;
@RequiredArgsConstructor
public class MyService {
private final MyRepository myRepository; // final ํ๋
private @NonNull MyOtherRepository myOtherRepository; // @NonNull ํ๋
private SubRepository subRepository;
public void performService() {
myRepository.doSomething();
}
}
myRepository์ myOtherRepository ๋ง ์์ฑ์ ์ฃผ์ ๋๊ณ , subRepository๋ ์์ฑ์์ ํฌํจ๋์ง ์์ผ๋ฉฐ ์ฃผ์ ๋ ์๋จ.
์ฐจ์ด์ ์ ๋ฆฌ
| ํน์ง | @AllArgsConstructor | @RequiredArgsConstructor | |
| ์์ฑ์์ ํฌํจ๋๋ ํ๋ | ํด๋์ค์ ๋ชจ๋ ํ๋ (๋จ, final ํ๋๋ ๋ฐ๋์ ์ฃผ์ ) | final ํ๋์ **@NonNull**์ด ๋ถ์ ํ๋๋ค๋ง ํฌํจ | |
| ํ๋ ์ ์ฝ | ํ๋๊ฐ final์ธ์ง ์ฌ๋ถ์ ๊ด๊ณ์์ด ๋ชจ๋ ํ๋๋ฅผ ์์ฑ์์ ํฌํจ | final์ด๊ฑฐ๋ @NonNull ์ด๋ ธํ ์ด์ ์ด ๋ถ์ ํ๋๋ง ํฌํจ | |
| ์ฌ์ฉ ์ | ๋ชจ๋ ํ๋๋ฅผ ๋งค๊ฐ๋ณ์๋ก ์ฌ์ฉํ๋ ์์ฑ์๋ฅผ ์์ฑ | ์ฃผ์ ์ด ํ์ํ ํ๋๋ง ๋งค๊ฐ๋ณ์๋ก ์ฌ์ฉํ๋ ์์ฑ์๋ฅผ ์์ฑ |
๊ฒฐ๋ก
- @AllArgsConstructor๋ ๋ชจ๋ ํ๋๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๊ฐ๋ ์์ฑ์๋ฅผ ์์ฑํ๋ฉฐ, ์ฃผ๋ก ๋ชจ๋ ํ๋๋ฅผ ์ฃผ์ ํด์ผ ํ ๋ ์ฌ์ฉ๋ฉ๋๋ค.
- @RequiredArgsConstructor๋ final ํ๋ ๋๋ **@NonNull**์ด ๋ถ์ ํ๋์ ๋ํด์๋ง ์์ฑ์๋ฅผ ์์ฑํ๋ฉฐ, ์ฃผ๋ก ์ฃผ์ ์ด ๋ฐ๋์ ํ์ํ ํ๋๋ง ์ฃผ์ ํ๊ณ ์ถ์ ๋ ์ฌ์ฉ๋ฉ๋๋ค.
'SpringBoot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [SpringBoot] RESTful API (0) | 2025.01.14 |
|---|---|
| [SpringBoot] ์ฟผ๋ฆฌ์คํธ๋ง(Query String)๊ณผ ์ฃผ์๋ณ์๋งคํ(Path variable) (0) | 2025.01.14 |
| [SpringBoot] maven VS gradle (0) | 2025.01.12 |
| [SpringBoot] servlet-context.xml, root-context.xml, pom.xml, web.xml ์ฐจ์ด์ (0) | 2025.01.12 |
| [SpringBoot] @LoginUser ์ด๋ ธํ ์ด์ (0) | 2024.10.10 |