[Spring] DI(Dependency Injection)
๊ฐ๋
DI๋ Dependency Injection ์์กด๊ด๊ณ ์ฃผ์ ์ด๋ผ๋ ๋ป์ผ๋ก ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ์์ ํต์ฉ๋๋ ๊ฐ๋ ์ด๋ค.
'๊ฐ์ฒด๋ฅผ ์ฃผ์ ๋ฐ๋๋ค'๋ ๊ฒ์ ์ธ๋ถ์์ ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ์ธํฐํ์ด์ค๋ฅผ ํตํด ๋๊ฒจ๋ฐ๋ ๊ฒ์ด๋ค. ๊ฐ์ฒด๋ฅผ ์ค์ค๋ก ๋ง๋๋ ๊ฒ์ด ์๋๋ผ ์ ์ด๊ถ์ (์คํ๋ง์) ์์ํ์ฌ (์คํ๋ง์ด) ๋ง๋ค์ด ๋์ ๊ฐ์ฒด๋ฅผ ๋๊ฒจ ๋ฐ๋ ๊ฒ์ด๋ค.
- ์ด๋ ๊ฒ ํ๋ฉด ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ ์ ์๊ณ ,
- ๋ฐํ์์์ ์์กด๊ด๊ณ๊ฐ ๊ฒฐ์ ๋๊ธฐ ๋๋ฌธ์ ์ ์ฐํ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๋ค.
SOLID ์์น์์ O ์ ํด๋นํ๋ Open Closed Principle ์ ์งํค๊ธฐ ์ํด์ ๋์์ธ ํจํด ์ค ์ ๋ตํจํด์ ์ฌ์ฉํ๊ฒ ๋๋๋ฐ, ์์ฑ์ ์ฃผ์ ์ ์ฌ์ฉํ๊ฒ ๋๋ฉด ์ ๋ตํจํด์ ์ฌ์ฉํ๊ฒ ๋๋ค.
- OCP : ๊ฐ๋ฐฉ ํ์ ์์น
- ํ์ฅ์๋ ์ด๋ ค์๊ณ , ์์ ์๋ ๋ซํ์์ด์ผ ํ๋ค.
์์กด๊ด๊ณ ์ฃผ์
์๋ ํฌ๊ฒ ์์ฑ์ ์ฃผ์
, ์์ ์ ์ฃผ์
๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์๋ค.
์คํ๋ง์์๋ ํ๋ ์ฃผ์
๊น์ง ์ธ ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ์์กด์ฑ์ ์ฃผ์
ํ ์ ์๋ค.
์คํ๋ง DI
Setter Based Injection (์์ ์ ์ฃผ์ )
@Service
public class StudentServiceImpl implements StudentService {
private CourseService courseService;
@Autowired
public void setCourseService(CourseService courseService) {
this.courseService = courseService;
}
@Override
public void studentMethod() {
courseService.courseMethod();
}
}
- ์์ ์ ์ฃผ์ ์ผ๋ก ์์กด๊ด๊ณ ์ฃผ์ ์ ๋ฐํ์์์ ํ ์ ์๋๋ก ๋ฎ์ ๊ฒฐํฉ๋๋ฅผ ๊ฐ์ง๊ฒ ๋์๋ค.
- ํ์ง๋ง, ๊ตฌํ์ฒด๋ฅผ ์ฃผ์ ํด ์ฃผ์ง ์์๋ ๊ฐ์ฒด๋ ์์ฑ ๊ฐ๋ฅํ๋ค. → NPE(NullPointerException) ๋ฐ์
- ์คํ๋ง ์ปจํ ์ด๋๊ฐ ์๋ ์ธ๋ถ์์ ์์ ์๋ฅผ ํธ์ถํด์ ์ฃผ์ ํ ์ ์๋ ๋ฐฉ๋ฒ์ด๋ผ๋ ์ด๋ ค์๋ค.
์์ ์ ์ฃผ์ ์ ์ด๋ฌํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐ ํ ์ ์๋ ๋ฐฉ๋ฒ์ด ์์ฑ์ ์ฃผ์ ์ด๋ค.
Field Injection(ํ๋ ์ฃผ์ )
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private CourseService courseService;
@Override
public void studentMethod() {
courseService.courseMethod();
}
}
- ์์ ์๋ฅผ ํตํ ์ฃผ์ ๊ณผ ์ ์ฌํ ๋ฐฉ์์ผ๋ก ์ด๋ฃจ์ด์ง๊ธฐ ๋๋ฌธ์ ์์ ์ ์ฃผ์ ๋ฐฉ๋ฒ๊ณผ ๋จ์ ์ด ๋์ผํ๋ค.
- ์คํ๋ง ์ปจํ ์ด๋ ๋ง๊ณ ๋ ์ธ๋ถ์์ ์ฃผ์ ํ ์ ์๋ ๋ฐฉ๋ฒ์ด ์๋ค.
Constructor based Injection (์์ฑ์ ์ฃผ์ )
@Service
public class StudentServiceImpl implements StudentService {
private final CourseService courseService;
@Autowired
public StudentServiceImpl(CourseService courseService) {
this.courseService = courseService;
}
@Override
public void studentMethod() {
courseService.courseMethod();
}
}
์ฅ์
- ์์กด๊ด๊ณ ์ค์ ์ด ๋์ง ์์ผ๋ฉด ๊ฐ์ฒด ์์ฑ ๋ถ๊ฐ → ์ปดํ์ผ ํ์์ ์ธ์ง ๊ฐ๋ฅ, NPE(NullPointerException) ๋ฐฉ์ง
- ์์กด์ฑ ์ฃผ์ ์ด ํ์ํ ํ๋๋ฅผ final ๋ก ์ ์ธ ๊ฐ๋ฅ → Immutable
- ๋ํ, ์คํ๋ง์์ ์์ฑ์ ์ฃผ์
์ ์ด์ฉํด '์ํ์ฐธ์กฐ ๋ฐฉ์ง'๊ฐ ๊ฐ๋ฅํ๋ค.
- ์ปจํ ์ด๋๊ฐ ๋น์ ์์ฑํ๋ ์์ ์ ๊ฐ์ฒด์์ฑ์ ์ฌ์ดํด ๊ด๊ณ๊ฐ ์๊ธฐ๊ธฐ ๋๋ฌธ์ ์คํ๋ง์ด ์ํ์ฐธ์กฐ๋ผ๊ณ ์๋ ค์ค๋ค.
- ํ๋ ์ฃผ์ ์ด๋, ์์ ์ ์ฃผ์ ์ ๊ฐ์ฒด ์์ฑ์์ ์ ์ํ์ฐธ์กฐ๊ฐ ์ผ์ด๋๋์ง ์๋์ง ๋ฐ๊ฒฌํ ์ ์๋ ๋ฐฉ๋ฒ์ด ์๋ค.
- ๊ฐ์ฒด ์์ฑ ์์ ์ํ์ฐธ์กฐ์ ๊ฐ์ฒด ์์ฑ ํ ๋น์ฆ๋์ค ๋ก์ง์์์ ์ํ์ฐธ์กฐ๊ฐ ์ผ์ด๋๋ ๊ฒ์ ์์ ํ ๋ค๋ฅธ ์๊ธฐ๋ค.
- ๋จ์ ํ
์คํธ ์์ฑ์ ์ฉ์ดํ๋ค.
- field injection์ ์ฌ์ฉํด์ ์์ฑ๋ ํด๋์ค๋ผ๋ฉด ๋จ์ ํ ์คํธ์ ์์กด๊ด๊ณ๋ฅผ ๊ฐ์ง๋ ๊ฐ์ฒด๋ฅผ ์์ฑํด์ ์ฃผ์ ํ ์ ์๋ค.
- ์คํ๋ง IoC ์ปจํ ์ด๋๊ฐ ์์ฑํด์ ์ฃผ์ ํด์ฃผ๋ ๋ฐฉ์์ด๊ธฐ ๋๋ฌธ์ ์ธ๋ถ๋ก ๋ ธ์ถ๋์ด ์๋ ๊ฒ์ด ์๊ธฐ ๋๋ฌธ์ ์์กด๊ด๊ณ๋ฅผ ๊ฐ๊ณ ์๋ ๋ฉ์๋์ ๋จ์ ํ ์คํธ๋ฅผ ์์ฑํ๋ฉด NullPointerException์ด ๋ฐ์ํ๋ค.
- ๋ฐ๋ฉด, ์์ฑ์ ์ฃผ์ ์ ์ฌ์ฉํด ์์ฑ๋ ํด๋์ค๋ผ๋ฉด ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ์ํ๋ ๊ตฌํ์ฒด๋ฅผ ๋๊ฒจ์ฃผ๋ฉด ๋๊ณ , ๊ตฌํ์ฒด๋ฅผ ๋๊ฒจ์ฃผ์ง ์์ ๊ฒฝ์ฐ ๊ฐ์ฒด ์์ฑ ์์ฒด๊ฐ ๋ถ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์ ํ ์คํธํ๊ธฐ ํธํ๋ค.
์ฐธ๊ณ