Field Injection(ํ๋ ์ฃผ์ )
๋ณ์ ์ ์ธ๋ถ์ @Autowired Annotation์ ๋ถ์ธ๋ค.
@Component
public class SampleController {
@Autowired
private SampleService sampleService;
}
ํ๋์ฃผ์ ์ ์ฌ์ฉํ๋ฉด ์๋๋ ์ด์
1. ๋จ์ผ ์ฑ ์(SRP)์ ์์น ์๋ฐ
์์กด์ฑ์ ์ฃผ์
ํ๊ธฐ๊ฐ ์ฝ๋ค.
@Autowired ์ ์ธ ์๋ ๊ฐ์ ์ ํ ์์ด ๋ฌดํ์ ์ถ๊ฐํ ์ ์๋ค. Constructor์ parameter๊ฐ ๋ง์์ง๊ณผ ๋์์ ํ๋์ Class๊ฐ ๋ง์ ์ฑ
์์ ๋ ์๋๋ค๋ ๊ฑธ ์๊ฒ๋๋ค.
2. ์์กด์ฑ์ด ์จ๋๋ค.
DI Container๋ฅผ ์ฌ์ฉํ๋ค๋ ๊ฒ์ Class๊ฐ ์์ ์ ์์กด์ฑ๋ง ์ฑ ์์ง๋ค๋๊ฒ ์๋๋ผ ์ ๊ณต๋ ์์กด์ฑ ๋ํ ์ฑ ์์ง๋ค. ๊ทธ๋์ Class๊ฐ ์ด๋ค ์์กด์ฑ์ ์ฑ ์์ง์ง ์์ ๋, ๋ฉ์๋๋ ์์ฑ์๋ฅผ ํตํด(Setter๋ Constructor) ํ์คํ ์ปค๋ฎค๋์ผ์ด์ ์ด ๋์ด์ผํ๋ค.
ํ์ง๋ง Field Injection์ ์จ์ ์์กด์ฑ๋ง ์ ๊ณตํด์ค๋ค.
3. DI container์ ๊ฒฐํฉ์ฑ๊ณผ ํ ์คํธ ์ฉ์ด์ฑ
DI Framework์ ํต์ฌ ์์ด๋์ด๋ ๊ด๋ฆฌ๋๋ Class๊ฐ DI Container์ ์์กด์ฑ์ด ์์ด์ผ ํ๋ค.
DI Container ์์ด๋ Unit Test์์ Instanceํ ์ํฌ ์ ์๊ณ , ๊ฐ๊ฐ ๋๋์ด์ ํ ์คํธ๋ ํ ์ ์๋ค. Container์ ๊ฒฐํฉ์ฑ์ด ์๋ค๋ฉด ๊ด๋ฆฌํ๊ฑฐ๋ ๊ด๋ฆฌํ์ง ์๋ Class๋ฅผ ์ฌ์ฉํ ์ ์๊ณ , ์ฌ์ง์ด ๋ค๋ฅธ DI Container๋ก ์ ํํ ์ ์๋ค.
ํ์ง๋ง, Field Injection์ ์ฌ์ฉํ๋ฉด ํ์ํ ์์กด์ฑ์ ๊ฐ์ง Class๋ฅผ ๊ณง๋ฐ๋ก Instanceํ ์ํฌ ์ ์๋ค.
4. ๋ถ๋ณ์ฑ
Constructor Injection๊ณผ ๋ค๋ฅด๊ฒ Field Injection์ final์ ์ ์ธํ ์ ์๋ค. ๊ทธ๋์ ๊ฐ์ฒด๊ฐ ๋ณํ ์ ์๋ค.
5. ์ํ ์์กด์ฑ
Constructor Injection์์ ์ํ ์์กด์ฑ์ ๊ฐ์ง ๊ฒฝ์ฐ BeanCurrentlyCreationExeption์ ๋ฐ์์ํด์ผ๋ก์จ ์ํ ์์กด์ฑ์ ์ ์ ์๋ค.
๊ทธ๋ฌ๋ Field Injection์ ์คํ๋ง ์ปจํ ์ด๋ ๋ง๊ณ ๋ ์ธ๋ถ์์ ์ฃผ์ ํ ์ ์๋ ๋ฐฉ๋ฒ์ด ์๋ค.
์ํ ์์กด์ฑ?
A Class๊ฐ B Class๋ฅผ ์ฐธ์กฐํ๋๋ฐ B Class๊ฐ ๋ค์ A Class๋ฅผ ์ฐธ์กฐํ ๊ฒฝ์ฐ,A Class๊ฐ B Class๋ฅผ ์ฐธ์กฐํ๊ณ , B Class๊ฐ C Class๋ฅผ ์ฐธ์กฐํ๊ณ C Class๊ฐ A Class๋ฅผ ์ฐธ์กฐํ๋ ๊ฒฝ์ฐ ์ด๋ฅผ ์ํ ์์กด์ฑ(Circular Dependency)์ด๋ผ๊ณ ๋ถ๋ฅธ๋ค.
์ฝ๊ธฐ ์ฝ๊ณ ์ฌ์ฉ์ด ํธํ๋จ๊ฒ ์ด์ธ์ ์ฅ์ ์ด...
Setter Injection(์์ ์ ์ฃผ์ )
Setter Injection์ ์ ํ์ ์ธ ์์กด์ฑ์ ์ฌ์ฉํ ๋ ์ ์ฉํ๋ค. ์ํฉ์ ๋ฐ๋ผ ์์กด์ฑ ์ฃผ์ ์ด ๊ฐ๋ฅํ๋ค.
//Setter Injection์ set Method๋ฅผ ์ ์ํด์ ์ฌ์ฉํ๋ค.
@Component
public class SampleController {
private SampleService sampleService;
@Autowired
public void setSampleService(SampleService sampleService) {
this.sampleService = sampleService;
}
}
Setter Injection์ผ๋ก ์์กด๊ด๊ณ ์ฃผ์ ์ ๋ฐํ์์์ ํ ์ ์๋๋ก ๋ฎ์ ๊ฒฐํฉ๋๋ฅผ ๊ฐ์ง๊ฒ ๊ตฌํ๋์๋ค.
ํ์ง๋ง Setter Injection์ ํตํด์ Service์ ๊ตฌํ์ฒด๋ฅผ ์ฃผ์ ํด์ฃผ์ง ์์๋ Controller ๊ฐ์ฒด๋ ์์ฑ์ด ๊ฐ๋ฅํ๋ค.
Controller ๊ฐ์ฒด๊ฐ ์์ฑ๊ฐ๋ฅํ๋ค๋ ๊ฒ์ ๋ด๋ถ์ ์๋ Service์ method ํธ์ถ์ด ๊ฐ๋ฅํ๋ค๋ ๊ฒ์ธ๋ฐ, set์ ํตํด Service์ ๊ตฌํ์ฒด๋ฅผ ์ฃผ์ ํด์ฃผ์ง ์์์ผ๋ฏ๋ก, NullPointerException ์ด ๋ฐ์ํ๋ค.
์ฃผ์ ์ด ํ์ํ ๊ฐ์ฒด๊ฐ ์ฃผ์ ์ด ๋์ง ์์๋ ์ผ๋ง๋ ์ง ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์๋ค๋ ๊ฒ์ด ๋ฌธ์ ๋ค.
์ด๋ฅผ ํด๊ฒฐํ ์์ด๊ฐ Constructor Injection์ด๋ค
Constructor Injection(์์ฑ์ ์ฃผ์ )
//Constructor์ @Autowired Annotation์ ๋ถ์ฌ ์์กด์ฑ์ ์ฃผ์
๋ฐ์ ์ ์๋ค.
@Component
public class SampleService {
private SampleDAO sampleDAO;
@Autowired
public SampleService(SampleDAO sampleDAO) {
this.sampleDAO = sampleDAO;
}
}
@Component
public class SampleController {
private final SampleService sampleService = new SampleService(new SampleDAO());
...
}
์์ฑ์ ์ฃผ์ ์ ์ฌ์ฉํด์ผํ๋ ์ด์ !!!
(Spring Framework Reference์์ ๊ถ์ฅํ๋ ๋ฐฉ๋ฒ์ ์์ฑ์๋ฅผ ํตํ ์ฃผ์ ~!)
์์ฑ์๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด ์ข์ ์ด์ ๋ ํ์์ ์ผ๋ก ์ฌ์ฉํด์ผํ๋ ์์กด์ฑ ์์ด๋ Instance๋ฅผ ๋ง๋ค์ง ๋ชปํ๋๋ก ๊ฐ์ ํ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค.
Spring 4.3๋ฒ์ ๋ถํฐ๋ Class๋ฅผ ์๋ฒฝํ๊ฒ DI Framework๋ก๋ถํฐ ๋ถ๋ฆฌํ ์ ์๋ค.
๋จ์ผ ์์ฑ์์ ํํด @Autowired๋ฅผ ๋ถ์ด์ง ์์๋ ๋๋ค.
๋ํ ์์ ์ดํด๋ณธ Field Injection์ ๋จ์ ๋ค์ ์ฅ์ ์ผ๋ก ๊ฐ์ ธ๊ฐ ์ ์๋ค.
- null์ ์ฃผ์ ํ์ง ์๋ ํ NullPointerException ์ ๋ฐ์ํ์ง ์๋๋ค.
- final ์ ์ฌ์ฉํ ์ ์๋ค.
- ์ํ ์์กด์ฑ์ ์ ์ ์๋ค.
- ์์กด์ฑ์ ์ฃผ์ ํ๊ธฐ๊ฐ ๋ฒ๊ฑฐ๋ก์ ์๊ธฐ๊ฐ์ ๋๋ ์ ์๋ค. (์ด๋ฅผ ๋ฐํ์ผ๋ก SRP์์น์ ์๊ฐํ๊ฒ ๋๊ณ ๋ฆฌํฉํ ๋งํ๊ฒ๋๋ค.)
Reference.
'๐ฅ > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[spring] ๋น ๋ผ์ดํ ์ฌ์ดํด๊ณผ ๋ฒ์ (0) | 2021.08.31 |
---|---|
[spring] ์ปดํฌ๋ํธ ์ค์บ์ด๋ (0) | 2021.08.31 |
[Spring] "ํ์ด๋ฆฌ๋จ" ์คํ๋ง ์คํฐ๋ 1์ฐจ (0) | 2021.08.20 |
[Spring] ์์กด์ฑ ์ฃผ์ (DI)์ด๋ ? (0) | 2021.08.18 |