gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:48:10 25 4
gpt4 key购买 nike

是否可以将请求范围的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com