user1182583
user1182583

Reputation: 191

Spring request scope in HttpRequestHandlerServlet does not work

I can't understand why the configuration does not work.

I make spring web applcation without mvc and use HttpRequestHandlerServlet class. I need that all beans use one Connection in one request. And I set request scope in Connection bean but when I run it:

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.

My web.app is:

<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>

My app-context is:

<?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>

Upvotes: 0

Views: 1632

Answers (2)

user1182583
user1182583

Reputation: 191

I thing I understand. HttpRequestHandlerServlet is singleton. RequestHandler as field of HttpRequestHandlerServlet must be proxy to refresh all "request" scope beans inside

and RequestContextListener must be in web.xml:

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

it is 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>

Upvotes: 1

Robert Moskal
Robert Moskal

Reputation: 22553

The error message is pretty helpful. Since you aren't using the Dispatcher Servlet in your web.xml (as for a standard spring mvc app):

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

You need to find another way to give spring access to the request. Adding the RequestContextListener to your web.xml should do the trick:

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

Upvotes: 1

Related Questions