是否可以将请求范围的bean自动装配到应用程序范围的bean中.即
我有一个RequestScopedBean类:
class RequestScopedBean {
....
....
....
}
Run Code Online (Sandbox Code Playgroud)
以及一个应用程序作用域bean,其中请求作用域bean是自动装配的.
class ApplicationScopedBean {
@Autowire
private RequestScopedBean requestScopedBean;
....
....
....
}
Run Code Online (Sandbox Code Playgroud)
和spring-config 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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
">
<bean id="applicationScopedBeans" class="ApplicationScopedBean" />
<bean id="requestScopedBean" class="RequestScopedBean" scope="request">
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
当我尝试运行此应用程序时,applicationScopedBean的bean创建失败,并出现以下错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ApplicationScopedBean': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private RequestScopedBean requestScopedBean; nested exception is java.lang.IllegalStateException: No Scope registered for scope 'request'
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1075)
at com.amazon.coral.reflect.instantiate.SpringInstantiatorFactory$1.newInstance(SpringInstantiatorFactory.java:168)
... 19 more
Run Code Online (Sandbox Code Playgroud)
Bij*_*men 15
您还必须将自己标记requestScopedBean为作用域代理,这样Spring将在requestScopedBean的代理中注入,并在后台适当地管理范围.
<bean id="requestScopedBean" class="RequestScopedBean" scope="request">
<aop:scoped-proxy/>
</bean>
Run Code Online (Sandbox Code Playgroud)
更多这里
上面的例外表明您没有正确配置Spring以提供请求范围的bean.
您需要将它添加到如在文档中描述你的web.xml 在这里:
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
但是,除了配置之外,您的问题还有很多.您正在尝试将请求范围的bean注入到单例范围的bean中.当DI容器启动时,Spring解析依赖关系并实例化单例.这意味着ApplicationScopedBean只会被创建一次(此时将不会有飞行中的请求,因此自动装配很可能会失败).
如果您使用的是原型范围的bean而不是请求范围,那么每次使用它时都必须考虑使用新实例来提供单例范围的bean.Spring文档的Method Injection一章中描述了这种方法.
| 归档时间: |
|
| 查看次数: |
16391 次 |
| 最近记录: |