๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’Ž/Spring

[Spring] DI(Dependency Injection)

by dar0m! 2021. 5. 2.

๊ฐœ๋…

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์ด ๋ฐœ์ƒํ•œ๋‹ค.
    • ๋ฐ˜๋ฉด, ์ƒ์„ฑ์ž ์ฃผ์ž…์„ ์‚ฌ์šฉํ•ด ์ž‘์„ฑ๋œ ํด๋ž˜์Šค๋ผ๋ฉด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์›ํ•˜๋Š” ๊ตฌํ˜„์ฒด๋ฅผ ๋„˜๊ฒจ์ฃผ๋ฉด ๋˜๊ณ , ๊ตฌํ˜„์ฒด๋ฅผ ๋„˜๊ฒจ์ฃผ์ง€ ์•Š์€ ๊ฒฝ์šฐ ๊ฐ์ฒด ์ƒ์„ฑ ์ž์ฒด๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ…Œ์ŠคํŠธํ•˜๊ธฐ ํŽธํ•˜๋‹ค.

 

์ฐธ๊ณ 

 

๋Œ“๊ธ€