spring - 将请求范围的 bean Autowiring 到应用程序范围的 bean 中

标签 spring scopes

是否可以将请求范围的 bean Autowiring 到应用程序范围的 bean 中。即

我有一个类 RequestScopedBean:

class RequestScopedBean {
   ....
   ....
   ....
}

还有一个类应用程序范围的 bean,其中请求范围的 bean 是 Autowiring 的。

class ApplicationScopedBean {
   @Autowire
   private RequestScopedBean requestScopedBean;
   ....
   ....
   ....
}

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>

当我尝试运行此应用程序时,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

最佳答案

您还必须将您的 requestScopedBean 标记为范围代理,这样 Spring 将注入(inject) requestScopedBean 的代理并在后台适本地管理范围。

<bean id="requestScopedBean" class="RequestScopedBean" scope="request">  
    <aop:scoped-proxy/>
</bean>

更多 here

关于spring - 将请求范围的 bean Autowiring 到应用程序范围的 bean 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13556037/

相关文章:

java - 如何删除表中的值? (MVN、Spring、Java)

forms - 在 Spring MVC 中将值设置为 NULL 而不是空字符串

java - 如何避免将空值发送到java中的sql中

spring - 如何在 Thymeleaf 上设置多个条件

google-api - 无法使用 - auth/user.phonenumbers.read 范围从用户的谷歌个人资料中读取电话号码

c# - 变量的有效范围

java - 事务注释不同的行为

ruby-on-rails-3 - 范围是在 Rails 类还是实例方法中?

javascript - 保护javascript中的全局引用