์Šคํ”„๋ง ์˜์กด์„ฑ ์ฃผ์ž… (Spring Dependency Injection)

์Šคํ”„๋ง์—์„œ ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํ˜„์žฌ ์„ธ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฒซ ๋ฒˆ์งธ๋Š” ์ƒ์„ฑ์ž ์ฃผ์ž… ๋ฐฉ์‹์ด๊ณ  ๋‘ ๋ฒˆ์งธ๋Š” Setter ์ฃผ์ž… ๋ฐฉ์‹, ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ Field ์ฃผ์ž… ๋ฐฉ์‹์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ˆœ์„œ๋Œ€๋กœ ํ•œ๋ฒˆ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

1. ์ƒ์„ฑ์ž ์ฃผ์ž… (Constructor Based Dependency Injection)

๋ง ๊ทธ๋Œ€๋กœ ์ƒ์„ฑ์ž์— @Autowired๋กœ ๋‹ค๋ฅธ ๋นˆ์„ ์ฃผ์ž…ํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

@Controller
public class MyController {
    private MyService myService;
 
    @Autowired
    public MyController(MyService myService) {
        this.myService = myService;
    }
}

์ฃผ์ž…ํ•ด์•ผํ•˜๋Š” ๋นˆ์ด ๋งŽ์„ ๊ฒฝ์šฐ ์ƒ์„ฑ์ž ์ž์ฒด๊ฐ€ ์ปค์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๊ฒฝ์šฐ ๋งŒ์•ฝ lombok ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@Controller
@RequiredArgsConstructor
public class MyController {

    private final MyService myService;
 
}

@RequiredArgsConstructor ์–ด๋…ธํ…Œ์ด์…˜์„ class ๋ ˆ๋ฒจ์— ๋ถ™์—ฌ์ฃผ๊ณ  ์ฃผ์ž…ํ•  ๋นˆ์€ final ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด lombok์—์„œ ์ž๋™์œผ๋กœ ์ƒ์„ฑ์ž ์ฃผ์ž…์„ ์ด์šฉํ•˜์—ฌ ๋นˆ๋“ค์„ ์ฃผ์ž…ํ•ด์ค๋‹ˆ๋‹ค. ํ•„์š”ํ•œ ๋นˆ๋“ค์€ ๋ชจ๋‘ final ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์„œ ํ•„๋“œ๋กœ ์ถ”๊ฐ€๋งŒ ํ•ด์ฃผ๋ฉด ๋˜์ฃ . 

2. Setter ์ฃผ์ž… (Setter Based Dependency Injection)

Setter ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฃผ์ž…ํ•˜๋Š” ๋ฐฉ์‹์ด์ฃ . 

@Controller
public class MyController {

    private MyService myService;
    
    @Autowired
    public void setMyService(MyService myService) {
        this.myService = myService;
    }
}

ํŠน์ดํ•œ ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ์ด ๋ฐฉ์‹์€ ๊ฑฐ์˜ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋ชป๋ดค๋„ค์š”

3. Field ์ฃผ์ž… (์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”)

์˜ˆ์ „๋ถ€ํ„ฐ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์Šคํ”„๋ง์—์„œ ๊ถŒ๊ณ ํ•˜์ง€ ์•Š๋Š” ๋ฐฉ์‹์ด๊ธฐ๋„ ํ•˜์ฃ . 

@Controller
public class MyController {

    @Autowired
    private MyService myService;
}

ํ•„๋“œ ์ฃผ์ž… ๋ฐฉ์‹์€ ๊ณต์‹๋ฌธ์„œ์—๋„ ์†Œ๊ฐœ์กฐ์ฐจ ํ•˜๊ณ ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ณต์‹๋ฌธ์„œ์—๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹๋งŒ ์žˆ๋‹ค๊ณ  ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์ƒ์„ฑ์ž ์ฃผ์ž… ๋ฐฉ์‹๊ณผ setter ์ฃผ์ž…๋ฐฉ์‹์ด์ฃ . ๊ทธ ์ด์œ ๋Š” ํ•„๋“œ ์ฃผ์ž…์„ ์‚ฌ์šฉํ•˜๊ฒŒ๋˜๋ฉด ์ˆœํ™˜์ฐธ์กฐ ๋ฌธ์ œ๋ฅผ ์šฐํšŒํ•  ์ˆ˜ ์žˆ๊ฒŒ๋˜์–ด ์ˆœํ™˜์ฐธ์กฐ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ  ๊ฐœ๋ฐœ์„ ํ•˜๊ฒŒ๋˜๊ณ , ๊ทธ๋ ‡๊ฒŒ ์„ค๊ณ„๋œ ํด๋ž˜์Šค๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ธฐ๋Šฅ์„ ํ•˜๊ฒŒ๋˜์–ด ํ•˜๋‚˜์˜ ์ฑ…์ž„๋งŒ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค๋Š” Single Responsibility ์›์น™์— ์œ„๋ฐฐ๋  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์•„์ง€๊ฒŒ๋˜์ฃ . ๋˜ ๋‹ค๋ฅธ ์—ฌ๋Ÿฌ ๋‹จ์ ๋“ค์— ๋Œ€ํ•ด์„œ stackoverflow๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ•„๋“œ ์ฃผ์ž…์„ ์‚ฌ์šฉํ•˜๊ณ  ๊ณ„์‹œ๋‹ค๋ฉด ์ƒ์„ฑ์ž ์ฃผ์ž… ๋ฐฉ์‹์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ์„ธ์š”. ๊ทธ๋Ÿฌ๋ฉด ์ˆœํ™˜์ฐธ์กฐ๋ฅผ ์ฐพ์•„๋‚ผ ์ˆ˜ ์žˆ๊ณ  ์ˆœํ™˜์ฐธ์กฐ ๊ณ ๋ฆฌ ์•ˆ์— ์žˆ๋Š” ํด๋ž˜์Šค๋“ค์„ ์‚ดํŽด๋ณด๋ฉด Single Responsibility ์›์น™์„ ์œ„๋ฐฐํ•˜๋Š” ํด๋ž˜์Šค๊ฐ€ ๋ณด์ผ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๊ฒƒ๋“ค์€ ํด๋ž˜์Šค ๋ถ„๋ฆฌ๋ฅผ ํ†ตํ•ด ๊ฐœ์„ ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 

 

 

 

์ฐธ๊ณ ๋ฌธ์„œ

docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-dependencies

stackoverflow.com/questions/39890849/what-exactly-is-field-injection-and-how-to-avoid-it