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

标签 java spring servlets

我不明白为什么配置不起作用。

我在没有 mvc 的情况下制作 Spring Web 应用程序并使用 HttpRequestHandlerServlet 类。我需要所有 bean 在一个请求中使用一个连接。我在 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>

最佳答案

错误消息非常有帮助。由于您没有在 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>

关于java - HttpRequestHandlerServlet 中的 Spring 请求范围不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29316853/

相关文章:

java - Spring中的@Transactional注解

java - 无法读取文档 : Expected type float, 整数或字符串

java - 在 Spring-Security 中,j_spring_security_logout 到底是什么?我听说它被称为 "handler"但我不确定那是什么意思

javascript - 无法在运行时使用 json 数据填充 Highchart

java - Xss 过滤器正则表达式捕获错误的单词

java - 用Java PreparedStatemen执行一批SQL命令后参数值会自动清零吗?

java - 来自 C,我需要一些帮助来弄清楚如何最好地从输入字符串生成国际象棋位图

ajax - 如何使用 jsf 2.0 配置 spring webflow?

java - 为 Log4j 1.2 注册自定义 LoggerFactory 的正确方法?

java - 需要帮助对整数进行一些特定的格式化(java)