๐ŸŒฟ Spring

Spring Container(ApplicationContext)์™€ Bean

์—ฐ_์šฐ๋ฆฌ 2022. 3. 28. 15:02
๋ฐ˜์‘ํ˜•

๋ชฉ์ฐจ

     

     

     

    @Configuration

    ์ด ์–ด๋…ธํ…Œ์ด์…˜์„ ํด๋ž˜์Šค์— ๋ถ™์ด๋ฉด ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ์Šคํ”„๋ง ์„ค์ • ํด๋ž˜์Šค๋กœ ๋“ฑ๋กํ•œ๋‹ค.

     

    @Bean ๋นˆ

    ์Šคํ”„๋ง์ด ์ƒ์„ฑํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋นˆ์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

    @Bean์„ ๋ฉ”์„œ๋“œ์— ๋ถ™์ด๋ฉด ํ•ด๋‹น ๋ฉ”์„œ๋“œ๊ฐ€ ์ƒ์„ฑํ•œ ๊ฐ์ฒด๋ฅผ ์Šคํ”„๋ง์ด ๊ด€๋ฆฌํ•˜๋Š” ๋นˆ ๊ฐ์ฒด๋กœ ๋“ฑ๋กํ•œ๋‹ค.

    @Configuration  //AppContext.java๋ฅผ ์Šคํ”„๋ง ์„ค์ • ํด๋ž˜์Šค๋กœ ๋“ฑ๋กํ•œ๋‹ค
    public class AppContext {
    
        @Bean
        public Greeter greeter(){
            Greeter greeter = new Greeter();
            greeter.setFormat("%s, ์•ˆ๋…•ํ•˜์„ธ์š”.");
            return greeter;
        }
    }

     

    Greeterํƒ€์ž…์ด๊ณ , "greeter" ๋ผ๋Š” ์ด๋ฆ„์˜ Bean์ด ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋ก๋œ๋‹ค.

     

     

     

    ApplicationContext = Spring Container

    ApplicationContext ctx = new AnnotationConfigApplicationContext(AppContext.class);
    //์–ด๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜์˜ ์„ค์ •์ •๋ณด๋ฅผ ์ฝ์–ด์˜ค๊ธฐ
    
    ApplicationContext ctx = new GenericXmlApplicationContext(AppContext.xml);
    //XML๊ธฐ๋ฐ˜์˜ ์„ค์ •์ •๋ณด๋ฅผ ์ฝ์–ด์˜ค๊ธฐ

    ์–ด๋…ธํ…Œ์ด์…˜, XML๊ธฐ๋ฐ˜์˜ ์„ค์ •์ •๋ณด๋ฅผ ์ฝ์–ด์™€์„œ ๋นˆ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” Context๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

    ์–ด๋…ธํ…Œ์ด์…˜๊ธฐ๋ฐ˜์„ ๋ฐ”ํƒ•์œผ๋กœ ์„ค๋ช…ํ•˜๋ฉด, AppContext์— ์ •์˜ํ•œ @Bean์„ ์ฝ์–ด์™€ "greeter"๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

     

    ApplicationContext๋Š” Bean๊ฐ์ฒด์˜ ์ƒ์„ฑ, ์ดˆ๊ธฐํ™”, ๋ณด๊ด€, ์ œ๊ฑฐ ๋“ฑ์„ ์—ญํ• ์„ ๋งก๊ณ ์žˆ๋‹ค.

    => ๊ฐ์ฒด๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์„ Container๋ผ๊ณ ๋„ ๋ถ€๋ฅธ๋‹ค.

     

     

    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    public class Main {
    
        public static void main(String[] args) {
            AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppContext.class);
    
            Greeter greeter1 = ctx.getBean("greeter", Greeter.class);
            //์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์—์„œ Gretter.classํƒ€์ž…์ด๊ณ , ์ด๋ฆ„์ด greeter ๋นˆ์„ ๊ฐ€์ ธ์˜จ๋‹ค
    
            Greeter greeter2 = ctx.getBean("greeter", Greeter.class);
            Greeter greeter3 = new Greeter();
            Greeter greeter4 = new Greeter();
            
            System.out.println(greeter1 == greeter2);   //true
            System.out.println(greeter1 == greeter3);   //false
            System.out.println(greeter3 == greeter4);   //false
    
            ctx.close();
        }
    }

    greeter1๊ณผ greeter2๋Š” ๊ฐ™์€ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. = ๊ฐ™์€ ๊ฐ์ฒด์ด๋‹ค.

    greeter3์™€ greeter4๋Š” ๋‹ค๋ฅธ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. = ๋‹ค๋ฅธ ๊ฐ์ฒด์ด๋‹ค.

     

    ์Šคํ”„๋ง์€ ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋ก๋œ Bean์„ ๊ธฐ๋ณธ์ ์œผ๋กœ 1๊ฐœ๋งŒ ์ƒ์„ฑํ•ด์ค€๋‹ค. (์‹ฑ๊ธ€ํ†ค)

     

     

     

     

     

     

     

    BeanFactory

    ๋นˆ ์ƒ์„ฑ๊ณผ ๊ฒ€์ƒ‰์— ๋Œ€ํ•œ ๊ธฐ๋Šฅ์„ ์ •์˜ํ•˜๊ณ ์žˆ๋‹ค.

     

     

    ApplicationContext

    BeanFactory๋ฅผ ์ƒ์†๋ฐ›์•„์„œ ๋” ํŽธ๋ฆฌํ•˜๊ฒŒ ๋นˆ์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค€๋‹ค.

    + ๋ฉ”์‹œ์ง€, ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋“ฑ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํŽธ๋ฆฌ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ด์ค€๋‹ค.

     

     

    ๋ฐ˜์‘ํ˜•
    • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
    • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
    • ํŠธ์œ„ํ„ฐ ๊ณต์œ ํ•˜๊ธฐ
    • ๊ตฌ๊ธ€ ํ”Œ๋Ÿฌ์Šค ๊ณต์œ ํ•˜๊ธฐ
    • ์นด์นด์˜คํ†ก ๊ณต์œ ํ•˜๊ธฐ