Java HttpRequestHandlerServlet中的Spring请求作用域不工作
我不明白为什么配置不起作用 我在没有mvc的情况下制作SpringWeb应用程序,并使用HttpRequestHandlerServlet类。我需要所有bean在一个请求中使用一个连接。我在连接bean中设置了请求范围,但运行时:Java HttpRequestHandlerServlet中的Spring请求作用域不工作,java,spring,servlets,Java,Spring,Servlets,我不明白为什么配置不起作用 我在没有mvc的情况下制作SpringWeb应用程序,并使用HttpRequestHandlerServlet类。我需要所有bean在一个请求中使用一个连接。我在连接bean中设置了请求范围,但运行时: IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or pro
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>
org.springframework.web.context.ContextLoaderListener
上下文配置位置
/WEB-INF/app-context.xml
监测
org.springframework.web.context.support.HttpRequestHandlerServlet
监测
/监测
我的应用程序上下文是:
<?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应用程序):
springapp
org.springframework.web.servlet.DispatcherServlet
1.
您需要找到另一种方法来授予spring对请求的访问权。将RequestContextListener添加到web.xml应该可以做到以下几点:
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
org.springframework.web.context.request.RequestContextListener
我明白一件事。HttpRequestHandlerServlet是单例的。HttpRequestHandlerServlet的RequestHandler作为字段必须是代理才能刷新其中的所有“请求”范围bean
和RequestContextListener必须位于web.xml中:
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
org.springframework.web.context.request.RequestContextListener
它是work 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>
上下文侦听器的顺序是什么,我认为RequestContextListener需要进行第二步。org.springframework.web.context.ContextLoaderListener org.springframework.web.context.request.RequestContextListener ContextLoaderListener首先启动。RequestContextListener是请求侦听器,从request开始实际上,因为您使用的是HttpRequestHandlerServlet,我认为您不需要它。当您不使用请求作用域时,bean被正确注入,对吗?我将所有bean的作用域更改为“request”。错误已消失,但现在bean不会随新请求而更改。我指的是每次请求迭代中的旧对象。这很有意义。我很高兴你解决了你的问题!
<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>