프로토타입, 싱글톤 스코프 같이 쓸 때 궁금한 점이 있습니다!
179
작성한 질문수 53
안녕하세요, 항상 양질의 답글 감사드립니다.
이번 강의를 들으면서 싱글톤 스코프, 프로토타입 스코프를 같이 사용할 때 발생하는 일이 정확히 어떤지 궁금해서 문의드립니다.
public class SingletonTypeTestWithProtoTyepBean {
@Test
void prototypeTest(){
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(PrototypeBean.class);
PrototypeBean prototypeBean1 = ac.getBean(PrototypeBean.class);
PrototypeBean prototypeBean2 = ac.getBean(PrototypeBean.class);
prototypeBean1.addCount();
prototypeBean2.addCount();
System.out.println("prototypeBean2.getCount() = " + prototypeBean2.getCount());
System.out.println("prototypeBean1.getCount() = " + prototypeBean1.getCount());
Assertions.assertThat(prototypeBean1.getCount()).isEqualTo(prototypeBean2.getCount());
}
@Test
void SingletonTestWithProto(){
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ClientBean.class, PrototypeBean.class);
ClientBean bean1 = ac.getBean(ClientBean.class);
ClientBean bean2 = ac.getBean(ClientBean.class);
int count1 = bean1.logic();
int count2 = bean2.logic();
System.out.println("count1 = " + count1);
System.out.println("count2 = " + count2);
Assertions.assertThat(count1).isNotEqualTo(count2);
ac.close();
}
@RequiredArgsConstructor
@Scope("singleton")
static class ClientBean{
private final PrototypeBean prototypeBean;
public int logic(){
prototypeBean.addCount();
return prototypeBean.getCount();
}
@PostConstruct
public void initSingleton(){
System.out.println("ClientBean.initSingleton");
}
@PreDestroy
public void singltonend(){
System.out.println("ClientBean.singltonend");
}
}
@Scope("prototype")
static class PrototypeBean {
private int count = 0 ;
public void addCount() {
count++;
}
public int getCount(){
return count;
}
@PostConstruct
public void init(){
System.out.println("PrototypeBean.init");
}
@PreDestroy
public void destory(){
System.out.println("PrototypeBean.destory");
}
}
}
위 코드의 실행 결과는 아래와 같습니다.
코드 실행 결과를 바탕으로 실행 과정을 유추해보면 다음과 같은 것 같은데, 제가 생각하는 것이 맞는지 한번 알려주실 수 있을까요?
1. 스프링 컨테이너 생성과 함께 클라이언트 클래스의 빈 객체는 생성된다.
2. 클라이언트 클래스의 빈 객체는 생성자 주입을 통해서 되기 때문에, 생성되는 시점에서 프로토타입 클래스 빈 객체를 호출한다
3. 이 때 프로토타입 빈 객체는 생성되고, 의존관계 주입되고, 초기화 메서드까지 진행되고 스프링 컨테이너에 저장된다. (프로토타입 초기화 메서드 실행 로그 확인)
4. 스프링 컨테이너에서 프로토타입 빈 객체를 찾아와 클라이언트 클래스 빈 객체에 주입시켜준다. 이 때 스프링 컨테이너에서 프로토타입 빈 객체는 사라진다.
5. DI 단계는 이미 완료되었으며, 클라이언트 초기화 메서드 실행됨.
위의 단계로 진행되는 것이 맞을까요? 앞에 설명해주신 내용을 좀 더 생각해봐서 위와 같이 정리했는데... 뇌피셜로는 맞는 것 같은데, 정말로 맞을지 궁금합니다. 항상 감사드립니다.
답변 1
SingletonService가 JVM이 뜰 때 생성되는게 맞나요?
0
28
1
섹션3. 11 회원객체 다이어그램
0
36
1
OCP, DIP과 @Qualifier 어노테이션에 대해서 질문합니다.
0
35
1
코드 자료
0
80
2
구현체가 동적으로 정해질 때, 팩토리 기법을 사용하나요?
0
79
2
MemberService의 인터페이스를 왜 사용하는지 궁금합니다.
0
98
1
롬복 @Setter를 써야 하는 상황이 있는건가요?
0
104
1
빈 등록 메서드의 파라미터가 빈이 아니어도 되나요?
0
88
1
테스트 속도가 나중에 영향이 있을까요?
0
90
1
gradle 설정 안떠서 질문 남깁니다!
0
140
2
build.gradle로 프로젝트를 여는 이유
0
100
1
provider 사용하는 이유
0
97
1
다음 강의 뭘 들어야 할까요
0
139
2
프로토타입 빈, 직접 destroy 호출 안 할 경우
0
69
1
beanB
0
91
2
퀴즈다시풀기
0
75
1
Gradle로 바꿔도 오류가 똑같이 발생하네요 ㅠㅠ
0
102
2
"중복 등록과 충돌" 강의에서 강사님과 다른 에러가 발생합니다.
0
71
3
run 실행했는데 결과창이 이렇게 뜨네요 왜 그런건가요>
0
112
2
도메인의 정의?
0
63
1
ApplicationContext 질문입니다.
0
68
1
@Scope의 proxyMode를 사용할때 단위 테스트 방법
0
98
2
ai api 선정하기 관련 질문
0
133
2
생성자 자동주입 관련해서
0
68
1





