【问题标题】:Error creating bean with name : Scope 'request' is not active for the current thread使用名称创建 bean 时出错:范围“请求”对于当前线程不活动
【发布时间】:2011-09-21 20:18:21
【问题描述】:

我已经集成了 SWF 2.2.1、Primefaces 2.2.1、JSF 2、Spring Security 3、Spring 3.1.0M1I 和 EhCache 以及 AspectJ 和 Castor。

我在我的 app-config.xml 中为 castor 定义了 bean,比如

<bean id="oXMapper" class="com.abc.xyz.util.OXMapper">
    <property name="unmarshaller" ref="unmarshaller" />
    <property name="marshaller" ref="marshaller" />
    <property name="acordRequest" ref="acordRequest" />
    <property name="acordResponse" ref="acordResponse" />
</bean>

<bean id="unmarshaller" class="org.springframework.oxm.castor.CastorMarshaller">
    <property name="mappingLocation"
        value="classpath:/templates/mapping/ACORD_Response_Mapping.xml" />
</bean>

<bean id="marshaller" class="org.springframework.oxm.castor.CastorMarshaller">
    <property name="mappingLocation"
        value="classpath:/templates/mapping/ACORD_Request_Mapping.xml" />
</bean>

<bean id="acordRequest" class="com.abc.xyz.cate.domain.ACORD">
    <property name="insuranceSvcRq" ref="insuranceSvcRq" />
    <property name="signonRq" ref="CltSearch_signonRq" />
</bean>

我有一个搜索页面,从中构建参数,例如

<h:panelGrid>
    <h:selectOneRadio id="#{msg.srchType}" value="#{acordRequest.insuranceSvcRq.com_csc_ClientSearchRq.com_csc_SearchInfo.com_csc_SearchCriteria.com_csc_ClientSearch.com_csc_SearchNameByType}">
        <f:selectItem itemLabel="#{msg.exact}" itemValue="E" id="#{msg.exact}" />
    <f:selectItem itemLabel="#{msg.phonetic}" itemValue="S" id="#{msg.phonetic}" />
    <f:selectItem itemLabel="#{msg.truncated}" itemValue="P" id="#{msg.truncated}" />
        </h:selectOneRadio>
</h:panelGrid>

使用 Spring Webflow,我调用 OXMapper 函数并将 ACORD(用于构建 xml 的分层结构)对象传递给它。这个OXMapper类分别负责对象和xml的编组和解组。

现在的问题是,每当我在搜索页面上再次搜索时,旧值会自动填充到字段中。

所以我尝试将范围更改为“请求”。

<bean id="acordRequest" class="com.abc.xyz.cate.domain.ACORD"
    scope="request">
    <property name="insuranceSvcRq" ref="insuranceSvcRq" />
    <property name="signonRq" ref="CltSearch_signonRq" />
</bean>

更改范围后出现以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'oXMapper' defined in ServletContext resource [/WEB-INF/config/app-config.xml]: Cannot resolve reference to bean 'acordRequest' while setting bean property 'acordRequest'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'acordRequest': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

我的web.xml如下

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/config/web-application-config.xml
    </param-value>
</context-param>
<error-page>
    <error-code>500</error-code>
    <location>/error.xhtml</location>
</error-page>
<context-param>
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
    <param-value>.xhtml</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<context-param>
    <param-name>facelets.DEVELOPMENT</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
    <param-value>1</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/WEB-INF/springsecurity.taglib.xml</param-value>
</context-param>
<!-- Enables Spring Security -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
    <listener-class>
        org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<servlet>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <url-pattern>/certs/*</url-pattern>
</servlet-mapping>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

我的 faces-config.xml 没有这样的东西

<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">

<application>
    <!-- <message-bundle>JsfMessageResources</message-bundle> -->
</application>
<!-- JSF 2.0 Version of this faces-config.xml file -->

【问题讨论】:

    标签: spring aspectj spring-webflow castor


    【解决方案1】:

    如果您没有使用 Spring MVC,为了使用 request 范围,您必须在 web.xml 中引用 RequestContextListener:

    如果您使用 Servlet 2.4+ web 容器,请求在 Spring 的 DispatcherServlet 之外处理(例如,当使用 JSF 或 Struts 时),您需要将以下 javax.servlet.ServletRequestListener 添加到您的 web 应用程序 web 的声明中.xml 文件:

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

    引用docs。

    【讨论】:

    • 感谢 abalogh 的回复。我忘了提到我什至尝试在 web.xml 中包含 RequestContextListener,正如你提到的那样。我仍然遇到同样的错误。 :-(
    • 您能发布您的 web.xml 和 faces-config.xml 吗?另外请尝试升级到 Spring 3.1M2,它不太可能解决您的问题,但是当有更新的里程碑时,为什么要使用过时的里程碑?
    • 我只使用 Spring 3.1M2,更新后的版本。我已经发布了 web.xml 广告 faces-config.xml。谢谢
    • 您的搜索页面的网址是什么?
    • 您想查看应用程序在某个网络下的页面吗?您将无法访问它。我需要将它部署在我的本地机器上。无论如何,我会这样做。
    猜你喜欢
    • 2018-10-28
    • 2019-06-06
    • 2018-11-05
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 2015-01-29
    相关资源
    最近更新 更多