ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring] ์Šคํ”„๋ง IOC ์ปจํ…Œ์ด๋„ˆ์™€ Bean
    Spring Boot 2023. 1. 15. 00:50

    ๐ŸŒˆ Spring IOC, Container, DI, Bean?

    ์Šคํ”„๋ง์„ ์‚ฌ์šฉํ•˜๋‹ค ๋ณด๋ฉด ๊พธ์ค€ํžˆ ๋“ค์–ด์˜ค๋˜ ์šฉ์–ด๋“ค์ด๋‹ค. ์ด ์žฅ์—์„œ๋Š” ๊ทธ ๊ฐœ๋…์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ณ  ์ •๋ฆฌํ•ด ๋ณธ๋‹ค.

    IOC (Inversion of Control)

    ์ œ์–ด์˜ ์—ญ์ „์ด๋ผ ๋ถ€๋ฅธ๋‹ค. ์•„๋ž˜์—์„œ ์ปจํ…Œ์ด๋„ˆ์™€ ํ•จ๊ป˜ ์„ค๋ช…ํ•œ๋‹ค.

    Container (์ปจํ…Œ์ด๋„ˆ)

    ์ปจํ…Œ์ด๋„ˆ๋Š” ๋ณดํ†ต ๊ฐ์ฒด์˜ ์ƒ๋ช…์ฃผ๊ธฐ, ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๋“ค์—๊ฒŒ ์ถ”๊ฐ€์ ์ธ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” ์˜์กด์„ฑ๋„ ๊ด€๋ฆฌํ•œ๋‹ค.

     

    Spring IOC Container - ์Šคํ”„๋ง IOC ์ปจํ…Œ์ด๋„ˆ

    ์Šคํ”„๋ง์€ ์Šคํ”„๋ง IOC ์ปจํ…Œ์ด๋„ˆ์—์„œ ๊ฐ์ฒด๋ฅผ ๊ด€๋ฆฌํ•˜๋Š”๋ฐ, ์ด๋•Œ ์Šคํ”„๋ง์ด ์ œ์–ด ๊ถŒํ•œ์„ ๊ฐ€์ ธ ๊ฐ์ฒด ์ƒ์„ฑ, ๊ด€๊ณ„, ์˜์กด์„ฑ, ์ƒ๋ช…์ฃผ๊ธฐ ๊ด€๋ฆฌ ํ•˜๋Š”๋ฐ ์ด๋•Œ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ Bean์ด๋ผ๊ณ  ํ•œ๋‹ค. ์ด Bean๋“ค์€ ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ๊ด€๋ฆฌ๋œ๋‹ค.

    ์ฆ‰, ๊ฐœ๋ฐœ์ž๋Š” ์ด๋Ÿฐ ๊ณผ์ •์„ ์Šคํ‚ต ํ•จ์œผ๋กœ์จ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์— ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

    ๊ทธ๋ ‡๋‹ค๋ฉด ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ํด๋ž˜์Šค๋ฅผ ์Šคํ”„๋ง IOC ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ• ๊นŒ? ๋ฐ”๋กœ Bean์œผ๋กœ ๋“ฑ๋กํ•ด ์ฃผ๋ฉด ๋œ๋‹ค. Bean์œผ๋กœ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ ์˜ˆ์ „์— ์‚ฌ์šฉํ•˜๋˜ XML์„ ํ†ตํ•ด ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ์™ธํ•˜๊ณ  ์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด ๋“ฑ๋กํ•˜๋Š” 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ํ™•์ธํ•ด ๋ณด์ž.

     

    1. Bean์œผ๋กœ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•

    1-1. Component Scan

    Bean์œผ๋กœ ๋“ฑ๋ก๋  ์ค€๋น„๊ฐ€ ๋œ ํด๋ž˜์Šค๋“ค์„ ์Šค์บ”ํ•˜์—ฌ Bean์œผ๋กœ ๋“ฑ๋กํ•ด ์ฃผ๋Š” ๊ณผ์ •์ด๋‹ค.

     

    ์ค€๋น„๊ฐ€ ๋œ ํด๋ž˜์Šค๋“ค์ด๋ž€ @Component ์–ด๋…ธํ…Œ์ด์…˜์„ ํด๋ž˜์Šค์— ์ถ”๊ฐ€ํ•œ ๊ฒƒ์„ ๋งํ•œ๋‹ค. ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” @Controller, @Service, @Repository๋„ ๋”ฐ๋ผ๊ฐ€ ๋ณด๋ฉด @Componet์ด ์žˆ๋Š” ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ๊ทธ๋ ‡๋‹ค๋ฉด ์ด์ œ ํ•˜๋‚˜ ๋” ๊ถ๊ธˆ์ฆ์ด ์ƒ๊ธด๋‹ค. ๋‚˜๋Š” ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์„ ํ•œ ์ ์ด ์—†๋Š”๋ฐ ์™œ ์œ„์˜ ์–ด๋…ธํ…Œ์ด์…˜๋งŒ ๋ถ™์˜€์„ ๋ฟ์ธ๋ฐ Bean์œผ๋กœ ๋“ฑ๋ก๋œ ๊ฑธ๊นŒ?

     

    ์Šคํ”„๋ง๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ~Application ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑ๋˜๋Š”๋ฐ ์ด ํด๋ž˜์Šค์—๋Š” @SpringBootApplication ์–ด๋…ธํ…Œ์ด์…˜์ด ์žˆ๋Š”๋ฐ ๊ทธ ์•ˆ์„ ์‚ดํŽด๋ณด๋ฉด @ComponentScan ์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด์„œ ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜์œ„ ํŒจํ‚ค์ง€๋ฅผ ์Šค์บ”ํ•˜๋Š” ๊ฑธ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

     

    1-2. Java config - @Configuration, @Bean

    @Configuration ํด๋ž˜์Šค์—์„œ @Bean ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ด ์ˆ˜๋™์œผ๋กœ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๋นˆ์„ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

    @Configuration
    public class TestConfig {
        @Bean
        public OpenApi openApi() {
            return new OpenApi();
        }
    }

    ์œ„์™€ ๊ฐ™์ด ์ˆ˜๋™์œผ๋กœ Bean์„ ๋“ฑ๋กํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ฃผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

    1. ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ์ œ์–ด๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜์—ฌ ์‚ฌ์šฉํ•  ๋•Œ

    2. ์ „๋ฒ”์œ„์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋“ฑ๋กํ•  ๋•Œ

    3. ๋‹คํ˜•์„ฑ์„ ํ™œ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ตฌํ˜„์ฒด๋ฅผ ๋“ฑ๋กํ•ด์ฃผ์–ด์•ผ ํ•  ๋•Œ

     

    2. DI(Dependency Injection) - ์˜์กด์„ฑ ์ฃผ์ž…

    Bean์œผ๋กœ ๋“ฑ๋กํ•˜๊ณ  ์Šคํ”„๋ง IOC ์ปจํ…Œ์ด๋„ˆ์— ์˜ํ•ด ์ƒ์„ฑ๋„ ๋˜์—ˆ๋‹ค๋ฉด, ์ด์ œ ์˜์กด์„ฑ ์ฃผ์ž…์„ ํ†ตํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์˜์กด์„ฑ ์ฃผ์ž…์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ์ฐจ๋ก€๋Œ€๋กœ ํ™•์ธํ•ด ๋ณด์ž.

    2-1. ํ•„๋“œ ์ฃผ์ž…

    @Controller
    public class TestController {
        @Autowired
        private TestService testService;
    }

    2-2. ์ˆ˜์ •์ž(Setter) ์ฃผ์ž…

    @Controller
    public class TestController {
    
        private TestService testService;
    
        @Autowired
        public void setTestService(TestService testService) {
            this.testService = testService;
        }
    }

    2-3. ์ƒ์„ฑ์ž ์ฃผ์ž…

    @Controller
    public class TestController {
    
        private final TestService testService;
    
        public TestController(TestService testService) {
            this.testService = testService;
        }
    }

    ๋‹ค๋งŒ, ํ•„๋“œ๋‚˜ Setter ๋ฉ”์„œ๋“œ์—์„œ ์ฃผ์ž…์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์ƒ์„ฑ์ž ์ฃผ์ž… ๋ฐฉ๋ฒ•์ด ๊ถŒ์žฅ๋œ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

    1. ์ˆœํ™˜ ์ฐธ์กฐ ๋ฐฉ์ง€

    A๊ฐ€ B๋ฅผ ์ฐธ์กฐํ•˜๊ณ , B๊ฐ€ ๋‹ค์‹œ A๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ฌธ์ œ

    2. ๋ถˆ๋ณ€์„ฑ(Immutability)

    ์ƒ์„ฑ์ž๋กœ ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•  ๋•Œ final๋กœ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๊ณ , ๋Ÿฐํƒ€์ž„์—์„œ ์˜์กด์„ฑ์„ ์ฃผ์ž…๋ฐ›๋Š” ๊ฐ์ฒด๊ฐ€ ๋ณ€ํ•  ์ผ์ด ์—†์–ด์ง€๊ฒŒ ๋œ๋‹ค. ๋˜, ํ•„๋“œ ์ฃผ์ž… ๋ฐฉ์‹์€ null์ด ๋งŒ๋“ค์–ด์งˆ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š”๋ฐ final๋กœ ์„ ์–ธํ•œ ์ƒ์„ฑ์ž ์ฃผ์ž… ๋ฐฉ์‹์€ null์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

    Reference

    ๋Œ“๊ธ€

Designed by Tistory.