해결된 질문
작성
·
193
1
@Test
@DisplayName("스프링 컨테이너와 싱글톤")
void springContainer() {
ApplicationContext applicationContext1 = new AnnotationConfigApplicationContext(AppConfig.class);
MemberService memberService1 = applicationContext1.getBean("memberService", MemberService.class);
MemberService memberService2 = applicationContext1.getBean("memberService", MemberService.class);
System.out.println("memberService1 = " + memberService1);
System.out.println("memberService2 = " + memberService2);
assertThat(memberService1).isSameAs(memberService2);
ApplicationContext applicationContext2 = new AnnotationConfigApplicationContext(AppConfig.class);
MemberService memberService3 = applicationContext2.getBean("memberService", MemberService.class);
assertThat(memberService1).isSameAs(memberService3);
}
답변 1
1
안녕하세요. 서한성님, 공식 서포터즈 OMG입니다.
ApplicationContext applicationContext2 = new AnnotationConfigApplicationContext(AppConfig.class);
ApplicationContext 자체가 스프링 컨테이너이기 때문에 서로 다른 스프링 컨테이너를 2개 생성하신 것으로
서로 다른 빈이 생성되는게 맞습니다.
질문에 대한 직접적인 답변은 아니지만 참고하시면 도움 되리라 생각합니다.
https://www.inflearn.com/questions/212497
감사합니다.