Spring에서 Bean의 스코프란 Bean의 생성 및 관리되는 범위를 의미합니다.
기본적으로 Spring의 Bean은 Singleton으로 관리되지만 다른 스코프를 설정할 수도 있습니다.
| 스코프 | 설명 |
|---|---|
| Singleton | (기본값) 하나의 객체만 생성하여 공유 |
| Prototype | 요청할 때마다 새로운 객체 생성 |
| Request | HTTP 요청마다 새로운 객체 생성 (웹) |
| Session | HTTP 세션마다 새로운 객체 생성 (웹) |
| Application | 애플리케이션 내에서 하나의 객체만 생성 |
이번 글에서는 Prototype 스코프에 대해 집중적으로 살펴보겠습니다.
Prototype 스코프는 요청할 때마다 새로운 객체를 생성하는 Bean 스코프입니다.
@Component
@Scope("prototype") // 프로토타입 스코프 설정
public class PrototypeBean {
public PrototypeBean() {
System.out.println("Prototype Bean 생성됨!");
}
}
이렇게 설정하면 PrototypeBean을 요청할 때마다 새로운 객체가 생성됩니다.
@Component
@Scope("singleton") // (기본값)
public class SingletonBean {
}
@Component
@Scope("prototype")
public class PrototypeBean {
}
public class PrototypeScopeTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
PrototypeBean bean1 = context.getBean(PrototypeBean.class);
PrototypeBean bean2 = context.getBean(PrototypeBean.class);
System.out.println(bean1 == bean2); // false (다른 객체)
}
}
-> bean1 != bean2
즉, 요청할 때마다 새로운 객체가 생성됨을 확인할 수 있습니다.
Prototype Bean은 Spring이 관리하지 않기 때문에 자동으로 소멸되지 않습니다.
이 때문에 싱글톤 Bean에서 프로토타입 Bean을 사용할 때 주의가 필요합니다.
@Component
@Scope("singleton")
public class SingletonBean {
@Autowired
private PrototypeBean prototypeBean; // 주입 시점에 객체가 고정됨
public void print() {
System.out.println(prototypeBean);
}
}
위 코드를 실행하면 항상 같은 Prototype 객체가 반환됩니다.
왜냐하면 Spring이 SingletonBean을 생성할 때 prototypeBean을 한 번만 주입하기 때문입니다.
ObjectProvider 사용@Component
@Scope("singleton")
public class SingletonBean {
@Autowired
private ObjectProvider<PrototypeBean> prototypeBeanProvider;
public void print() {
PrototypeBean prototypeBean = prototypeBeanProvider.getObject();
System.out.println(prototypeBean);
}
}
-> getObject()를 호출할 때마다 새로운 객체가 생성됨
@Lookup 애노테이션 사용@Component
@Scope("singleton")
public class SingletonBean {
@Lookup
public PrototypeBean getPrototypeBean() {
return null; // Spring이 자동으로 오버라이드 함
}
public void print() {
System.out.println(getPrototypeBean());
}
}
-> @Lookup을 사용하면 자동으로 새로운 Prototype Bean을 주입받을 수 있음
장점
단점
Spring의 기본 스코프가 Singleton인 이유는 성능과 효율성 때문이지만 Prototype 스코프는 상태를 가지는 객체를 독립적으로 관리할 때 매우 유용하다는 것을 배웠습니다.
싱글톤 Bean과 함께 사용할 때 관리해야 하는 점이 많다는 것이 중요하다고 느꼈습니다.