[Spring] 4.์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์™€ ์Šคํ”„๋ง ๋นˆ

 

1. ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ ๊ณผ์ •

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);

- ApplicationContext : ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ (์ธํ„ฐํŽ˜์ด์Šค)

- xml ๊ธฐ๋ฐ˜์œผ๋กœ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๊ณ  Annotation ๊ธฐ๋ฐ˜์˜ ์ž๋ฐ” ์„ค์ • ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค.

- AppConfig ๋ฐฉ์‹ -> Annoation ๊ธฐ๋ฐ˜ ์ž๋ฐ” ์„ค์ • ํด๋ž˜์Šค

- new AnnotaionConfigApplicationContext(AppConfig.class) = ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„์ฒด

 

* ๋” ์ •ํ™•ํžˆ๋Š” BeanFactory / ApplicationContext๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ด์•ผ๊ธฐํ•จ

* BeanFactory๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๊ฑฐ์˜ ์—†์Œ

 

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์˜ ์ƒ์„ฑ ๊ณผ์ •

1) ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ

- new AnnotaionConfigApplicationContext(AppConfig.class)

- ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•  ๋• ๊ตฌ์„ฑ ์ •๋ณด๋ฅผ ์ €์žฅํ•ด์ค˜์•ผํ•จ (AppConfig.class)

 

2) ์Šคํ”„๋ง ๋นˆ ๋“ฑ๋ก

- ์„ค์ • ์ •๋ณด(AppConfig.class) ๋„˜๊ฒจ์ฃผ๋ฉด ๊ทธ๊ฑธ ๋ณด๋ฉด์„œ @Bean ๋ถ™์€๊ฑฐ ํ˜ธ์ถœํ•ด์„œ ๋“ฑ๋ก

  [๋นˆ์ด๋ฆ„] = ๋ฉ”์„œ๋“œ๋ช…, [๋นˆ๊ฐ์ฒด] = ๋ฐ˜ํ™˜ ๊ฐ์ฒด

 

* ๋นˆ์ด๋ฆ„ ์ง์ ‘ ๋ถ€์—ฌ ๊ฐ€๋Šฅ @Bean(name="memberService2")

* ์ฃผ์˜) ๋นˆ ์ด๋ฆ„์€ ํ•ญ์ƒ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ๋ถ€์—ฌํ•ด์•ผํ•จ

 

3) ์Šคํ”„๋ง ๋นˆ ์˜์กด๊ด€๊ณ„ ์„ค์ • - ์ค€๋น„

 

4) ์Šคํ”„๋ง ๋นˆ ์˜์กด๊ด€๊ณ„ ์„ค์ • - ์™„๋ฃŒ

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

 

* ์ฐธ๊ณ ) ์Šคํ”„๋ง ๋นˆ์„ ์ƒ์„ฑํ•˜๊ณ  ์˜์กด๊ด€๊ณ„ ์ฃผ์ž… ๋‹จ๊ณ„ ๋‚˜๋ˆ ์ ธ์žˆ์Œ

            ํ•˜์ง€๋งŒ ์ž๋ฐ”์ฝ”๋“œ๋กœ ์Šคํ”„๋ง ๋นˆ ๋“ฑ๋กํ•˜๋ฉด ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…๋„ ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌ๋จ

            ๊ทธ๋Ÿฐ๋ฐ ์Šคํ”„๋ง ๋ผ์ดํ”„ ์‚ฌ์ดํด์€ ๋‚˜๋ˆ ์ ธ์žˆ์Œ (๋‚˜์ค‘์— ์„ค๋ช…)


2. ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋ก๋œ ๋ชจ๋“  ๋นˆ ์กฐํšŒ

ใ„ดhello.cored.beanfind

class ApplicationContextInfoTest {
  AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);

  @Test
  @DisplayName("๋ชจ๋“  ๋นˆ ์ถœ๋ ฅํ•˜๊ธฐ")
  void findAllBean() {
    String[] beanDefinitionNames = ac.getBeanDefinitionNames();
    for (String beanDefinitionName : beanDefinitionNames) {
      Object bean = ac.getBean(beanDefinitionName);
      System.out.println("name=" + beanDefinitionName + " object=" + bean);
    }
  }
  
  @Test
  @DisplayName("์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋นˆ ์ถœ๋ ฅํ•˜๊ธฐ") 
  void findApplicationBean() {
    String[] beanDefinitionNames = ac.getBeanDefinitionNames();
    for (String beanDefinitionName : beanDefinitionNames) {
      BeanDefinition beanDefinition = ac.getBeanDefinition(beanDefinitionName);
      //Role ROLE_APPLICATION: ์ง์ ‘ ๋“ฑ๋กํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋นˆ
      //Role ROLE_INFRASTRUCTURE: ์Šคํ”„๋ง์ด ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋นˆ
      if (beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION) {
      Object bean = ac.getBean(beanDefinitionName);
      System.out.println("name=" + beanDefinitionName + " object=" + bean);
    }
  }
}

3. ์Šคํ”„๋ง ๋นˆ ์กฐํšŒ - ๊ธฐ๋ณธ

 

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

- ac.getBean(๋นˆ์ด๋ฆ„, ํƒ€์ž…)

- ac.getBean(ํƒ€์ž…)

์กฐํšŒ ๋Œ€์ƒ ์Šคํ”„๋ง ๋นˆ์ด ์—†์œผ๋ฉด ์˜ˆ์™ธ ๋ฐœ์ƒ : NoSuchBeanDefinitionException: No bean named 'xxxxx' available

 

class ApplicationContextBasicFindTest {
  AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
  
  @Test
  @DisplayName("๋นˆ ์ด๋ฆ„์œผ๋กœ ์กฐํšŒ")
  void findBeanByName() {
    MemberService memberService = ac.getBean("memberService",MemberService.class);
    assertThat(memberService).isInstanceOf(MemberServiceImpl.class);
  } 
  
  @Test
  @DisplayName("์ด๋ฆ„ ์—†์ด ํƒ€์ž…๋งŒ์œผ๋กœ ์กฐํšŒ") 
  void findBeanByType() {
    MemberService memberService = ac.getBean(MemberService.class);
    assertThat(memberService).isInstanceOf(MemberServiceImpl.class);
  }

  @Test
  @DisplayName("๊ตฌ์ฒด ํƒ€์ž…์œผ๋กœ ์กฐํšŒ")
  void findBeanByName2() {
    MemberServiceImpl memberService = ac.getBean("memberService",MemberServiceImpl.class);
    assertThat(memberService).isInstanceOf(MemberServiceImpl.class);
  } 
  
  @Test
  @DisplayName("๋นˆ ์ด๋ฆ„์œผ๋กœ ์กฐํšŒX") 
  void findBeanByNameX() {
    //ac.getBean("xxxxx", MemberService.class);
    Assertions.assertThrows(NoSuchBeanDefinitionException.class, () ->
    ac.getBean("xxxxx", MemberService.class));
  } 
}

* ์ฐธ๊ณ ) ๊ตฌ์ฒด ํƒ€์ž…์œผ๋กœ ์กฐํšŒํ•˜๋ฉด ๋ณ€๊ฒฝ์‹œ ์œ ์—ฐ์„ฑ์ด ๋–จ์–ด์ง„๋‹ค.

* ๋งˆ์ง€๋ง‰ ํ…Œ์ŠคํŠธ์—์„œ ์˜ค๋ฅธ์ชฝ ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ–ˆ์„๋•Œ ์™ผ์ชฝ Exception์ด ๋ฐœ์ƒํ•ด์•ผ ํ…Œ์ŠคํŠธ ์„ฑ๊ณต


4. ์Šคํ”„๋ง ๋นˆ ์กฐํšŒ - ๋™์ผํ•œ ํƒ€์ž…์ด ๋‘˜ ์ด์ƒ

- ํƒ€์ž…์œผ๋กœ ์กฐํšŒ์‹œ ๊ฐ™์€ ํƒ€์ž…์˜ ์Šคํ”„๋ง ๋นˆ์ด ๋‘˜ ์ด์ƒ์ด๋ฉด ์˜ค๋ฅ˜ ๋ฐœ์ƒ

  ์ด๋•Œ ๋นˆ ์ด๋ฆ„์„ ์ง€์ •ํ•˜์ž

- ac.getBeansOfType()์„ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ํƒ€์ž…์˜ ๋ชจ๋“  ๋นˆ์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

 

class ApplicationContextSameBeanFindTest {
  AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SameBeanConfig.class);
  
  @Test
  @DisplayName("ํƒ€์ž…์œผ๋กœ ์กฐํšŒ์‹œ ๊ฐ™์€ ํƒ€์ž…์ด ๋‘˜ ์ด์ƒ ์žˆ์œผ๋ฉด, ์ค‘๋ณต ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค") 
  void findBeanByTypeDuplicate() {
    //MemberRepository bean = ac.getBean(MemberRepository.class);
    assertThrows(NoUniqueBeanDefinitionException.class, () -> ac.getBean(MemberRepository.class));
  } 
  
  @Test
  @DisplayName("ํƒ€์ž…์œผ๋กœ ์กฐํšŒ์‹œ ๊ฐ™์€ ํƒ€์ž…์ด ๋‘˜ ์ด์ƒ ์žˆ์œผ๋ฉด, ๋นˆ ์ด๋ฆ„์„ ์ง€์ •ํ•˜๋ฉด ๋œ๋‹ค") 
  void findBeanByName() {
    MemberRepository memberRepository = ac.getBean("memberRepository1", MemberRepository.class);
    assertThat(memberRepository).isInstanceOf(MemberRepository.class);
  }
  
  @Test
  @DisplayName("ํŠน์ • ํƒ€์ž…์„ ๋ชจ๋‘ ์กฐํšŒํ•˜๊ธฐ")
  void findAllBeanByType() {
    Map<String, MemberRepository> beansOfType = ac.getBeansOfType(MemberRepository.class);
    for (String key : beansOfType.keySet()) {
      System.out.println("key = " + key + " value = " + beansOfType.get(key));
    }
    System.out.println("beansOfType = " + beansOfType);
    assertThat(beansOfType.size()).isEqualTo(2);
  }
  
  @Configuration
  static class SameBeanConfig {
    @Bean
    public MemberRepository memberRepository1() {
      return new MemoryMemberRepository();
    }
    
    @Bean
    public MemberRepository memberRepository2() {
      return new MemoryMemberRepository();
    }
  }
}

5. ์Šคํ”„๋ง ๋นˆ ์กฐํšŒ - ์ƒ์†๊ด€๊ณ„

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

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

class ApplicationContextExtendsFindTest {
  AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(TestConfig.class);
  
  @Test
  @DisplayName("๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ์กฐํšŒ์‹œ, ์ž์‹์ด ๋‘˜ ์ด์ƒ ์žˆ์œผ๋ฉด, ์ค‘๋ณต ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค") 
  void findBeanByParentTypeDuplicate() {
    //DiscountPolicy bean = ac.getBean(DiscountPolicy.class);
    assertThrows(NoUniqueBeanDefinitionException.class, () ->
    ac.getBean(DiscountPolicy.class));
  }
  
  @Test
  @DisplayName("๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ์กฐํšŒ์‹œ, ์ž์‹์ด ๋‘˜ ์ด์ƒ ์žˆ์œผ๋ฉด, ๋นˆ ์ด๋ฆ„์„ ์ง€์ •ํ•˜๋ฉด ๋œ๋‹ค")
  void findBeanByParentTypeBeanName() {
    DiscountPolicy rateDiscountPolicy = ac.getBean("rateDiscountPolicy",DiscountPolicy.class);
    assertThat(rateDiscountPolicy).isInstanceOf(RateDiscountPolicy.class);
  } 
  
  @Test
  @DisplayName("ํŠน์ • ํ•˜์œ„ ํƒ€์ž…์œผ๋กœ ์กฐํšŒ") 
  void findBeanBySubType() {
    RateDiscountPolicy bean = ac.getBean(RateDiscountPolicy.class);
    assertThat(bean).isInstanceOf(RateDiscountPolicy.class);
  }
  
  @Test
  @DisplayName("๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ๋ชจ๋‘ ์กฐํšŒํ•˜๊ธฐ")
  void findAllBeanByParentType() {
    Map<String, DiscountPolicy> beansOfType =
    ac.getBeansOfType(DiscountPolicy.class);
    assertThat(beansOfType.size()).isEqualTo(2);
    for (String key : beansOfType.keySet()) {
      System.out.println("key = " + key + " value=" + beansOfType.get(key));
    } 
  }
  
  @Test
  @DisplayName("๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ๋ชจ๋‘ ์กฐํšŒํ•˜๊ธฐ - Object")
  void findAllBeanByObjectType() {
    Map<String, Object> beansOfType = ac.getBeansOfType(Object.class);
    for (String key : beansOfType.keySet()) {
      System.out.println("key = " + key + " value=" + beansOfType.get(key));
    } 
  }

  @Configuration
  static class TestConfig {
    @Bean
    public DiscountPolicy rateDiscountPolicy() {
      return new RateDiscountPolicy();
    }
    @Bean
    public DiscountPolicy fixDiscountPolicy() {
      return new FixDiscountPolicy();
    }
  } 
}

6. BeanFactory์™€ ApplicationContext

BeanFactory

- ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์˜ ์ตœ์ƒ์œ„ ์ธํ„ฐํŽ˜์ด์Šค

- ์Šคํ”„๋ง ๋นˆ์„ ๊ด€๋ฆฌํ•˜๊ณ  ์กฐํšŒํ•˜๋Š” ์—ญํ• 

- getBean()์ œ๊ณต

- ์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ–ˆ๋˜ ๋Œ€๋ถ€๋ถ„์˜ ๊ธฐ๋Šฅ์€ BeanFactory๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ

 

ApplicationContext

- BeanFactory๊ธฐ๋Šฅ์„ ๋ชจ๋‘ ์ƒ์†ํ•ด์„œ ์ œ๊ณต

- ๋นˆ์„ ๊ด€๋ฆฌํ•˜๊ณ  ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ธฐ๋Šฅ์„ BeanFactory๊ฐ€ ์ œ๊ณตํ•ด์ฃผ๋Š”๋ฐ, ๊ทธ๋Ÿฌ๋ฉด ๋‘˜์˜ ์ฐจ์ด๋Š”?

  ์—ฌ๋Ÿฌ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์•„์„œ ์‚ฌ์šฉ

- ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•  ๋• ๋นˆ์„ ๊ด€๋ฆฌํ•˜๊ณ  ์กฐํšŒํ•˜๋Š” ๊ธฐ๋Šฅ + ์ˆ˜ ๋งŽ์€ ๋ถ€๊ฐ€๊ธฐ๋Šฅ์ด ํ•„์š”

- ๋ฉ”์„ธ์ง€ ์†Œ์Šค๋ฅผ ํ™œ์šฉํ•œ ๊ตญ์ œํ™” ๊ธฐ๋Šฅ : ์˜ˆ๋ฅผ ๋“ค์–ด ํ•œ๊ตญ์—์„œ ๋“ค์–ด์˜ค๋ฉด ํ•œ๊ตญ์–ด๋กœ, ์˜์–ด๊ถˆ๋„ˆ์—์„œ ๋“ค์–ด์˜ค๋ฉด ์˜์–ด๋กœ ์ถœ๋ ฅ

- ํ™˜๊ฒฝ๋ณ€์ˆ˜ : ๋กœ์ปฌ, ๊ฐœ๋ฐœ, ์šด์˜ ๋“ฑ์„ ๊ตฌ๋ถ„ํ•ด์„œ ์ฒ˜๋ฆฌ

- ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ด๋ฒคํŠธ : ์ด๋ฒคํŠธ ๋ฐœํ–‰ ๋ฐ ๊ตฌ๋…ํ•˜๋Š” ๋ชจ๋ธ์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ง€์›

- ํŽธ๋ฆฌํ•œ ๋ฆฌ์†Œ์Šค ์กฐํšŒ : ํŒŒ์ผ, ํด๋ž˜์ŠคํŒจ์Šค, ์™ธ๋ถ€ ๋“ฑ์—์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ์กฐํšŒ


7. ๋‹ค์–‘ํ•œ ์„ค์ • ํ˜•์‹ ์ง€์› - ์ž๋ฐ”์ฝ”๋“œ, XML

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

  ex) ์ž๋ฐ”์ฝ”๋“œ, XML, Grovvy

XML ์„ค์ • ์‚ฌ์šฉ

- ๋ ˆ๊ฑฐ์‹œ ํ”„๋กœ์ ํŠธ

- ์ปดํŒŒ์ผ ์—†์ด ๋นˆ ์„ค์ • ์ •๋ณด ๋ณ€๊ฒฝ (์žฅ์ )

- GenericXmlApplication ์‚ฌ์šฉํ•˜์—ฌ xml ์„ค์ • ํŒŒ์ผ ๋„˜๊ธฐ๋ฉด ๋จ

 

XmlAppConfig ์‚ฌ์šฉ ์ž๋ฐ” ์ฝ”๋“œ

public class XmlAppContext {
  @Test
  void xmlAppContext() {
    ApplicationContext ac = new GenericXmlApplicationContext("appConfig.xml");
    MemberService memberService = ac.getBean("memberService", MemberService.class);
    assertThat(memberService).isInstanceOf(MemberService.class);
  }
}

 

xml ๊ธฐ๋ฐ˜ ์Šคํ”„๋ง ๋นˆ ์„ค์ • ์ •๋ณด

ใ„ด src/main/resources/appConfig.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.order.OrderServiceImpl">
    <constructor-arg name="memberRepository" ref="memberRepository" />
    <constructor-arg name="discountPolicy" ref="discountPolicy" />
  </bean>
  <bean id="discountPolicy" class="hello.core.discount.RateDiscountPolicy" />
</beans>

- AppConfig์™€ ๊ฑฐ์˜ ๋น„์Šท


8. ์Šคํ”„๋ง ๋นˆ ์„ค์ • ๋ฉ”ํƒ€ ์ •๋ณด - BeanDefinition

- ์Šคํ”„๋ง ์–ด๋–ป๊ฒŒ ๋‹ค์–‘ํ•œ ์„ค์ • ์ง€์›? : BeanDefinition์ด๋ผ๋Š” ์ถ”์ƒํ™”!

- ์—ญํ• ๊ณผ ๊ตฌํ˜„์„ ์ž˜ ๋‚˜๋ˆˆ๊ฒƒ

- BeanDefinition์„ ๋นˆ ์„ค์ • ๋ฉ”ํƒ€์ •๋ณด๋ผ๊ณ ํ•œ๋‹ค.

- @Bean, <bean> ๋‹น ๊ฐ๊ฐ ํ•˜๋‚˜์”ฉ ๋ฉ”ํƒ€์ •๋ณด๊ฐ€ ์ƒ์„ฑ๋จ

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

- AnnotaionConfigApplicationContext๋Š” AnnotationBeanDefinitionreader๋ฅผ ์‚ฌ์šฉํ•ด์„œ AppConfig.class๋ฅผ ์ฝ๊ณ  BeanDefinition์„ ์ƒ์„ฑ

- GenericXmlApplicationContext๋Š” XmlBeanDefinitionReader๋ฅผ ์‚ฌ์šฉํ•ด์„œ appConfig.xml ์„ค์ • ์ •๋ณด๋ฅผ ์ฝ๊ณ  BeanDefinition์„ ์ƒ์„ฑ

- ์ƒˆ๋กœ์šด ํ˜•์‹์˜ ์„ค์ • ์ •๋ณด๊ฐ€ ์ถ”๊ฐ€๋˜๋ฉด XmlBeanDefinitionReader๋ฅผ ๋งŒ๋“ค์–ด์„œ BeanDefinition์„ ์ƒ์„ฑํ•˜๋ฉด ๋จ

 

BeanDefinition ์ •๋ณด

- BeanClassName: ์ƒ์„ฑํ•  ๋นˆ์˜ ํด๋ž˜์Šค ๋ช…(์ž๋ฐ” ์„ค์ • ์ฒ˜๋Ÿผ ํŒฉํ† ๋ฆฌ ์—ญํ• ์˜ ๋นˆ์„ ์‚ฌ์šฉํ•˜๋ฉด ์—†์Œ)

- factoryBeanName: ํŒฉํ† ๋ฆฌ ์—ญํ• ์˜ ๋นˆ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ด๋ฆ„, ์˜ˆ)

- appConfig factoryMethodName: ๋นˆ์„ ์ƒ์„ฑํ•  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ ์ง€์ •, ์˜ˆ) memberService

- Scope: ์‹ฑ๊ธ€ํ†ค(๊ธฐ๋ณธ๊ฐ’)

- lazyInit: ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๋นˆ์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์‹ค์ œ ๋นˆ์„ ์‚ฌ์šฉํ•  ๋•Œ ๊นŒ์ง€ ์ตœ๋Œ€ํ•œ ์ƒ์„ฑ์„ ์ง€์—ฐ์ฒ˜๋ฆฌ ํ•˜๋Š”์ง€ ์—ฌ๋ถ€

- InitMethodName: ๋นˆ์„ ์ƒ์„ฑํ•˜๊ณ , ์˜์กด๊ด€๊ณ„๋ฅผ ์ ์šฉํ•œ ๋’ค์— ํ˜ธ์ถœ๋˜๋Š” ์ดˆ๊ธฐํ™” ๋ฉ”์„œ๋“œ ๋ช…

- DestroyMethodName: ๋นˆ์˜ ์ƒ๋ช…์ฃผ๊ธฐ๊ฐ€ ๋๋‚˜์„œ ์ œ๊ฑฐํ•˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ ๋ช…

- Constructor arguments, Properties: ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…์—์„œ ์‚ฌ์šฉํ•œ๋‹ค. (์ž๋ฐ” ์„ค์ • ์ฒ˜๋Ÿผ ํŒฉํ† ๋ฆฌ ์—ญํ• ์˜ ๋นˆ์„ ์‚ฌ์šฉํ•˜๋ฉด ์—†์Œ)

public class BeanDefinitionTest {
  AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
  //    GenericXmlApplicationContext ac = new
  GenericXmlApplicationContext("appConfig.xml");
  
  @Test
  @DisplayName("๋นˆ ์„ค์ • ๋ฉ”ํƒ€์ •๋ณด ํ™•์ธ")
  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 + " beanDefinition = " + beanDefinition);
      }
    }
  }
}

์ •๋ฆฌ

- BeanDefinition์„ ์ง์ ‘ ์ƒ์„ฑํ•ด์„œ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋กํ•  ์ˆ˜ ๋„ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์‹ค๋ฌด์—์„œ BeanDefinition์„ ์ง์ ‘ ์ •์˜ํ•˜๊ฑฐ๋‚˜ ์‚ฌ์šฉํ•  ์ผ์€ ๊ฑฐ์˜ ์—†๋‹ค. ์–ด๋ ค์šฐ๋ฉด ๊ทธ๋ƒฅ ๋„˜์–ด๊ฐ€๋ฉด ๋œ๋‹ค^^!

- BeanDefinition์— ๋Œ€ํ•ด์„œ๋Š” ๋„ˆ๋ฌด ๊นŠ์ด์žˆ๊ฒŒ ์ดํ•ดํ•˜๊ธฐ ๋ณด๋‹ค๋Š”, ์Šคํ”„๋ง์ด ๋‹ค์–‘ํ•œ ํ˜•ํƒœ์˜ ์„ค์ • ์ •๋ณด๋ฅผ BeanDefinition์œผ๋กœ ์ถ”์ƒํ™”ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์ •๋„๋งŒ ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค.

- ๊ฐ€๋” ์Šคํ”„๋ง ์ฝ”๋“œ๋‚˜ ์Šคํ”„๋ง ๊ด€๋ จ ์˜คํ”ˆ ์†Œ์Šค์˜ ์ฝ”๋“œ๋ฅผ ๋ณผ ๋•Œ, BeanDefinition ์ด๋ผ๋Š” ๊ฒƒ์ด ๋ณด์ผ ๋•Œ๊ฐ€ ์žˆ๋‹ค. ์ด๋•Œ ์ด๋Ÿฌํ•œ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ๋– ์˜ฌ๋ฆฌ๋ฉด ๋œ๋‹ค.

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ