OOP >
컨테이너에 등록하는 객체의 타입별 생명주기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Scope
import org.springframework.context.annotation.ScopedProxyMode
@Configuration
class AppConfig {
// 의존성 주입되거나 컨테이너에서 조회될 때마다 새로 생성되는 빈.
// 이 빈의 생명 주기는 해당 빈을 사용하는 객체에 의해 결정되며, 더 이상 필요하지 않을 때 GC에 의해 제거된다.
@Bean
@Scope("prototype")
fun prototypeService(): IPrototypeService {
return PrototypeService()
}
// HTTP 요청마다 새로 생성되는 빈.
// HTTP 요청이 완료되면 해당 빈도 더 이상 필요가 없어 GC에 의해 제거된다.
@Bean
@Scope("request", proxyMode = ScopedProxyMode.TARGET_CLASS) // HTTP 요청 당 하나의 객체 생성
fun requestService(): IRequestService {
return RequestService()
}
// @Scope 어노테이션을 명시하지 않으면 기본적으로 해당 빈은 싱글톤(singleton)으로 관리된다.
// 싱글톤 빈은 스프링 컨테이너가 시작될 때 생성되고, 애플리케이션이 종료될 때까지 유지된다.
@Bean
fun singletonService(): ISingletonService {
return SingletonService()
}
}
OOP 카테고리의 다른 글