Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Session Scope in Spring Beans

Tags:

java

spring

jsf-2

I'm using JSF 2 for the view and Spring for the business logic. I'm trying to set a session scope to one of my spring beans using annotations(@Scope("session")), but I'm getting this exception:

    SEVERE: Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'handleFiles': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private creazione.util.FTPOperations creazione.components.HandleOldFiles.operations; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'ftpOperations': 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.

I know about RequestContextListener. It's in my web.xml. I've also added RequestContextFilter:

<listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <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>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>

Nothing seems to work. What am I doing wrong? Thank you!

like image 752
spauny Avatar asked Sep 29 '11 13:09

spauny


2 Answers

If you are using annotation based configuration, just change in your main config xml this tag:

<context:component-scan base-package="my.package"/>

to

<context:component-scan base-package="my.package" scoped-proxy="targetClass" />

Also it is possible to mark class for working with proxy via annotations:

@Component
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)

This work for me. More details here: 4.12 Classpath scanning, managed components and writing configurations using Java

Also if you're create bean via context xml configuration, here is another example for this case:

<bean id="somebean" class="com.package.beans" scope="session">
    <aop:scoped-proxy/>
</bean>
like image 76
msangel Avatar answered Sep 30 '22 19:09

msangel


Try to define the beans that have to be injected as session with aop:scoped-proxy.

<bean id="ftpOperations" class="..." scope="session">
    <aop:scoped-proxy/>
</bean>

Add also the relevant namespace, if it's not present already:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
    ...
xsi:schemaLocation="
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        ...
like image 35
stivlo Avatar answered Sep 30 '22 18:09

stivlo