小编Lyd*_*win的帖子

将请求范围的bean自动装配到应用程序范围的bean中

是否可以将请求范围的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 …
Run Code Online (Sandbox Code Playgroud)

spring scopes

10
推荐指数
2
解决办法
2万
查看次数

标签 统计

scopes ×1

spring ×1