1

I have a Spring bean with request scope :

@Configuration
@ComponentScan("com.######.my")
@PropertySource(value = "classpath:my.properties")
public class MyConfiguration {

    @Bean
    @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
    public MyBean myBean() {
        return new MyBean();
    }

    @Bean
    @Conditional(MyActivation.class)
    public MyAspect myAspect() {
        return new MyAspect();
    }

    @Bean
    @Conditional(MyActivation.class)
    public MyServices myServices() {
        return new MyServicesImpl();
    }

}

This bean is used to get common values when aspecting various methods, and some of them are in "@Async" annotated method. For these methods in asynchronous part I have the error :

Error creating bean with name 'scopedTarget.myBean': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.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.

Is there any chance to deal with request scope in async methods ? Thanks for your help.

I use Spring 4.3.10.RELEASE version with spring-aop.

When I tried one of these methods explained here (How to enable request scope in async task executor) or here (https://stackoverflow.com/a/21400914) I have :

Error creating bean with name 'scopedTarget.cctBean': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: Cannot ask for request attribute - request is not active anymore!

My web.xml :

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app 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"
    version="3.1">

    <display-name>My Application</display-name>

    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.###.AppConfiguration</param-value>
    </context-param>

    <filter>
        <filter-name>LogBackMDCFilter</filter-name>
        <filter-class>com.###.LogBackMDCFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>LogBackMDCFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>services</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>com.orange.sbo.openvod.ServiceDispatcherConfiguration</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>services</servlet-name>
        <url-pattern>/services/json/*</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>com.###.AdminDispatcherConfiguration</param-value>
        </init-param>
    </servlet>

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

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

    <!-- Use for the request scope -->
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

<!--     <filter> -->
<!--         <filter-name>requestContextFilter</filter-name> -->
<!--         <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class> -->
<!--     </filter> -->

<!--     <filter-mapping> -->
<!--         <filter-name>requestContextFilter</filter-name> -->
<!--         <servlet-name>dispatcher</servlet-name> -->
<!--         <dispatcher>REQUEST</dispatcher> -->
<!--         <dispatcher>ERROR</dispatcher> -->
<!--     </filter-mapping> -->

</web-app>
Nono
  • 43
  • 2
  • 8
  • Possible duplicate of [Scope 'session' is not active for the current thread; IllegalStateException: No thread-bound request found](https://stackoverflow.com/questions/21286675/scope-session-is-not-active-for-the-current-thread-illegalstateexception-no) – Bourbia Brahim Jul 24 '19 at 09:15
  • I have tried solutions explained in this thread but still in error with a message a little bit different as I explain in my message : "Cannot ask for request attribute - request is not active anymore!" instead of "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?" – Nono Jul 24 '19 at 09:30

0 Answers0