EEALL@ONCE

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

Spring๐ŸŒฑ

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

์˜ฌ์—ฃ์›์Šค 2023. 7. 25. 23:15
728x90

1. ๊ธฐ๋ณธ 


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

 

import hello.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;

import static org.assertj.core.api.Assertions.assertThat;


public 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(MemberService.class);
    }

    @Test
    @DisplayName("๋นˆ ์ด๋ฆ„์œผ๋กœ ์กฐํšŒ ์•ˆ๋จ ํ…Œ์ŠคํŠธ")
    void notFindBeanByName(){
        org.junit.jupiter.api.Assertions.assertThrows(NoSuchBeanDefinitionException.class,()->ac.getBean("์•ˆ๋งŒ๋“ฆ",MemberService.class));
    }
}

 

2. ๋™์ผํ•œ ํƒ€์ž…์ด ๋‘˜ ์ด์ƒ

 

์ด๋ฆ„์œผ๋กœ ์กฐํšŒํ–ˆ์„ ๋•Œ๋Š” ์ƒ๊ด€์—†์œผ๋‚˜, ํƒ€์ž…์œผ๋กœ๋งŒ ์กฐํšŒํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ!

์ด๋ฆ„์„ ๋‹ค๋ฅด๊ฒŒ ์ง€์ •ํ•ด์„œ ์ด๋ฆ„์œผ๋กœ ์กฐํšŒํ•˜์ž.

package hello.core.beanfind;


import hello.core.member.MemberRepository;
import hello.core.member.MemoryMemberRepository;

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;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class ApplicationContextSameBean {

    AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(sameBeanConfig.class);

    @Configuration
    static class sameBeanConfig{
        @Bean
        public MemberRepository memberRepository1(){
            return new MemoryMemberRepository();
        }

        @Bean
        public MemberRepository memberRepository2(){
            return new MemoryMemberRepository();
        }

    }


    @Test
    @DisplayName("ํƒ€์ž…์œผ๋กœ๋งŒ ์กฐํšŒ ์‹œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ")
    void findByType(){
        assertThrows(NoUniqueBeanDefinitionException.class,()->ac.getBean(MemberRepository.class));
    }

    @Test
    @DisplayName("๋นˆ ์ด๋ฆ„์œผ๋กœ ์กฐํšŒ")
    void findByName(){
        MemberRepository memberRepository = ac.getBean("memberRepository1",MemberRepository.class);
        assertThat(memberRepository).isInstanceOf(MemberRepository.class);
    }

    @Test
    @DisplayName("๋ชจ๋‘ ์กฐํšŒ")
    void findAll(){
        Map<String,MemberRepository> beansOfType=ac.getBeansOfType(MemberRepository.class);
        for(String key : beansOfType.keySet()){
            System.out.println("key = " + key +"value" + beansOfType.get(key));
        }

        assertThat(beansOfType.size()).isEqualTo(2);
    }

}

์ด๋ฆ„ / ๋ฆฌํ„ด๊ฐ’์ฃผ์†Œ ๋กœ ๋ฐ˜ํ™˜ ํ•ด์คŒ 

 

 

728x90