Bean Scope는 스프링 컨테이너에서 빈 객체의 생명 주기와 접근 범위를 결정하는 설정입니다. 스프링에서는 다양한 범위(Scope)를 제공하여 빈을 관리할 수 있으며, 각 스코프는 빈이 생성되고 활용되는 방식을 다르게 정의합니다. 이를 통해 필요한 용도에 따라 빈의 범위를 설정하고, 자원을 효율적으로 사용할 수 있습니다.
스프링에서 제공하는 주요 스코프는 다음과 같습니다:
@Scope("singleton")
을 통해 명시적으로 지정할 수도 있습니다.<bean id="singletonBean" class="com.example.MyBean" scope="singleton"/>
!https://docs.spring.io/spring-framework/reference/_images/singleton.png
@Scope("prototype")
또는 XML 설정에서 scope="prototype"
으로 설정합니다.<bean id="prototypeBean" class="com.example.MyBean" scope="prototype"/>
!https://docs.spring.io/spring-framework/reference/_images/prototype.png
위 두가지 Scope이 가장 중요해요.
아래 Scope은 참고용으로 필요할 때만 사용하세요.
@Scope("request")
또는 XML 설정에서 scope="request"
로 설정합니다.<bean id="requestBean" class="com.example.MyBean" scope="request"/>
@Scope("session")
또는 XML 설정에서 scope="session"
으로 설정합니다.<bean id="sessionBean" class="com.example.MyBean" scope="session"/>
@Scope("application")
또는 XML 설정에서 scope="application"
으로 설정합니다.<bean id="applicationBean" class="com.example.MyBean" scope="application"/>
@Scope("websocket")
으로 설정합니다.@Scope
어노테이션을 사용하여 빈 스코프를 지정할 수 있습니다.@Component
@Scope("prototype")
public class MyBean { ... }
<bean>
태그에 scope
속성을 사용하여 스코프를 지정합니다.<bean id="myBean" class="com.example.MyBean" scope="prototype"/>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ScopeExample {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
// Singleton 스코프: 동일한 인스턴스를 반환
MyBean singletonBean1 = context.getBean("singletonBean", MyBean.class);
MyBean singletonBean2 = context.getBean("singletonBean", MyBean.class);
System.out.println(singletonBean1 == singletonBean2); // true
// Prototype 스코프: 서로 다른 인스턴스를 반환
MyBean prototypeBean1 = context.getBean("prototypeBean", MyBean.class);
MyBean prototypeBean2 = context.getBean("prototypeBean", MyBean.class);
System.out.println(prototypeBean1 == prototypeBean2); // false
}
}
위 예제에서 singletonBean
은 동일한 인스턴스를 반환하며, prototypeBean
은 서로 다른 인스턴스를 반환합니다.
빈 스코프를 활용하여 필요한 경우에 따라 효율적인 자원 관리가 가능합니다. 예를 들어, 애플리케이션 전역에서 사용하는 로깅이나 설정 관리와 같은 빈은 싱글톤으로 설정하고, 사용자의 요청이나 세션에 따라 생성되는 빈은 프로토타입 또는 세션 스코프로 설정하는 것이 좋습니다.
package org.example.di02;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@SpringBootApplication
public class Di05Application {
public static void main(String[] args) {
SpringApplication.run(Di05Application.class, args);
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeanConfig.class);
// 싱글톤 Scope : 같은 참조, 항상 같은 인스턴스
HelloBean1 helloBean11 = context.getBean("helloBean1", HelloBean1.class);
HelloBean1 helloBean12 = context.getBean("helloBean1", HelloBean1.class);
System.out.println(helloBean11); // org.example.di02.HelloBean1@3f14c570
System.out.println(helloBean12); // org.example.di02.HelloBean1@3f14c570
// 프로토타입 Scope: 다른 참조, 다른 인스턴스
HelloBean2 helloBean21 = context.getBean("helloBean2", HelloBean2.class);
HelloBean2 helloBean22 = context.getBean("helloBean2", HelloBean2.class);
System.out.println(helloBean21); // org.example.di02.HelloBean2@8a58114
System.out.println(helloBean22); // org.example.di02.HelloBean2@2f597247
context.close();
}
}