2

I have been putting efforts in understanding this error. I have gone through most of the questions related to this and I am not clear as most of them addressed how to solve it. I want to understand what error is and why is it coming . Please help me in understanding below :

  1. Why does spring look into current thread for the scope "session" ? Does spring store all the request attributes and session attributes in the thread context ?

2.I recieve this error inconsistently. If it is an issue, it should come all the times. Below is my code snippet.

-- Application context

<bean id="location" class="com.mindtree.ruc.cmn.utils.LoginLocation" scope="session"> <aop:scoped-proxy/> </bean> <bean id="homePageRH" class="com.rsaame.pas.home.ui.HomePageRH"> <property name="location" ref="location" /> </bean> 

-- web.xml

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

-- logs:

Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.rsaame.pas.dao.cmn.PASStoredProcedure]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.location': Scope 'session' 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. at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:141) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:108) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:280) ... 42 more Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.location': Scope 'session' 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. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:341) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192) at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:33) at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.getTarget(Cglib2AopProxy.java:653) at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:604) 

2 Answers 2

7

The Problem is that the session scope is only available in Spring Contexts that run in a Web Context. -- But in a normal web application you have two Spring Contexts!! One that is started by

<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 

and a second that is started by:

<servlet> <servlet-name>SpringProject-DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/webmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> 

In the first context (loaded by the ContextLoaderListener) it does not run in a "WebContext", therefore do not have a session scope!

Easyest solution When possible: Move that sesson scoped been to the web context (webmvc-config.xml in the example above)


Maybe this helps a bit: ContextLoaderListener or not?

Sign up to request clarification or add additional context in comments.

1 Comment

Great Explanation :) I have been trying to understand and this answer clarified it all.
2

The answer to your first question lies in your configuration itself. The bean scope is defined as session and you have specified <aop:scoped-proxy/> as well. This means that this particular bean is to be used within a thread which mandatorily has a HTTP session associated with it. If in case no session is available you get the mentioned error.

Answer to second question is that it seems you are referring this bean from at least two other beans; one which has HTTP session associated with it while the other doesn't. Hence the issue is intermittent.

To get rid of it you need to analyse your bean definitions and ensure that bean in question is used / injected such that a HTTP session is available while invocation.

1 Comment

@JavaBond..Thanks for the reply. Yes, there is another singleton bean calling the session bean in its constructor.I think I have to enforce condition that,session bean is created before singleton bean is constructed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.