๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ด๋ก /Spring

[Spring] ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋ก๋œ ๋นˆ ์กฐํšŒ / Bean ์กฐํšŒ / ์ถœ๋ ฅ

by 6161990 2021. 7. 24.

๐Ÿ“Œ ์Šคํ”„๋ง ๋นˆ ์ถœ๋ ฅ

๋ชจ๋“  ๋นˆ ์ถœ๋ ฅํ•˜๊ธฐ

  • ์‹คํ–‰ํ•˜๋ฉด ์Šคํ”„๋ง์— ๋“ฑ๋ก๋œ ๋ชจ๋“  ๋นˆ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค. 
  • ac.getBeanDefinitionNames() : ์Šคํ”„๋ง์— ๋“ฑ๋ก๋œ ๋ชจ๋“  ๋นˆ ์ด๋ฆ„์„ ์กฐํšŒํ•œ๋‹ค. -> 28๋ฒˆ ๋ผ์ธ
  • ac.getBean(๋นˆ์ด๋ฆ„, ํƒ€์ž…) or ac.getBean(ํƒ€์ž…) : ๋นˆ ์ด๋ฆ„์œผ๋กœ ๋นˆ ๊ฐ์ฒด(์ธ์Šคํ„ด์Šค)๋ฅผ ์กฐํšŒํ•œ๋‹ค. -> 35๋ฒˆ ๋ผ์ธ

 

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋นˆ ์ถœ๋ ฅํ•˜๊ธฐ

  • ์Šคํ”„๋ง์ด ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋นˆ์€ ์ œ์™ธํ•˜๊ณ , ๋‚ด๊ฐ€ ๋“ฑ๋กํ•œ ๋นˆ๋งŒ ์ถœ๋ ฅํ•ด๋ณด์ž.
  • ์Šคํ”„๋ง์ด ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋นˆ์€ getRole()๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค. -> 34๋ฒˆ ๋ผ์ธ
  • ROLE_APPLICATION : ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•œ ๋นˆ
  • ROLE_INFRASTRUCTURE : ์Šคํ”„๋ง์ด ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋นˆ 

 

 

 

๐Ÿ“Œ ์Šคํ”„๋ง ๋นˆ ์กฐํšŒ - ๊ธฐ๋ณธ์ ์ธ ์กฐํšŒ๋ฐฉ๋ฒ•

  • ac.getBean(๋นˆ์ด๋ฆ„, ํƒ€์ž…)
  • ac.getBean(ํƒ€์ž…)
  • ์กฐํšŒ ๋Œ€์ƒ ์Šคํ”„๋ง์ด ์—†์„ ๊ฒฝ์šฐ NoSuchBeanDefinitionException : No bean named '000' available
  • **์ฐธ๊ณ ** ๊ตฌ์ฒด ํƒ€์ž…์œผ๋กœ ์กฐํšŒํ•˜๋ฉด ๋ณ€๊ฒฝ์‹œ ์œ ์—ฐ์„ฑ์ด ๋–จ์–ด์ง„๋‹ค.

 

 

  • ๋นˆ ์ด๋ฆ„์œผ๋กœ ์กฐํšŒํ•˜๊ธฐ :  findBeanByName()  ๋ฉ”์†Œ๋“œ
    AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);

    @Test
    @DisplayName("๋นˆ ์ด๋ฆ„์œผ๋กœ ์กฐํšŒ")
    void findBeanByName(){
        MemberService memberService = ac.getBean("memberService", MemberService.class);
        System.out.println("memberService = "+ memberService);
        System.out.println("memberService.getClass() = "+memberService.getClass());
        Assertions.assertThat(memberService).isInstanceOf(MemberServiceImpl.class);
    }

 

 

  • ์ด๋ฆ„์—†์ด ํƒ€์ž…์œผ๋กœ๋งŒ ์กฐํšŒ :  findBeanByType()  ๋ฉ”์†Œ๋“œ
@Test
@DisplayName("์ด๋ฆ„์—†์ด ํƒ€์ž…์œผ๋กœ๋งŒ ์กฐํšŒ")
void findBeanByType(){
  MemberService memberService = ac.getBean(MemberService.class);
  Assertions.assertThat(memberService).isInstanceOf(MemberServiceImpl.class);
}

 

 

  • ๊ตฌ์ฒดํƒ€์ž…์œผ๋กœ ์กฐํšŒ :  findBeanByName2()  ๋ฉ”์†Œ๋“œ
@Test
@DisplayName("๊ตฌ์ฒด ํƒ€์ž…์œผ๋กœ ์กฐํšŒ")
void findBeanByName2(){ //ํ•˜์ง€๋งŒ ๊ตฌ์ฒด์— ์˜์กดํ•˜๋Š” TEST์ด๊ธฐ ๋•Œ๋ฌธ์— ์ข‹์€ TEST๋ผ๊ณ ๋Š” ๋ณผ ์ˆ˜ ์—†์Œ, ์œ ์—ฐ์„ฑ์ด ๋–จ์–ด์ง
    MemberServiceImpl memberService = ac.getBean("memberService", MemberServiceImpl.class);
    System.out.println("memberService = "+ memberService);
    System.out.println("memberService.getClass() = "+memberService.getClass());
    Assertions.assertThat(memberService).isInstanceOf(MemberServiceImpl.class);
}

 

 

  • ๋นˆ ์ด๋ฆ„์œผ๋กœ ์กฐํšŒ ์‹œ, ํ•ด๋‹น ๋นˆ์„ ์ฐพ์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ  :  findBeanByNameX()  ๋ฉ”์†Œ๋“œ
@Test
@DisplayName("๋นˆ ์ด๋ฆ„์œผ๋กœ ์กฐํšŒX")
void findBeanByNameX(){
    // MemberService memberService = ac.getBean("xxxxx", MemberService.class);
    assertThrows(NoSuchBeanDefinitionException.class, ()->ac.getBean("xxxxx", MemberService.class));
    // ์˜ค๋ฅธ์ชฝ ํ•ญ์—์„œ ํ„ฐ์ง„ ์˜ˆ์™ธ๊ฐ€ ์™ผ์ชฝํ•ญ์ด๋ผ๋ฉด test ์„ฑ๊ณต
}

 

 


 

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

  • ํƒ€์ž…์œผ๋กœ ์กฐํšŒ์‹œ ๊ฐ™์€ ํƒ€์ž…์˜ ์Šคํ”„๋ง ๋นˆ์ด ๋‘˜ ์ด์ƒ์ด๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. -> ์ด๋ฆ„์„ ์ง€์ •ํ•ด์•ผํ•œ๋‹ค.
  • ac.getBeansOfType()์„ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ํƒ€์ž…์˜ ๋ชจ๋“  ๋นˆ์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

  • ํƒ€์ž…์œผ๋กœ ์กฐํšŒ ์‹œ, ๋นˆ์ด ๋‘˜ ์ด์ƒ์žˆ์œผ๋ฉด ์ค‘๋ณต ์˜ค๋ฅ˜ ๋ฐœ์ƒ :  findBeanByTypeDuplicate() 
  AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SameBeanConfig.class);
  
  @Test
  @DisplayName("ํƒ€์ž…์œผ๋กœ ์กฐํšŒ์‹œ ๊ฐ™์€ ํƒ€์ž…์ด ๋‘˜ ์ด์ƒ ์žˆ์œผ๋ฉด, ์ค‘๋ณต ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.")
  void findBeanByTypeDuplicate(){
    // ac.getBean(MemberRepository.class); ์—๋Ÿฌ
       assertThrows(NoUniqueBeanDefinitionException.class, ()->ac.getBean(MemberRepository.class));
  }

 

 

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

 

  • ํŠน์ • ํƒ€์ž…์„ ๋ชจ๋‘ ์กฐํšŒ : findAllBeanByName() 
@Test
@DisplayName("ํŠน์ • ํƒ€์ž…์„ ๋ชจ๋‘ ์กฐํšŒํ•˜๊ธฐ")
void findAllBeanType(){
  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);
}

 

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

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

 

 

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

 

 

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

 

 

  • ํŠน์ • ํ•˜์œ„ ํƒ€์ž…์„ ์กฐํšŒ :  findBeanBySubType()
@Test
@DisplayName("ํŠน์ • ํ•˜์œ„ ํƒ€์ž…์„ ์กฐํšŒ") //์ข‹์€ TEST๋Š” ์•„๋‹˜
void findBeanBySubType(){
	RateDiscountPolicy bean = ac.getBean(RateDiscountPolicy.class);
	assertThat(bean).isInstanceOf(RateDiscountPolicy.class);
}

 

 

  • ๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ๋ชจ๋‘ ์กฐํšŒ : findAllBeanParentType()
@Test
@DisplayName("๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ๋ชจ๋‘ ์กฐํšŒํ•˜๊ธฐ")
void findAllBeanParentType(){
  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));
  }
}

 

 

  • ๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ๋ชจ๋‘ ์กฐํšŒ - Object : findAllBeanObjectType()
@Test
@DisplayName("๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ๋ชจ๋‘ ์กฐํšŒํ•˜๊ธฐ - object") //์ž๋ฐ” beans์— ์žˆ๋Š” ๊ฐ์ฒด ๋ชจ๋‘ ์ถœ๋ ฅ๋จ
void findAllBeanObjectType(){
  Map<String, Object> beansOfType = ac.getBeansOfType(Object.class);
  for(String key : beansOfType.keySet()){
  	System.out.println("key = " + key + " value = "+ beansOfType.get(key));
  }
}