Spring - 无法在请求范围

时间:2015-05-27 15:17:42

标签: spring spring-ioc

我正在尝试实例化一个组件对象,我在applicationContext.xml中为其声明了<bean>条目。实例化我的目标组件类的流程如下

CalculateController - &gt; CalculateService - &gt; CalculateComponent

其中

CalculatorController - scope = request,使用@Controller注释并包含在webapplicationContext.xml中的组件扫描中

CalculatorService - Scope = singleton,使用@service注释并包含在applicationContext.xml中的组件扫描中

CalculateComponent - scope = request,no annotation,从webapplicationConext.xml和applicationContext.xml中的组件扫描中排除。在webApplicationContext.xml中使用scope = request定义bean条目。还包括 <aop:scoped-proxy/>定义中的<bean>

我在web.xml中包含了以下条目

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
            classpath:spring/applicationContext.xml
            /WEB-INF/mvc-dispatcher-servlet.xml
            ....Other resource xmls
    </param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- To retrieve session related information -->
<listener>
    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>

<listener>
    <listener-class>
            org.springframework.web.context.request.RequestContextListener 
    </listener-class>
</listener> 


<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

请注意,CalculateComponent有一个3参数引用对象构造函数,并且它们中的所有三个都在webApplicationContext.xml中具有<bean>条目且具有单例范围,并且它们未被注释。

当请求发送以创建CalculateComponent对象时,spring容器会抛出以下错误。

  

&#34;未找到线程绑定请求:您是指请求   实际Web请求之外的属性,或处理请求   在最初接收线程之外?如果你真的   在网络请求中运行并仍然收到此消息,您的   代码可能在外面运行   DispatcherServlet / DispatcherPortlet:在这种情况下,请使用   RequestContextListener或RequestContextFilter公开当前   。请求&#34;

请告知。

更新

当我从contextConfigLocation中删除/WEB-INF/mvc-dispatcher-servlet.xml并启动服务器时,我收到了Autowired失败错误 - &#34;找不到依赖的CalculateComponent类型的限定bean:&#34;即使在我将范围从请求更改为单身之后。

然后我在CalculateService中注释掉了CalculateComponent的自动装配,现在我可以看到CalculateComponent启动了两次(如我所提到的@Serge Ballesta)。因此我得出结论,在加载DispatcherServlet之前,通过ContextLoaderListener(applicationContext.xml中的bean条目)启动CalculateService(即加载mvc-dispatcher-servlet中没有提到bean)。

我在contextConfigLocation中再次添加了/WEB-INF/mvc-dispatcher-servlet.xml,但这次是作为第一个条目(即在applicationContext.xml之上)。现在,CalculateComponent再次加载两次,自动装配完成了单例范围。通过此设置,我将CalculateComponent范围更改回请求,但再次&#34;我找到了无线程绑定请求&#34;错误。

所以问题是,

ContextLoaderListener尝试在加载/可用之前初始化DispatcherServlet(CalculateComponent)的资源。

2 个答案:

答案 0 :(得分:0)

拥有一个请求作用域控制器... AFAIK很奇怪,框架期望它是一个单例。尝试将其放在单一范围内。

我认为你有/WEB-INF/mvc-dispatcher-servlet.xml的所有bean都实例化了两次:

  • 首先在根应用程序上下文中按原样在全局contextConfigLocation
  • 中声明
  • 接下来是名为mvc-dispatcher
  • 的servlet的servlet应用程序上下文

您应该重命名该文件,因此它不是加载器作为servlet应用程序上下文,或者从根上下文中删除它

答案 1 :(得分:0)

回答我自己的问题。正如我在我的问题中提到的,这是我获取请求作用域组件bean的流程。

CalculateController - &gt; CalculateService - &gt; CalculateComponent。

但是通过异步请求调用了CalculateController。 我们无法从异步请求线程访问Web范围的bean。

参考:How to enable request scope in async task executor