prototype scope에 proxy를 사용했을 경우 실패하는 이유가 뭔가요?
package hello.core.scope;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Provider;
import static org.assertj.core.api.Assertions.assertThat;
public class SingletonWithPrototypeTest2 {
@Test
void singletonClientUsePrototype() {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ClientBean.class, PrototypeBean.class);
ClientBean clientBean1 = ac.getBean(ClientBean.class);
int count1 = clientBean1.logic();
assertThat(count1).isEqualTo(1);
ClientBean clientBean2 = ac.getBean(ClientBean.class, PrototypeBean.class);
int count2 = clientBean2.logic();
assertThat(count2).isEqualTo(1);
}
@Scope("singleton")
static class ClientBean {
@Autowired
private PrototypeBean prototypeBean;
public int logic() {
PrototypeBean prototypeBean1 = prototypeBean;
prototypeBean1.addCount();
int count = prototypeBean1.getCount();
return count;
}
}
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
static class PrototypeBean {
private int count = 0;
public void addCount() {
System.out.println("count = " + count + " : " + this);
count++;
}
public int getCount() {
System.out.println("count = " + count + " : " + this);
return count;
}
@PostConstruct
public void init() {
System.out.println("PrototypeBean.init " + this);
}
@PreDestroy
public void destroy() {
System.out.println("PrototypeBean.destroy");
}
}
}
1, ProxyMode를 사용했을 경우 addCount() 와 getCount() 호출 시 같은 PrototypeBean을 사용하게 할 순 없나요?
2. 위에서 addCount() 와 getCount() 호출 시 다른 PrototypeBean이 생성되는 이유가 무엇인가요?
답변 1
회원 도메인 인터페이스 개발
0
42
1
빈 스코프 개념의 중요성
0
31
1
SingletonService가 JVM이 뜰 때 생성되는게 맞나요?
0
78
2
섹션3. 11 회원객체 다이어그램
0
42
1
OCP, DIP과 @Qualifier 어노테이션에 대해서 질문합니다.
0
44
1
코드 자료
0
91
2
구현체가 동적으로 정해질 때, 팩토리 기법을 사용하나요?
0
90
2
MemberService의 인터페이스를 왜 사용하는지 궁금합니다.
0
105
1
롬복 @Setter를 써야 하는 상황이 있는건가요?
0
107
1
빈 등록 메서드의 파라미터가 빈이 아니어도 되나요?
0
95
1
테스트 속도가 나중에 영향이 있을까요?
0
96
1
gradle 설정 안떠서 질문 남깁니다!
0
157
2
build.gradle로 프로젝트를 여는 이유
0
110
1
provider 사용하는 이유
0
109
1
다음 강의 뭘 들어야 할까요
0
143
2
프로토타입 빈, 직접 destroy 호출 안 할 경우
0
74
1
beanB
0
99
2
퀴즈다시풀기
0
81
1
Gradle로 바꿔도 오류가 똑같이 발생하네요 ㅠㅠ
0
111
2
"중복 등록과 충돌" 강의에서 강사님과 다른 에러가 발생합니다.
0
89
3
run 실행했는데 결과창이 이렇게 뜨네요 왜 그런건가요>
0
122
2
도메인의 정의?
0
72
1
ApplicationContext 질문입니다.
0
72
1
@Scope의 proxyMode를 사용할때 단위 테스트 방법
0
103
2





