[Spring] ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ

2021. 10. 3. 08:00ใ†Back-End ์ž‘์—…์‹ค/Spring Framework

728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š”? ์ฃผ๋‹ˆํ•˜๋ž‘ ์ž…๋‹ˆ๋‹ค.

์˜ค๋Š˜์€ Spring์˜ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์™€ ์Šคํ”„๋ง ๋นˆ์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ ํ•ด ๋ณด๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

์ด ๋‚ด์šฉ์€ ์ธํ”„๋Ÿฐ - ๊น€์˜ํ•œ ๊ฐ•์‚ฌ๋‹˜์˜ ์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์ •๋ฆฌ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.




 

๐Ÿ“‹ ๋ชฉ์ฐจ


01. [Spring] ์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ
02. [Spring] ์ข‹์€ ๊ฐ์ฒด ์ง€ํ–ฅ ์„ค๊ณ„์˜ 5๊ฐ€์ง€ ์›์น™ (SOLID)
03. 
[Spring] ๊ฐ์ฒด ์ง€ํ–ฅ ์„ค๊ณ„์™€ ์Šคํ”„๋ง   
04. [Spring] OCP์™€ DIP ๊ณ ๋ คํ•˜๊ธฐ
05. [Spring] ๊ฐ์ฒด ์ง€ํ–ฅ ์›๋ฆฌ ์ ์šฉ - ์ƒˆ๋กœ์šด ํ• ์ธ ์ •์ฑ… ๊ฐœ๋ฐœ
06. 
[Spring] AppConfig ๋ฆฌํŒฉํ„ฐ๋ง    
07. [Spring] Ioc, DI Container  
08. [Spring] ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ  
09. [Spring] ์‹ฑ๊ธ€ํ†ค ์ปจํ…Œ์ด๋„ˆ  
10. [Spring] Component Scan And Auto wired Start
11. 
[Spring] ์˜์กด๊ด€๊ณ„ ์ฃผ์ž… ๋ฐฉ๋ฒ•    
12. [Spring] Bean Life Cycle Call Back
13. 
[Spring] Bean Scope

 

 

 


 

 

 

๐Ÿ“Œ Spring Container Create


 

๋จผ์ € ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ณผ์ •๋ถ€ํ„ฐ ์•Œ์•„๋ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);

 

ApplicationContext๋ฅผ Spring Container๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ์ด์—์š”. ๋˜ํ•œ ์ด๊ฒƒ์€ Interface๋ž๋‹ˆ๋‹ค!

Spring Container๋Š” XML ๊ธฐ๋ฐ˜์œผ๋กœ ๋งŒ๋“ค์ˆ˜๋„ ์žˆ๊ณ , Annotaion ๊ธฐ๋ฐ˜์˜ Java ์„ค์ • Class๋กœ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

์ด ์ „์— AppConfig.java๋ฅผ ์‚ฌ์šฉํ–ˆ๋˜ ๋ฐฉ์‹์ด ๋ฐ”๋กœ Annotaion ๊ธฐ๋ฐ˜์˜ Java ์„ค์ • Class๋กœ Spring Container๋ฅผ ๋งŒ๋“  ๊ฒƒ์ด์—์š”.

Java ์„ค์ • Class๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ Spring Container(ApplicationContext)๋ฅผ ๋งŒ๋“ค์–ด ๋ณผ๊นŒ์š”?

  1. new AnnotaionConfigApplicationContext(AppConfig.class);
  2. ์ด Class๋Š” ApplicationContext Interface์˜ ๊ตฌํ˜„์ฒด

 

์ฐธ๊ณ  : ์ •ํ™•ํžˆ๋Š” Spring Container๋ฅผ ๋ถ€๋ฅผ ๋•Œ, BeanFactory, ApplicationContext๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ด์•ผ๊ธฐ ํ•œ๋‹ค.

BeanFactory๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๊ฑฐ์˜ ์—†์œผ๋ฏ€๋กœ, ์ผ๋ฐ˜์ ์œผ๋กœ ApplicationContext๋ฅผ Spring Container๋ผ๊ณ  ํ•œ๋‹ค.

 

 

    ๐Ÿ“Spring Container ์ƒ์„ฑ ๊ณผ์ •

         ๐Ÿ‘‰ RateDiscountPolicy ์ถ”๊ฐ€

 

 

new AnnotaionConfigApplicationContext(AppConfig.class);

 

์œ„์™€ ๊ฐ™์ด Spring Container๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๊ตฌ์„ฑ ์ •๋ณด(์ธ์ž๊ฐ’์— ๋“ค์–ด๊ฐ€๋Š” ๊ตฌ์„ฑ ์ •๋ณด๋ฅผ ๋‹ด์€ Class)๋ฅผ ์ง€์ •ํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

์—ฌ๊ธฐ์„œ๋Š” AppConfig.class๋กœ ์‚ฌ์šฉํ•˜์˜€์ฃ .

 

         ๐Ÿ‘‰ Spring Bean ๋“ฑ๋ก

 

 

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜์–ด ์˜จ ์„ค์ • ํด๋ž˜์Šค ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ์Šคํ”„๋ง ๋นˆ์— ๋“ฑ๋กํ•˜๋Š” ๊ฒƒ์ด์—์š”.

 

์—ฌ๊ธฐ์„œ ๋นˆ ์ด๋ฆ„์€ Method์˜ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉฐ, ์ง์ ‘ ๋ถ€์—ฌํ•  ์ˆ˜๋„ ์žˆ๋Š” ๊ฒƒ์ด์—์š”. ๋ฐ”๋กœ ์•„๋ž˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ์š”!

@Bean(name = "BeanName์ž…๋ ฅ")

 

ํ•˜์ง€๋งŒ, ์กฐ์‹ฌํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด ์žˆ๋Š” ๊ฒƒ์ด์—์š”. Bean ์ด๋ฆ„์€ ํ•ญ์ƒ ๋‹ค๋ฅธ ์ด๋ฆ„์„ ๋ถ€์—ฌํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

๊ฐ™์€ ์ด๋ฆ„์„ ๋ถ€์—ฌํ•˜๋ฉด Spring Container๋Š” ๋‹ค๋ฅธ ๋นˆ์„ ๋ฌด์‹œํ•ด ๋ฒ„๋ฆฌ๊ฑฐ๋‚˜, ๊ธฐ์กด ๋นˆ์„ ๋ฎ์–ด์“ฐ๊ธฐ ํ•ด ๋ฒ„๋ฆฌ๊ฑฐ๋‚˜, ์„ค์ •์— ๋”ฐ๋ผ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด์—์š”.

 

         ๐Ÿ‘‰ Spring Bean ์˜์กด๊ด€๊ณ„ ์„ค์ • - ์ค€๋น„

 

 

         ๐Ÿ‘‰ Spring Bean ์˜์กด๊ด€๊ณ„ ์„ค์ • - ์™„๋ฃŒ

 

 

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” ์„ค์ • ์ •๋ณด๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ์˜์กด๊ด€๊ณ„๋ฅผ ์ฃผ์ž…(DI)ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

๋‹จ์ˆœํžˆ Java์˜ Method๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ ๊ฐ™์ด ๋ณด์ด์ง€๋งŒ, ์ฐจ์ด๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

 

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

 

์ •๋ฆฌํ•˜์ž๋ฉด

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์„ค์ •(๊ตฌ์„ฑ) ์ •๋ณด๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ์Šคํ”„๋ง ๋นˆ๋„ ๋“ฑ๋กํ•˜๊ณ , ์˜์กด๊ด€๊ณ„๋„ ์„ค์ •ํ•œ ๊ฒƒ์ด์—์š”.

์ด์ œ ํ•œ๋ฒˆ Data๋ฅผ ์กฐํšŒ ํ•ด ๋ด์š” ์šฐ๋ฆฌ!

 

         ๐Ÿ‘‰ ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋ก๋œ ๋ชจ๋“  ๋นˆ ์กฐํšŒ

 

package hello.core.beanfind;


import hello.core.AppConfig;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class ApplicationContextInfoTest {

    AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);

    @Test @DisplayName("๋ชจ๋“  Bean ์ถœ๋ ฅํ•˜๊ธฐ")
    void findAllBean() {
        String[] beanDefinitionNames = ac.getBeanDefinitionNames();

        for (String beanDefinitionName : beanDefinitionNames) {
            Object bean = ac.getBean(beanDefinitionName);

            System.out.println("name = " + beanDefinitionName + "\n object = " + bean);
            System.out.println("----------------------------------------------------------------");
        }
    }

    @Test @DisplayName("Application Bean ์ถœ๋ ฅํ•˜๊ธฐ")
    void findApplicationBean() {
        String[] beanDefinitionNames = ac.getBeanDefinitionNames();

        for (String beanDefinitionName : beanDefinitionNames) {
            BeanDefinition beanDefinition = ac.getBeanDefinition(beanDefinitionName);  // getBeanDefinition ์ด๊ฒƒ์€ Bean์— ๋Œ€ํ•œ Meta Data ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
            // ROLE_INFRASTRUCTOR๋Š” ์Šคํ”„๋ง ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๋Š” Bean์— ๋Œ€ํ•ด ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
            if (beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION) {  // getRole Method์—์„œ ROL_AAPLICATON์€ Spring์ด ์‚ฌ์ „์— ๋“ฑ๋กํ•œ Bean์ด ์•„๋‹Œ ์‚ฌ์šฉ์ž๊ฐ€ ์ƒ์„ฑํ•œ Bean ํ˜น์€ ์™ธ๋ถ€ Library ๋ผ๋Š” ๋œป

                Object bean = ac.getBean(beanDefinitionName);

                System.out.println("name = " + beanDefinitionName + "\n object = " + bean);
                System.out.println("----------------------------------------------------------------");

            }
        }
    }
} // Class ๋

 

  1. ๋ชจ๋“  ๋นˆ ์ถœ๋ ฅ
    • ์‹คํ–‰ ์‹œ Spring์— ๋“ฑ๋ก๋œ ๋ชจ๋“  ๋นˆ ์ •๋ณด ์ถœ๋ ฅ
    • AnnotationConfigApplicationContext.getBeanDefinitionNames() : ์Šคํ”„๋ง์— ๋“ฑ๋ก๋œ ๋ชจ๋“  ๋นˆ ์ด๋ฆ„ ์กฐํšŒ ์‹œ ์‚ฌ์šฉ
    • AnnotationConfigApplicationContext.getBean() : ๋นˆ ์ด๋ฆ„์œผ๋กœ ๋นˆ ๊ฐ์ฒด(์ธ์Šคํ„ด์Šค) ์กฐํšŒ ์‹œ ์‚ฌ์šฉ
  2. Application Bean Output
    • ์Šคํ”„๋ง ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋นˆ์„ ์ œ์™ธํ•˜๊ณ , ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ๋นˆ๋งŒ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•
    • ์Šคํ”„๋ง ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋นˆ์€ getRole()์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌ๋ถ„ ๊ฐ€๋Šฅ
      • ROLE_APPLICATION : ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ •์˜ํ•œ ๋นˆ
      • ROLE_INFRASTRUCTURE : ์Šคํ”„๋ง ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋นˆ

 

 

 

         ๐Ÿ‘‰ Spring Bean ์กฐํšŒ - ๊ธฐ๋ณธ

 

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์—์„œ ์Šคํ”„๋ง ๋นˆ์„ ์ฐพ๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ผ๊นŒ์š”?

  1. AnnotationConfigApplicationContext.getBean(BeanName, BeanType)
  2. AnnotationConfigApplicationContext.getBean(BeanType)
  3. ์กฐํšŒ ๋Œ€์ƒ ์Šคํ”„๋ง ๋นˆ์ด ์—†์„ ๊ฒฝ์šฐ ์˜ˆ์™ธ ๋ฐœ์ƒ
    • NoSuchBeanDefinitionException : No bean named '~~~~' available

 

package hello.core.beanfind;

import hello.core.AppConfig;
import hello.core.member.MemberService;
import hello.core.member.MemberServiceImpl;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class ApplicationContextBasicFindTest {

    AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);

    @Test @DisplayName("Bean Name์œผ๋กœ ์กฐํšŒ")
    void findBeanName() {
        MemberService memberService = ac.getBean("memberService", MemberService.class);

        Assertions.assertThat(memberService).isInstanceOf(MemberServiceImpl.class);
    }  // findBeanName() ๋

    @Test @DisplayName("์ด๋ฆ„ ์—†์ด ํƒ€์ž…์œผ๋กœ๋งŒ ์กฐํšŒ")
    void findBeanByType() {
        MemberService memberService = ac.getBean(MemberService.class);

        Assertions.assertThat(memberService).isInstanceOf(MemberServiceImpl.class);
    } // findBeanByType() ๋

    @Test @DisplayName("๊ตฌ์ฒด ํƒ€์ž…์œผ๋กœ ์กฐํšŒ")
    void findBeanName1() {  // ์ด๋ ‡๊ฒŒ ๊ตฌ์ฒดํ™”๋ฅผ ํ†ตํ•ด ์กฐํšŒ๋ฅผ ํ•˜๋Š” ๊ฒƒ์€ ์ข‹์ง€ ์•Š๋‹ค. ์™œ๋ƒํ•˜๋ฉด? ์ถ”์ƒํ™”์— ์˜์กดํ•ด์•ผ ํ•˜๋Š”๋ฐ, ๊ตฌ์ฒดํ™”์— ์˜์กดํ•˜๊ธฐ ๋•Œ๋ฌธ
        MemberService memberService = ac.getBean("memberService", MemberServiceImpl.class);

        Assertions.assertThat(memberService).isInstanceOf(MemberServiceImpl.class);
    } // findBeanByType() ๋

    @Test @DisplayName("๋นˆ ์ด๋ฆ„์œผ๋กœ ์กฐํšŒ๊ฐ€ ๋˜์ง€ ์•Š์„ ๋•Œ")
    void findBeanByNameX() { // Test๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” ์‹คํŒจ์— ๋Œ€ํ•œ ๊ฒƒ๋„ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.
        // ac.getBean("xxxx", MemberService.class); MemberService์— "xxxx"๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋œ Bean์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— NoSuchBeanDefinitionException Exception์ด ๋œฐ ๊ฒƒ์ด๋‹ค.

        org.junit.jupiter.api.Assertions.assertThrows(NoSuchBeanDefinitionException.class,
                () -> ac.getBean("xxxx", MemberService.class)); // ์˜ค๋ฅธ์ชฝ์— ์ธ์ž๊ฐ’์ด ์‹คํ–‰๋˜๋ฉด ์™ผ์ชฝ ์ธ์ž๊ฐ’ Exception์ด ํ„ฐ์ ธ์•ผ Test ์ •์ƒ์ด ๋‚˜์˜จ๋‹ค.
    } // findBeanByNameX() ๋

} // Class ๋

 

์ฐธ๊ณ ๋กœ ๊ตฌ์ฒด Type์œผ๋กœ ์กฐํšŒํ•˜๋ฉด ๋œ๋‹ค ์•ˆ๋œ๋‹ค? ์•ˆ ๋œ๋‹ค! ์™œ ์•ˆ๋œ๋‹ค? ์—ญํ• ๊ณผ ๊ตฌํ˜„์„ ๋‚˜๋ˆ ์•ผ ํ•˜๊ณ , DIP, OCS ์œ„๋ฐ˜์ด๋‹ค!

๊ทธ๋Ÿฌ๋ฏ€๋กœ, ๋ณ€๊ฒฝ ์‹œ ์œ ์—ฐ์„ฑ์ด ๋–จ์–ด์ง€๊ฒŒ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์˜ ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด์—์š”!

 

 

 

         ๐Ÿ‘‰ Spring Bean ์กฐํšŒ - ๋™์ผํ•œ ํƒ€์ž…์ด ๋‘˜ ์ด์ƒ์ผ ๋•Œ

 

๋งŒ์•ฝ ํƒ€์ž…์œผ๋กœ ์กฐํšŒํ•  ๋•Œ, ๊ฐ™์€ ํƒ€์ž…์˜ ์Šคํ”„๋ง ๋นˆ์ด ๋‘˜ ์ด์ƒ์ด๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด์—์š”. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋นˆ ์ด๋ฆ„์„ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์„๊ฒƒ์ด์—์š”.

AnnotationConfigApplicationContext.getBeanOfType()์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น ํƒ€์ž…์˜ ๋ชจ๋“  ๋นˆ์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค!

 

 

package hello.core.beanfind;

import hello.core.member.MemberRepository;
import hello.core.member.MemoryMemberRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

public class ApplicationContextBeanFindTest {

    AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SameBeanConfig.class);

    @Configuration
    static class SameBeanConfig { // Bean ์ด๋ฆ„์ด ๋‹ค๋ฅด๊ณ , ๊ฐ์ฒด ํƒ€์ž…(์ธ์Šคํ„ด์Šค ํƒ€์ž…)์ด ๊ฐ™๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

        @Bean
        public MemberRepository memberRepository1() {
            return new MemoryMemberRepository();
        } // memberRepository1() ๋

        @Bean
        public MemberRepository memberRepository2() {
            return new MemoryMemberRepository();
        } // memberRepository2() ๋
    } // SameBeanConfig Class ๋

    @Test @DisplayName("ํƒ€์ž…์œผ๋กœ ์กฐํšŒ ์‹œ ๊ฐ™์€ ํƒ€์ž…์ด ๋‘˜ ์ด์ƒ ์žˆ์œผ๋ฉด, ์ค‘๋ณต ์˜ค๋ฅ˜ ๋ฐœ์ƒ")
    void findBeanByTypeDuplicate() {
        MemberRepository bean = ac.getBean(MemberRepository.class);

        Assertions.assertThrows(NoUniqueBeanDefinitionException.class, () -> ac.getBean(MemberRepository.class));
    } // findBeanByTypeDuplicate() ๋

    @Test @DisplayName("ํƒ€์ž…์œผ๋กœ ์กฐํšŒ ์‹œ ๊ฐ™์€ ํƒ€์ž…์ด ๋‘˜ ์ด์ƒ ์žˆ์œผ๋ฉด, Bean ์ด๋ฆ„ ์ง€์ • ์‹œ ํ•ด๊ฒฐ")
    void findBeanByName() {
       MemberRepository memberRepository = ac.getBean("memberRepository1", MemberRepository.class);

        org.assertj.core.api.Assertions.assertThat(memberRepository).isInstanceOf(MemberRepository.class);

    } // findBeanByName() ๋

    @Test @DisplayName("ํŠน์ • ํƒ€์ž…์„ ๋ชจ๋‘ ์กฐํšŒํ•˜๊ธฐ")
    void findAllBeanByType() {
        Map<String, MemberRepository> beanOfType = ac.getBeansOfType(MemberRepository.class);

        for (String key : beanOfType.keySet()) {
            System.out.println("key = " + key +  "\n Value = " + beanOfType.get(key));
        }

        System.out.println("beanOfType = " + beanOfType);

        org.assertj.core.api.Assertions.assertThat(beanOfType.size()).isEqualTo(2);
    } // findAllBeanByType() ๋
} // Class ๋

 

 

 

         ๐Ÿ‘‰ Spring Bean ์กฐํšŒ - ์ƒ์† ๊ด€๊ณ„

 

๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ์กฐํšŒํ•˜๋ฉด ์ž์‹ ํƒ€์ž…๋„ ํ•จ๊ป˜ ์กฐํšŒ๋˜๋Š” ๊ฒƒ์ด์—์š”.

๋”ฐ๋ผ์„œ ๋ชจ๋“  ์ž๋ฐ” ๊ฐ์ฒด์˜ ์ตœ๊ณ  ๋ถ€๋ชจ ์•„๋‹ด๊ณผ ํ•˜์™€ ๊ฒฉ์ธ Java์˜ Object ํƒ€์ž…์œผ๋กœ ์กฐํšŒํ•˜๋ฉด ๋ชจ๋“  ์Šคํ”„๋ง ๋นˆ๋„ ์กฐํšŒ๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด์—์š”.

 

๋ฐ˜์‘ํ˜•

 

package hello.core.beanfind;

import hello.core.discount.DiscountPolicy;
import hello.core.discount.FixDiscountPolicy;
import hello.core.discount.RateDiscountPolicy;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

public class ApplicationContextExtendsFindTest {

    AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(TestConfig.class);

    @Configuration
    static class TestConfig {
        @Bean
        public DiscountPolicy rateDiscountPolicy() {
            return new RateDiscountPolicy();
        } // rateDiscountPolicy() ๋

        @Bean
        public DiscountPolicy fixDiscountPolicy() {
            return new FixDiscountPolicy();
        } // fixDiscountPolicy() ๋

    } // TestConfig Class ๋

    @Test
    @DisplayName("๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ์กฐํšŒ ์‹œ ์ž์‹์ด ๋‘˜ ์ด์ƒ ์žˆ์œผ๋ฉด, ์ค‘๋ณต ์˜ค๋ฅ˜ ๋ฐœ์ƒ")
    void findBeanByParentTypeDuplicate() {
//        DiscountPolicy bean = ac.getBean(DiscountPolicy.class);

        Assertions.assertThrows(NoUniqueBeanDefinitionException.class, () -> ac.getBean(DiscountPolicy.class));
    } // findBeanByParentTypeDuplicate() ๋

    @Test
    @DisplayName("๋ถ€๋ชจ ํƒ€์ž… ์กฐํšŒ ์‹œ ์ž์‹์ด ๋‘˜ ์ด์ƒ ์žˆ์œผ๋ฉด, Bean ์ด๋ฆ„์œผ๋กœ ์กฐํšŒํ•˜๋ฉด ๋œ๋‹ค.")
    void findBeanByParentTypeBeanName() {
        DiscountPolicy bean = ac.getBean("rateDiscountPolicy", DiscountPolicy.class);

        org.assertj.core.api.Assertions.assertThat(bean).isInstanceOf(RateDiscountPolicy.class);
    } // findBeanByParentTypeBeanName() ๋

    @Test
    @DisplayName("๋ถ€๋ชจ ํƒ€์ž… ์กฐํšŒ ์‹œ ์ž์‹์ด ๋‘˜ ์ด์ƒ ์žˆ์œผ๋ฉด, ํŠน์ • ํ•˜์œ„ ํƒ€์ž…์œผ๋กœ ์กฐํšŒ ๊ฐ€๋Šฅ(๊ตฌํ˜„์ฒด๋ฅผ ์˜์กดํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ข‹์ง€ ์•Š์€ ๋ฐฉ๋ฒ•)")
    void findBeanBySubType() {
        RateDiscountPolicy bean = ac.getBean(RateDiscountPolicy.class);

        org.assertj.core.api.Assertions.assertThat(bean).isInstanceOf(RateDiscountPolicy.class);
    } // findBeanBySubType() ๋

    @Test
    @DisplayName("๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ์ „๋ถ€ ์กฐํšŒ")
    void findAllBeanByPrentType() {
        Map<String, DiscountPolicy> beansOfType = ac.getBeansOfType(DiscountPolicy.class);

        org.assertj.core.api.Assertions.assertThat(beansOfType.size()).isEqualTo(2);

        for (String key : beansOfType.keySet()) {
            System.out.println("key = " + key + "\n Value = " + beansOfType.get(key));
        } // for๋ฌธ ๋
    } // findAllBeanByPrentType() ๋

    @Test
    @DisplayName("์ž๋ฐ”์˜ ์•„๋‹ด Object ํƒ€์ž…์œผ๋กœ ์ „๋ถ€ ์กฐํšŒ")
    void findAllBeanByObjectType() {
        Map<String, Object> beansOfType = ac.getBeansOfType(Object.class);

        org.assertj.core.api.Assertions.assertThat(beansOfType.size()).isEqualTo(16);

        for (String key : beansOfType.keySet()) {
            System.out.println("key = " + key + "\n value = " + beansOfType.get(key));
        } // for๋ฌธ ๋
    } // findAllBeanByObjectType() ๋
} // Class ๋

 

 

         ๐Ÿ‘‰ BeanFactory์™€ ApplicationContext

 

 

BeanFactory

  1. ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์˜ ์ตœ์ƒ์œ„ ์ธํ„ฐํŽ˜์ด์Šค
  2. ์Šคํ”„๋ง ๋นˆ์„ ๊ด€๋ฆฌํ•˜๊ณ , ์กฐํšŒํ•˜๋Š” ์—ญํ• 
  3. getBean() ์ œ๊ณต
  4. ์•ž ์จ ํ™•์ธํ–ˆ๋˜ ๋ชจ๋“  ๊ธฐ๋Šฅ์€ BeanFactory๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ

 

 

 

ApplicationContext

  1. BeanFactory ๊ธฐ๋Šฅ์„ ๋ชจ๋‘ ์ƒ์† ๋ฐ›์•„ ์ œ๊ณต
  2. ๋นˆ์„ ๊ด€๋ฆฌํ•˜๊ณ  ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ธฐ๋Šฅ์„ BeanFactory๊ฐ€ ์ œ๊ณต
  3. Application ๊ฐœ๋ฐœ ์‹œ ๋นˆ์„ ๊ด€๋ฆฌํ•˜๊ณ , ์กฐํšŒํ•˜๋Š” ๊ธฐ๋Šฅ์€ ๋ฌผ๋ก  ์ˆ˜ ๋งŽ์€ ๋ถ€๊ฐ€ ๊ธฐ๋Šฅ ํ•„์š”ํ•œ๋ฐ, ์ด๋ฅผ ์ œ๊ณต

 

  • MessageSource
    • ๋ฉ”์‹œ์ง€์†Œ์Šค๋ฅผ ํ™œ์šฉํ•œ ๊ตญ์ œํ™” ๊ธฐ๋Šฅ : ์˜ˆ๋ฅผ ๋“ค์–ด ํ•œ๊ตญ์—์„œ ์ ‘์†ํ•˜๋ฉด ํ•œ๊ตญ์–ด๋กœ ๋ณด์ด๊ฒŒ ํ•˜๊ณ , ๋‹ค๋ฅธ ์–ธ์–ด๊ถŒ์—์„œ ์ ‘์†ํ•˜๋ฉด ๊ทธ ๋‚˜๋ผ์˜ ์–ธ์–ด๋กœ ์ถœ๋ ฅ
  • EnvironmentCapable
    • ํ™˜๊ฒฝ ๋ณ€์ˆ˜ : ๊ฐœ์ธ PC์—์„œ ํ˜น์€ ๊ฐœ๋ฐœ ์„œ๋ฒ„์—์„œ ์šด์˜ ์„œ๋ฒ„์—์„œ ํ•˜๋Š” ์ผ์ด ๋ชจ๋‘ ๋‹ค๋ฅด ๋“ฏ ์ด๋ฅผ ๊ตฌ๋ถ„ํ•ด์„œ ์ฒ˜๋ฆฌ
  • ApplicationEventPublisher
    • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ด๋ฒคํŠธ : ์ด๋ฒคํŠธ๋ฅผ ๋ฐœํ–‰ํ•˜๊ณ , ๊ตฌ๋…ํ•˜๋Š” ๋ชจ๋ธ์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ง€์›
  • ResourceLoader
    • ํŽธ๋ฆฌํ•œ ์ž์› ์กฐํšŒ : File, Class Path, Remote(์™ธ๋ถ€) ๋“ฑ์—์„œ ์ž์›์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›

 

์ •๋ฆฌํ•˜์ž๋ฉด

  1. ApplicationContext๋Š” BeanFactory์˜ ๊ธฐ๋Šฅ ์ƒ์†
  2. ApplicationContext๋Š” ๋นˆ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ๊ณผ ๋”๋ถˆ์–ด ํŽธ๋ฆฌํ•œ ๋ถ€๊ฐ€ ๊ธฐ๋Šฅ ์ œ๊ณต
  3. BeanFactory๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•  ์ผ์€ ๊ฑฐ์˜ ์—†๊ณ , ๋ถ€๊ฐ€ ๊ธฐ๋Šฅ์ด ํฌํ•จ๋œ ApplicationContext ์‚ฌ์šฉ
  4. BeanFactory๋‚˜ ApplicationContext๊ฐ€ ๋ฐ”๋กœ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ

 

 

 

 

    ๐Ÿ“๋‹ค์–‘ํ•œ ์„ค์ • ํ˜•์‹ ์ง€์› - Java Code, XML

 

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” ๋‹ค์–‘ํ•œ ํ˜•์‹์˜ ์„ค์ • ์ •๋ณด๋ฅผ ๋ฐ›์•„๋“œ๋ฆด ์ˆ˜ ์žˆ๊ฒŒ ์œ ์—ฐํ•˜๊ฒŒ ๋งŒ๋“ค์–ด ๋†“์€ ๊ฒƒ์ด์—์š”.

์šฐ๋ฆฌ๋Š” ์ด๊ฒƒ์„ ํ†ตํ•ด ์ž๋ฐ” ์ฝ”๋“œ, XML, Groovy ๋“ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค!

 

 

 

 

         ๐Ÿ‘‰ Annotation ๊ธฐ๋ฐ˜ Java Code Configuration ์‚ฌ์šฉ

 

new AnnotaionConfigApplicationContext(AppConfig.class);

AnnotationConfigApplicationContext ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์ž๋ฐ” ์ฝ”๋“œ๋กœ ์ด๋ฃจ์–ด์ง„ ์„ค์ • ์ •๋ณด๋ฅผ ๋„˜๊ธฐ๋ฉด ๋˜๋Š” ๊ฒƒ์ด์—์š”.

 

 

        ๐Ÿ‘‰ XML Configuration ์‚ฌ์šฉ

 

์ตœ๊ทผ์—๋Š” ์Šคํ”„๋ง ๋ถ€ํŠธ๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋ฉด์„œ XML ๊ธฐ๋ฐ˜ ์„ค์ •์€ ์ž˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!

ํ•˜์ง€๋งŒ, ์•„์ง ๋งŽ์€ ๋ ˆ๊ฑฐ์‹œ ํ”„๋กœ์ ํŠธ๋“ค์ด XML๋กœ ์ด๋ค„์ ธ ์žˆ๊ณ , ๋˜ XML์„ ์‚ฌ์šฉํ•˜๋ฉด Compile ์—†์ด ๋นˆ ์„ค์ • ์ •๋ณด๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์ ๋„ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

GenericXmlApplicationContext๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, xml ์„ค์ • ํŒŒ์ผ์„ ๋„˜๊ธฐ๋ฉด ๋˜๋Š” ๊ฒƒ์ด์—์š”.

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id = "memberService" class = "hello.core.member.MemberServiceImpl">
        <constructor-arg name = "memberRepository" ref = "memberRepository" />
    </bean>

    <bean id = "memberRepository" class = "hello.core.member.MemoryMemberRepository"/>

    <bean id = "orderService" class = "hello.core.orderImsi.OrderServiceImpl">
        <constructor-arg name = "memberRepository" ref = "memberRepository"/>
        <constructor-arg name = "discountPolicy" ref = "discountPolicy"/>
    </bean>

    <bean id = "discountPolicy" class = "hello.core.discount.RateDiscountPolicy"/>
</beans>

 

 

package hello.core.xml;

import hello.core.member.MemberService;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class XmlAppContextTest {

    @Test void xmlAppContext() {
        ApplicationContext ac = new GenericXmlApplicationContext("appConfig.xml");

        MemberService memberService = ac.getBean("memberService", MemberService.class);

        Assertions.assertThat(memberService).isInstanceOf(MemberService.class);
    }
}

 

XML ๊ธฐ๋ฐ˜์˜ appConfig.xml ์Šคํ”„๋ง ์„ค์ • ์ •๋ณด์™€ ์ž๋ฐ” ์ฝ”๋“œ๋กœ ๋œ AppConfig.java ์„ค์ • ์ •๋ณด๋ฅผ ๋น„๊ตํ•ด ๋ณด๋ฉด ๊ฑฐ์˜ ๋˜‘๊ฐ™์€ ๊ฒƒ์ด์—์š”.

 

Spring ๊ณต์‹ ์ฐธ๊ณ  ๋ฌธ์„œ

 

Spring Framework

 

spring.io

 

 

 

    ๐Ÿ“์Šคํ”„๋ง ๋นˆ ์„ค์ • ๋ฉ”ํƒ€ ์ •๋ณด - BeanDefintion

 

์Šคํ”„๋ง์€ ์–ด๋–ป๊ฒŒ ์ด๋Ÿฐ ๋‹ค์–‘ํ•œ ์„ค์ • ํ˜•์‹์„ ์ง€์›ํ•˜๋Š” ๊ฒƒ์ผ๊นŒ์š”? ๊ทธ ์ค‘์‹ฌ์—๋Š” BeanDefinition์ด๋ผ๋Š” ์ถ”์ƒํ™”๋กœ ์ด๋ค„์ง„ ์นœ๊ตฌ๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

ํ•œ๋งˆ๋””๋กœ ์ด๊ฒƒ ์—ญ์‹œ '์—ญํ• ๊ณผ ๊ตฌํ˜„์„ ๊ฐœ๋…์ ์œผ๋กœ ๋‚˜๋ˆˆ ๊ฒƒ' ์ด์ง€์š”.

์ด๊ฒƒ์€ XML์„ ์ฝ์–ด์„œ BeanDefinition์„ ๋งŒ๋“ค๋ฉด ๋˜๋Š” ๊ฒƒ์ด์—์š”. ํ˜น์€ ์ž๋ฐ” ์ฝ”๋“œ๋ฅผ ์ฝ์–ด BeanDefinition์„ ๋งŒ๋“ค๋ฉด ๋˜๋Š” ๊ฒƒ์ด์ง€์š”.

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” ์ž๋ฐ” ์ฝ”๋“œ์ธ์ง€ XML์ธ์ง€ ์ƒ๊ด€ ์—†์–ด์š”! 'I don't care'์ธ ๊ฒƒ์ด์ด์ฃ . ์˜ค์ง BeanDefinition๋งŒ ์•Œ๋ฉด ๋˜๋Š” ๊ฒƒ์ด์—์š”.

BeanDefinition์„ ๋นˆ ์„ค์ • Meta Information (๋ฉ”ํƒ€ ์ •๋ณด)๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

์ž๋ฐ” ์ฝ”๋“œ์—์„œ๋Š” @Bean XML์—์„œ๋Š” <bean>์œผ๋กœ ๊ฐ๊ฐ ํ•˜๋‚˜์”ฉ Meta Information์ด ์ƒ์„ฑ๋œ๋‹ต๋‹ˆ๋‹ค!

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” ์ด ๋ฉ”ํƒ€์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์Šคํ”„๋ง ๋นˆ์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด์—์š”.

 

 

AnnotaionConfigApplicationContext๋Š” AnnotatedBeanDefinitionReader๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ AppConfig.class๋ฅผ ์ฝ์–ด ์˜จ ๋’ค BeanDefinition์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด์—์š”.

๋˜ํ•œ, GenericXmlApplicationContext๋Š” XmlBeanDefinitionReader๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ appConfig.xml์„ ์ฝ์–ด ์˜จ ๋’ค BeanDefinition์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด์—์š”.

๋งŒ์•ฝ ๊ฐœ๋ฐœ์ž๊ฐ€ ์Šค์Šค๋กœ ์ƒˆ๋กœ์šด ํ˜•์‹์˜ ์„ค์ • ์ •๋ณด๊ฐ€ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด, XxxBeanDefinitionReader๋ฅผ ๋งŒ๋“ค์–ด BEanDefinition์„ ๋งŒ๋“ค๋ฉด ๋˜๋Š” ๊ฒƒ์ด์—์š”.

 

 

        ๐Ÿ‘‰ BeanDefinition Information

  1. BeanClassName : ์ƒ์„ฑํ•  ๋นˆ์˜ ํด๋ž˜์Šค ์ด๋ฆ„(์ž๋ฐ” ์„ค์ •์ฒ˜๋Ÿผ Factory ์—ญํ• ์˜ ๋นˆ์„ ์‚ฌ์šฉํ•˜๋ฉด ์—†์Œ)
  2. factoryBeanName : Factory ์—ญํ• ์˜ ๋นˆ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ด๋ฆ„ ์˜ˆ:) appConfig
  3. factoryMethodName : ๋นˆ์„ ์ƒ์„ฑํ•  Factory Method ์ง€์ • ์˜ˆ:) memberService
  4. Scope : ์‹ฑ๊ธ€ํ†ค(Defulat Value)
  5. lazyinit : ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ, ๋นˆ์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์‹ค์ œ ๋นˆ์„ ์‚ฌ์šฉํ•  ๋•Œ๊นŒ์ง€ ์ตœ๋Œ€ํ•œ ์ƒ์„ฑ์„ ์ง€์—ฐ์ฒ˜๋ฆฌ ํ•˜๋Š”์ง€ ์—ฌ๋ถ€
    JPA์—์„œ N + 1๋ฌธ์ œ์— ํ•ด๊ฒฐ ๋ฐฉ์•ˆ์—์„œ ๋˜ ๋‚˜์˜ค๋Š” ๊ฐœ๋…
  6. InitMethodName : ๋นˆ์„ ์ƒ์„ฑํ•˜๊ณ , ์˜์กด๊ด€๊ณ„๋ฅผ ์ ์šฉํ•œ ๋’ค ํ˜ธ์ถœ๋˜๋Š” ์ดˆ๊ธฐํ™” Method ์ด๋ฆ„
  7. DestoryMethodName : ๋นˆ์˜ ์ƒ๋ช…์ฃผ๊ธฐ๊ฐ€ ๋๋‚˜์„œ ์ œ๊ฑฐํ•˜๊ธฐ ์ง์ „ ํ˜ธ์ถœ๋˜๋Š” Method ์ด๋ฆ„
  8. Constructor arguments, Properties : ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…์—์„œ ์‚ฌ์šฉ (์ž๋ฐ” ์„ค์ •์ฒ˜๋Ÿผ Factory ์—ญํ• ์˜ ๋นˆ ์‚ฌ์šฉ ์‹œ ์—†์Œ)

 

package hello.core.beandefinition;

import hello.core.AppConfig;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class BeanDefinitionTest {

    AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);

    @Test @DisplayName("Bean Configuration Meta Information Check")
    void findApplicationBean() {

        String[] beanDefinitionNames = ac.getBeanDefinitionNames();

        for (String beanDefinitionName : beanDefinitionNames) {
           BeanDefinition beanDefinition = ac.getBeanDefinition(beanDefinitionName);

           if (beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION) {
               System.out.println("beanDefinitionName = " + beanDefinitionName + "\n beanDefinition ์ž์ฒด ์ถœ๋ ฅ = " + beanDefinition);
           } // if ๋ฌธ ๋
        } // for ๋ฌธ ๋

    } // findApplicationBean() ๋

} // Class ๋

 

 

package hello.core.beandefinition;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.support.GenericXmlApplicationContext;

public class BeanDefinitionTestForXml {

    // ๋ณ€์ˆ˜ ์ž๋ฃŒํ˜• ํƒ€์ž…์„ ApplicationContext๋กœ ํ•˜์ง€ ์•Š๋Š” ์ด์œ ๋Š” ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด getBeanDefinition์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ
    GenericXmlApplicationContext ac = new GenericXmlApplicationContext("appConfig.xml");

    @Test @DisplayName("Bean Configuration Meta Information Check For XML")
    void findApplicationBean() {

        String[] beanDefinitionNames = ac.getBeanDefinitionNames();

        for (String beanDefinitionName : beanDefinitionNames) {
            BeanDefinition beanDefinition = ac.getBeanDefinition(beanDefinitionName);

            if (beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION) {
                System.out.println("beanDefinitionName = " + beanDefinitionName + "\n beanDefinition ์ž์ฒด ์ถœ๋ ฅ = " + beanDefinition);
            } // if ๋ฌธ ๋
        } // for ๋ฌธ ๋

    } // findApplicationBean() ๋
} // Class ๋

 

์ •๋ฆฌํ•˜์ž๋ฉด

BeanDefinition์„ ์ง์ ‘ ์ƒ์„ฑํ•˜์—ฌ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋กํ•  ์ˆ˜๋„ ์žˆ๋‹ต๋‹ˆ๋‹ค! ํ•˜์ง€๋งŒ, ์ด๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๊ฑฐ์˜ ์—†๋‹ค๊ณ  ํ•˜๋„ค์š”!

๊ฐ€๋” ์Šคํ”„๋ง ์ฝ”๋“œ๋‚˜ ์Šคํ”„๋ง ๊ด€๋ จ ์˜คํ”ˆ ์†Œํ”„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด BeanDefiniton์ด๋ผ๋Š” ๊ฒƒ์ด ๋ณด์ผ ๋•Œ๊ฐ€ ์žˆ๋Š”๋ฐ, ์ด ๋•Œ ์˜ค๋Š˜ ๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ๋– ์˜ฌ๋ฆฌ๋ฉด ๋˜๋Š” ๊ฒƒ์ด์—์š”!

 

 

 


 

์ฃผ๋‹ˆํ•˜๋ž‘์˜ ๊ธ€์ด ๋งˆ์Œ์— ๋“œ์…จ๋‚˜์š”? ๊ตฌ๋…๊ณผ ๊ณต๊ฐ! ๊ทธ๋ฆฌ๊ณ , ๋Œ“๊ธ€ ๊ทธ๋ฆฌ๊ณ  ๋ฐฉ๋ช…๋ก์€ ์ฃผ๋‹ˆํ•˜๋ž‘์—๊ฒŒ ๋งŽ์€ ํž˜์ด ๋ฉ๋‹ˆ๋‹ค


728x90
๋ฐ˜์‘ํ˜•