HttpRequestHandlerServlet中的Spring请求范围不起作用

时间:2015-03-28 11:54:19

标签: java spring servlets

我无法理解为什么配置不起作用。

我在没有mvc的情况下制作spring web applcation并使用HttpRequestHandlerServlet类。我需要所有bean在一个请求中使用一个Connection。我在Connection bean中设置了请求范围,但是当我运行它时:

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.app是:

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

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

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/app-context.xml</param-value>
</context-param>

<servlet>
    <servlet-name>monitoring</servlet-name>
    <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>monitoring</servlet-name>
    <url-pattern>/monitoring</url-pattern>
</servlet-mapping>

我的应用环境是:

<?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:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop.xsd">

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUrl" value="jdbc:mysql://localhost/nts" />
    <property name="user" value="root" />
    <property name="password" value="root" />

    <property name="maxPoolSize" value="20" />
    <property name="minPoolSize" value="5" />
    <property name="maxStatements" value="100" />
    <property name="testConnectionOnCheckout" value="true" />
</bean>

<bean id="conn" class="java.sql.Connection" factory-bean="dataSource" factory-method="getConnection" scope="request"/>

<bean id="monitoring" class="com.sotas.terminal.server.servlet.MonitoringServlet">
    <property name="monitoringService">
        <bean class="com.sotas.terminal.server.service.MonitoringService">
            <property name="conn" ref="conn"/>
            <property name="providerDAO">
                <bean class="com.sotas.terminal.server.dao.ProviderDAOImpl">
                    <property name="conn" ref="conn"/>
                </bean>
            </property>
        </bean>
    </property>
</bean>

2 个答案:

答案 0 :(得分:1)

错误消息非常有用。由于您没有在web.xml中使用Dispatcher Servlet(与标准的spring mvc应用程序一样):

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

您需要找到另一种方式来为Spring提供对请求的访问权限。将RequestContextListener添加到web.xml应该可以解决问题:

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

答案 1 :(得分:1)

我明白了。 HttpRequestHandlerServlet是单例。 RequestHandler作为HttpRequestHandlerServlet的字段必须是代理才能刷新所有&#34; request&#34;

中的范围bean

和RequestContextListener必须位于web.xml中:

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

这是工作app-context.xml:

    <bean id="monitoring" class="com.sotas.terminal.server.servlet.MonitoringServlet" scope="request">
    <property name="conn" ref="conn"/>
    <aop:scoped-proxy/>
    <property name="monitoringService">
        <bean class="com.sotas.terminal.server.service.MonitoringService" scope="request">
            <property name="conn" ref="conn"/>
            <property name="providerDAO">
                <bean class="com.sotas.terminal.server.dao.ProviderDAOImpl" scope="request">
                    <property name="conn" ref="conn"/>
                </bean>
            </property>
        </bean>
    </property>
</bean>