728x90
스프링없는 순수한 DI 컨테이너인 AppConfig는 요청할 때 마다 객채를 새로 생성한다.
예를 들어, 클라이언트 A, B, C가 MemberService를 요청하면 각자 3번 MemberServiceImpl를 요청하게 된다.
고객 트래픽이 초당 100이 나오면 초당 100개 객체가 생성되고 소멸된다! 메모리 낭비가 심하다.
▶ 해결방안 : 해당 객체가 딱 1개만 생성되고, 공유하도록 설계하면 된다. => 싱글톤 패턴
테스트 코드
import hello.core.AppConfig;
import hello.core.member.MemberService;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;
public class SingletonTest {
@Test
@DisplayName("스프링 없는 순수한 DI 컨테이너")
void pureContainer() {
AppConfig appConfig = new AppConfig();
//1. 조회: 호출할 때 마다 객체를 생성
MemberService memberService1 = appConfig.memberService();
//2. 조회: 호출할 때 마다 객체를 생성
MemberService memberService2 = appConfig.memberService();
//참조값이 다른 것을 확인
System.out.println("memberService1 = " + memberService1);
System.out.println("memberService2 = " + memberService2);
//memberService1 != memberService2
assertThat(memberService1).isNotSameAs(memberService2);
}
}
'공부 > Spring' 카테고리의 다른 글
[Spring] 싱글톤 컨테이너 (0) | 2023.11.20 |
---|---|
[Spring] 싱글톤 패턴 (0) | 2023.11.16 |
[Spring] 스프링 빈 설정 메타 정보 - BeanDefinition (0) | 2023.11.10 |
[Spring] 다양한 설정 형식 지원 - 자바 코드, XMl (0) | 2023.11.09 |
[Spring] Beanfactory와 ApplicationContext (0) | 2023.11.07 |