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 !!! 有人可以向我解釋:如果我期望的行爲是正確的/或配置有什麼問題。