我只是想知道是否有其他方式來自動裝載請求作用域bean。所以現在我在配置文件中創建一個bean
:自動裝配請求作用域bean的方法
@Bean
@Scope(value=WebApplicationContext.SCOPE_REQUEST, proxyMode=ScopedProxyMode.DEFAULT)
public List<String> stringBean(){
return new ArrayList<String>();
}
所以通常我會自動裝配的applicationContext
使用bean
:
@Autowired
private ApplicationContext context;
@Override
public void anyName() {
List<String> list = (List<String>) getContext().getBean("stringBean");
}
這工作完全正常。但我不喜歡自動調整背景和演員的需要。於是,我就直接自動裝配豆:
@Autowired
private List<String> stringBean;
我得到了應用程序是什麼也很清楚,因爲一個請求開始之前沒有創建bean的啓動異常。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stringBean': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
是否有其他方式自動裝入請求作用域bean?
'ScopedProxyMode.DEFAULT',如果您還沒有配置任何,意味着NO (沒有創建代理)。嘗試使用'ScopedProxyMode.TARGET_CLASS'來使用'CGLIB'代理。 – alfcope
@alfcope看起來像這樣工作。你能解釋一下不同嗎?也可以在答案中。 – Patrick
當然,你有。這完全是關於代理。 – alfcope