2023. 8. 27. 19:03ㆍSpring/[inflearn]스프링 핵심 원리 - 기본편
-> 스프링 컨테이너에 프로토타입 스코프의 빈을 요청하면 매번 새로운 객체 인스턴스를 반환한다. 이런 프로토타입 빈을 싱글톤 빈과 같이 사용하는 경우 의도대로 동작하지 않을 수 있다.
-> 아래는 스프링 컨테이너에 프로토타입 빈을 직접 요청하는 예제다.
1. 클라이언트A는 스프링 컨테이너에 프로토타입 빈을 요청한다.
2. 스프링 컨테이너는 프로토타입 빈(PrototypeBean@x01)을 새로 생성해서 반환한다. 이 프로토타입 빈의 count 필드 값은 0이다.
3. 클라이언트는 조회한 프로토타입 빈에 addCount()를 호출하여 count필드를 +1한다.
- 프로토타입 빈(PrototypeBean@x01)의 count는 1이 된다.
1. 클라이언트B는 스프링 컨테이너에 프로토타입 빈을 요청한다.
2. 스프링 컨테이너는 프로토타입 빈을 새로 생성하여 반환한다. 이 프로토타입 빈의 count필드 값은 1이다.
3. 클라이언트는 조회한 프로토타입 빈에 addCount()를 호출하여 count += 1 한다.
- 프로토타입 빈(PrototypeBean@x02)의 count는 1이 된다.
-> 다음은 위 그림을 코드로 작성한 것이다.
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Scope;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class SingletonWithPrototypeTest1 {
@Test
void prototypeFind() {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(PrototypeBean.class);
PrototypeBean prototypeBean1 = ac.getBean(PrototypeBean.class);
prototypeBean1.addCount();
Assertions.assertThat(prototypeBean1.getCount()).isEqualTo(1);
PrototypeBean prototypeBean2 = ac.getBean(PrototypeBean.class);
prototypeBean2.addCount();
Assertions.assertThat(prototypeBean2.getCount()).isEqualTo(1);
}
@Scope("prototype") // 프로토타입 빈 스코프
static class PrototypeBean{
private int count = 0;
public void addCount() {
count += 1;
}
public int getCount(){
return count;
}
@PostConstruct
public void init(){
System.out.println("PrototypeBean.init " + this);
}
@PreDestroy
public void destroy(){
System.out.println("PrototypeBean.destroy");
}
}
}
-> 결과
◎ 싱글톤 빈에서 프로토타입 빈 사용
-> 다음은 싱글톤 빈이 의존관계 주입을 통해 프로토타입 빈을 주입받아 사용하는 예제다.
- clientBean은 싱글톤이므로 스프링 컨테이너 생성 시점에 함께 생성되고, 의존관계 주입도 한다.
1. clientBean은 의존관계 자동주입을 한다. 주입 시점에 프로토타입 빈을 요청한다.
2. 스프링 컨테이너는 프로토타입 빈을 생성해서 clientBean에 반환한다. 프로토타입 빈의 count 필드 값은 0이다.
- clientBean은 프로토타입 빈을 내부 필드에 관리한다.(참조값을 보관하는 것이다.)
- 클라이언트A는 clientBean을 스프링 컨테이너에 요청해서 받는다. 싱글톤이기 때문에 항상 같은 clientBean이 반환된다.
3. 클라이언트A는 clientBean.logic()을 호출한다.
4. clientBean은 프로토타입 빈의 addCount()를 호출하여 프로토타입 빈의 count를 증가시킨다. count는 1이 된다.
- 클라이언트B는 clientBean을 스프링 컨테이너에 요청해서 받는다. 역시 싱글톤이므로 항상 같은 clientBean이 반환된다.
- clientBean이 내부에 가지고 있는 프로토타입 빈은 이미 스프링 컨테이너 생성이 되면서 주입이 끝난 상태다. 주입 시점에 스프링 컨테이너에 요청하여 프로토타입 빈이 새로 생성된 것이며 사용할 때마다 새로 빈이 생성되는 것이 아니다.
5. 클라이언트B는 clientBean.logic()을 호출한다.
6. clientBean은 프로토타입 빈의 addCount()를 호출하여 프로토타입 빈의 count를 증가한다. 1이었던 count값이 2가 된다.
-> 다음은 위 그림을 코드로 작성한 것이다.
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Scope;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class SingletonWithPrototypeTest1 {
@Test
void singletonClientUsePrototype(){ // 싱그론 빈에서 프로토타입 빈 사용 테스트
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ClientBean.class, PrototypeBean.class);
ClientBean clientBean1 = ac.getBean(ClientBean.class);
int count1 = clientBean1.logic();
Assertions.assertThat(count1).isEqualTo(1);
ClientBean clientBean2 = ac.getBean(ClientBean.class);
int count2 = clientBean2.logic();
Assertions.assertThat(count2).isEqualTo(2);
}
@Scope("singleton") // 싱글톤 빈 스코프
static class ClientBean {
private final PrototypeBean prototypeBean;
@Autowired
public ClientBean(PrototypeBean prototypeBean) {// PrototypeBean 의존관계 주입
this.prototypeBean = prototypeBean;
}
public int logic(){
prototypeBean.addCount();
int count = prototypeBean.count;
return count;
}
}
@Scope("prototype") // 프로토타입 빈 스코프
static class PrototypeBean{
private int count = 0;
public void addCount() {
count += 1;
}
public int getCount(){
return count;
}
@PostConstruct
public void init(){
System.out.println("PrototypeBean.init " + this);
}
@PreDestroy
public void destroy(){
System.out.println("PrototypeBean.destroy");
}
}
}
-> 결과
-> 스프링은 일반적으로 싱글톤 빈을 사용하기 때문에 싱글톤 빈이 프로토타입 빈을 사용하게 된다.
-> 싱글톤 빈은 생성 시점에만 의존관계 주입을 받기 떄문에 프로토타입 빈이 새로 생성되지만, 싱글톤 빈과 함께 유지가 된다. 위의 결과를 보면 init메서드가 한 번만 실행된 것을 볼 수 있다.
-> 이러면 프로토타입 빈을 사용하는 의미가 없어진다.
※ 만약 2개의 클라이언트 객체가 각각 의존관계를 주입 받으면 각각 다른 인스턴스의 프로토타입 빈을 주입받기는 한다.
하지만 이것 역시 사용할 때마다 새로 생성되는 것이 아니기 때문에 한 번 생성된 이후에는 빈이 유지가 된다.
'Spring > [inflearn]스프링 핵심 원리 - 기본편' 카테고리의 다른 글
빈 스코프 - 웹 스코프 (0) | 2023.09.18 |
---|---|
빈 스코프 - 프로토타입 스코프와 싱글톤 빈과 함께 사용 시 Provider로 문제 해결 (0) | 2023.09.18 |
빈 스코프 - 프로토타입 스코프 (0) | 2023.08.26 |
빈 스코프 (0) | 2023.08.26 |
빈 생명주기 콜백 - 애너테이션(@PostConstruct, @PreDestory) (0) | 2023.08.24 |