JUST WRITE

Spring Bean Scopes 본문

Programing/Spring

Spring Bean Scopes

천재보단범재 2021. 9. 28. 00:17

Bean Scopes

Spring Bean Scopes

Bean Scope는 Bean이 존재할 수 있는 범위, 생명주기를 가리킨다.

Spring Framework에서는 6가지 Scope로 정리한다.

  • singleton
  • prototype
  • request
  • session
  • application
  • websocket

request, session, application, websocket 4가지 Scope는 Web Application에서만 적용할 수 있다.

Singleton Scope

기본 Scope이다.Spring Container는 Bean당 오직 한 객체만 생성한다.

모든 Request에 대해서 한 객체만 호출되어진다.(cache)

// Annotation
@Bean
@Scope("singleton")
public Person personSingleton() {
    return new Person();
}
<!-- XML 설정 -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="personSingleton" class="org.baeldung.scopes.Person" scope="singleton"/>    
</beans>
private static final String NAME = "John Smith";

@Test
public void givenSingletonScope_whenSetName_thenEqualNames() {
    ApplicationContext applicationContext = 
      new ClassPathXmlApplicationContext("scopes.xml");

    Person personSingletonA = (Person) applicationContext.getBean("personSingleton");
    Person personSingletonB = (Person) applicationContext.getBean("personSingleton");

    personSingletonA.setName(NAME);
    Assert.assertEquals(NAME, personSingletonB.getName());

    ((AbstractApplicationContext) applicationContext).close();
}

Prototype Scope

Bean이 호출될 때마다 다른 객체가 반환된다.

@Bean
@Scope("prototype")
public Person personPrototype() {
    return new Person();
}
<bean id="personPrototype" class="org.baeldung.scopes.Person" scope="prototype"/>

Request Scope

HTTP Request 마다 해당 Bean 객체가 생성된다.

@Bean
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public HelloMessageGenerator requestScopedBean() {
    return new HelloMessageGenerator();
}

// shotcut
@Bean
@RequestScope
public HelloMessageGenerator requestScopedBean() {
    return new HelloMessageGenerator();
}

Session Scope

HTTP Session 마다 해당 Bean 객체가 생성된다.

@Bean
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public HelloMessageGenerator sessionScopedBean() {
    return new HelloMessageGenerator();
}

// shotcut
@Bean
@SessionScope
public HelloMessageGenerator sessionScopedBean() {
    return new HelloMessageGenerator();
}

Application Scope

ServletContext lifecycle동안 해당 Bean 객체가 생성된다.

Singleton Scope와 비슷하지만 Multiple Servelet-based Application에서는 차이가 난다.

Application Scope에서는 ServletContext마다 Singleton

Singleton Scope에서는 ApplicationConext마다 Singleton

@Bean
@Scope(
  value = WebApplicationContext.SCOPE_APPLICATION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public HelloMessageGenerator applicationScopedBean() {
    return new HelloMessageGenerator();
}

// shotcut
@Bean
@ApplicationScope
public HelloMessageGenerator applicationScopedBean() {
    return new HelloMessageGenerator();
}

WebSocket Scope

WebSocket Scope Bean은 WebSocket Session Attribute에 저장된다.

@Bean
@Scope(scopeName = "websocket", proxyMode = ScopedProxyMode.TARGET_CLASS)
public HelloMessageGenerator websocketScopedBean() {
    return new HelloMessageGenerator();
}

// shotcut
@Bean
@Scope("websocket")
public HelloMessageGenerator websocketScopedBean() {
    return new HelloMessageGenerator();
}

[참고사이트]

728x90
반응형

'Programing > Spring' 카테고리의 다른 글

MappingJackson2JsonView  (0) 2021.10.09
Singleton Registry  (0) 2021.10.02
Bean LifeCycle  (0) 2021.09.29
Spring Bean  (0) 2021.09.26
IoC / DI  (0) 2021.09.26
Comments