EEALL@ONCE
๐ฑ ์คํ๋ง ๋น ์กฐํ - ๊ธฐ๋ณธ / ๋์ผํ ํ์ ์ด ๋ ์ด์ ๋ณธ๋ฌธ
Spring๐ฑ
๐ฑ ์คํ๋ง ๋น ์กฐํ - ๊ธฐ๋ณธ / ๋์ผํ ํ์ ์ด ๋ ์ด์
์ฌ์ฃ์์ค 2023. 7. 25. 23:15728x90
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
'Spring๐ฑ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฑ BeanFactory๔งฌ& ApplicationContext (0) | 2023.07.29 |
---|---|
๐ฑ ์คํ๋ง ๋น ์กฐํ - ์์ ๊ด๊ณ getBean() / getBeansOfType(.class) (0) | 2023.07.29 |
๐ฑ ์คํ๋ง ์ปจํ ์ด๋ ์์ฑ (0) | 2023.07.23 |
๐ฑ ์์ํ ์๋ฐ ์ฝ๋๋ก DIํ ๋ถ๋ถ ์คํ๋ง์ผ๋ก ์ ํํ๊ธฐ (์คํ๋ง ์ปจํ ์คํธ/ ์คํ๋ง ์ปจํ ์ด๋) (0) | 2023.07.23 |
๐ฑIoC,DI,์ปจํ ์ด๋ (0) | 2023.07.23 |