WEB/Server

[์Šคํ”„๋ง๋ถ€ํŠธ ์ž…๋ฌธ] 3. ์ปดํฌ๋„ŒํŠธ ์Šค์บ”๊ณผ ์ž๋™ ์˜์กด๊ด€๊ณ„ ์„ค์ •

kite707 2024. 4. 25.

DI์™€ IoC

์Šคํ”„๋ง์—๋Š” DI(Dependency Injection, ์˜์กด์„ฑ ์ฃผ์ž…)์ด๋ผ๋Š” ๊ฐœ๋…์ด ์žˆ๋‹ค. @Autowired๊ฐ€ ์žˆ์œผ๋ฉด ์Šคํ”„๋ง์ด ์—ฐ๊ด€๋œ ๊ฐ์ฒด๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์—์„œ ์ฐพ์•„์„œ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

 

๋˜ ์Šคํ”„๋ง์€ @Component ์–ด๋…ธํ…Œ์ด์…˜์ด ์žˆ์œผ๋ฉด ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ์ž๋™ ๋“ฑ๋กํ•˜๊ณ  ์ด๋Ÿฐ ๊ฐ์ฒด๋“ค์˜ ์ƒ์„ฑ, ์„ค์ •, ๊ด€๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ํ”„๋ ˆ์ž„์›Œํฌ(์Šคํ”„๋ง)๊ฐ€ ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์„ IoC(Inversion of Control, ์ œ์–ด์˜ ์—ญ์ „)์ด๋ผ๊ณ  ํ•œ๋‹ค.

 

์ด๋ ‡๊ฒŒ ๊ธ€๋กœ๋งŒ ์ฝ์–ด์„œ๋Š” ์ž˜ ์™€๋‹ฟ์ง€ ์•Š๋Š”๋‹ค. ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด DI์™€ IoC์— ๋Œ€ํ•ด ์ดํ•ดํ•ด๋ณด๋„๋ก ํ•˜์ž.

 

์ฝ”๋“œ๋ฅผ ๋ณด๊ธฐ ์ „ MemberController์—์„œ MemberService๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋Š” ์ƒํ™ฉ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜์ž. ์ด ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด ์„ ์–ธํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

private final MemberService memberService = new MemberService();

ํ•˜์ง€๋งŒ ์ด MemberService๊ฐ์ฒด๊ฐ€ ๋งŽ์€ ๊ณณ์—์„œ ์‚ฌ์šฉ๋˜์–ด์•ผ ํ•œ๋‹ค๋ฉด ์œ„ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š” ๊ณณ์—์„œ ๋งค ๋ฒˆ ์„ ์–ธํ•˜๋Š” ๊ฒƒ๋„ ๊ท€์ฐฎ๊ณ  ์ดํ›„ ์ด๋ฆ„์ด ๋ฐ”๋€Œ๊ธฐ๋ผ๋„ ํ•˜๋ฉด ๋ชจ๋‘ ๋ฐ”๊ฟ”์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๋˜ ์ด ๊ฐ์ฒด๊ฐ€ ๋ฌด์–ธ๊ฐ€๋ฅผ ์ €์žฅํ•œ๋‹ค๋ฉด ํ•˜๋‚˜๋งŒ ๋งŒ๋“ค์–ด์„œ ๊ณต์œ ํ•ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๊ฒƒ์„ ๊ฐ„ํŽธํ•˜๊ฒŒ ํ•ด์ฃผ๊ธฐ ์œ„ํ•œ ๊ธฐ๋ฒ•์ด ๋ฐ”๋กœ DI์ด๋‹ค.

์˜ˆ์ œ

๋จผ์ € ์œ„์—์„œ ๋งํ–ˆ๋˜ ๋Œ€๋กœ new๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  MemberController์—์„œ MemberService๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์งœ๋ฉด ๋œ๋‹ค.

@Controller
public class MemberController {
    private final MemberService memberService;

    @Autowired
    public MemberController(MemberService memberService){
        this.memberService=memberService;
    }
}
@Service
public class MemberService {

}

์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด @Controller์™€ @Service๊ฐ€ ๋ณด์ผ ๊ฒƒ์ด๋‹ค. ์‚ฌ์‹ค ์ž๋งคํ’ˆ์œผ๋กœ @Repository๋„ ์žˆ๋Š”๋ฐ cmd+click์„ ํ†ตํ•ด ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜๋“ค์˜ ์ •์˜๋ฅผ ๋ณด๋ฉด ์ด๋Ÿฐ ๋ฌธ๊ตฌ๊ฐ€ ์žˆ๋‹ค.

This annotation serves as a specialization of @Component

 

์ฆ‰ ์–˜๋“ค์€ ๋ชจ๋‘ @Component์˜ ํŠน์ˆ˜ ์ผ€์ด์Šค๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ์Šคํ”„๋ง์ด ์‹คํ–‰๋˜๋ฉด ์Šคํ”„๋ง์€ ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์ด๋ผ๋Š” ๊ฒƒ์„ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด์„œ @Component๊ฐ€ ๋ถ™์€ ๊ฐ์ฒด๋“ค์„ ๋ชจ๋‘ ์ƒ์„ฑํ•ด์„œ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ผ๋Š” ๊ณณ์— ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ์Šคํ”„๋ง์ด ๋Œ€์‹  ๊ฐ์ฒด๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ ์•ž์—์„œ ๋ณธ IoC(Inversion of Control, ์ œ์–ด์˜ ์—ญ์ „)์ด๋‹ค.

 

๊ทธ๋Ÿผ ์ด๋ ‡๊ฒŒ ์ƒ์„ฑํ•œ ์Šคํ”„๋ง ๋นˆ(๊ฐ์ฒด)๋“ค์€ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ• ๊นŒ? ๊ทธ๊ฑธ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ฐ”๋กœ @Autowired์ด๋‹ค. MemberController์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด @AutoWired ์–ด๋…ธํ…Œ์ด์…˜ ๋ฐ‘์— MemberController์˜ ์ƒ์„ฑ์ž๊ฐ€ ์žˆ๋‹ค. ๊ทธ ์ƒ์„ฑ์ž์—์„œ๋Š” MemberService๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„ this.memberService์— ๋“ฑ๋กํ•œ๋‹ค. ์ด๋•Œ ์ธ์ž๋กœ ๋ฐ›๋Š” MemberService๊ฐ€ ๋ฐ”๋กœ ์Šคํ”„๋ง ๋นˆ์ด๋‹ค. @AutoWired ์–ด๋…ธํ…Œ์ด์…˜ ๋•๋ถ„์— ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋กœ๋ถ€ํ„ฐ MemberService๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์ด ๋ฐ”๋กœ DI(Dependency Injection, ์˜์กด์„ฑ ์ฃผ์ž…)์ด๋‹ค.

์ •๋ฆฌ

์ด๋ฒˆ ๊ธ€์—์„œ๋Š” DI, IoC์˜ ๊ฐœ๋…๊ณผ ์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•ด ๊ฐ์ฒด๋ฅผ ์Šคํ”„๋ง๋นˆ์— ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๋‹ค. ์‚ฌ์‹ค ์ž๋ฐ” ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•ด ์Šคํ”„๋ง ๋นˆ์„ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋Š”๋ฐ ๊ทธ ๋‚ด์šฉ์€ ๋‹ค์Œ ๊ธ€์—์„œ ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ๋‹ค.

๋Œ“๊ธ€