0

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>

Jud
  • 1,324
  • 3
  • 24
  • 47
user1182583
  • 191
  • 1
  • 10

2 Answers2

1

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>
Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
  • what order are the context listeners in, RequestContextListener needs to go second I think. – Robert Moskal Mar 28 '15 at 15:01
  • org.springframework.web.context.ContextLoaderListener org.springframework.web.context.request.RequestContextListener – user1182583 Mar 28 '15 at 15:26
  • ContextLoaderListener is first on startup. RequestContextListener is the request listener, start on request – user1182583 Mar 28 '15 at 15:28
  • Actually, since you are using HttpRequestHandlerServlet, I don't think you need it. The bean is injected correctly when you don't use request scoping though, right? – Robert Moskal Mar 28 '15 at 15:37
  • I change all beans scope to "request". error is gone but now beans not change with new request. I mean old object in every request iteration. – user1182583 Mar 28 '15 at 15:49
  • Have you looked at this? http://stackoverflow.com/questions/13225008/request-scoped-beans-in-spring-httprequesthandler – Robert Moskal Mar 28 '15 at 16:02
  • it is for injecting prototype|request|session scope into singleton. but i want to inject request scope bean into request scope bean – user1182583 Mar 28 '15 at 16:16
1

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>
user1182583
  • 191
  • 1
  • 10