Spring中的请求作用域bean意味着容器为每个HTTP请求创建一个bean实例。请求作用域bean的实例化
让说我有一个RequestScopedBean豆:
@Component
public class RequestScopedBean {
@PostConstruct
void init() {
System.out.println("Init method called for each incoming HTTP Request");
}
}
public void doSomething() {}
配置:
@Configuration
public class MyBeansConfig {
@Bean
@Scope(value="request", proxyMode=TARGET_CLASS)
public RequestScopedBean requestScopedBean() {
return new requestScopedBean();
}
}
我用我的RequestScopedBean一个辛格尔顿豆里面 - 和我期待的是,为每个传入的HTTP请求调用init()方法。但事实并非如此。 init()方法只被调用一次,这意味着容器只创建一个我的实例RequestScopedBean !!! 有人可以向我解释:如果我期望的行为是正确的/或配置有什么问题。