9

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!

spauny
  • 4,976
  • 9
  • 44
  • 61

2 Answers2

27

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>
msangel
  • 9,895
  • 3
  • 50
  • 69
  • Hi this did'nt worked for me. All Three Ways i get the same error could not wire. – Atul Darne Sep 27 '13 at 13:05
  • There are many reasoms why it is not autowired - broken scope settings, absent cglib it classpath(in case if you're autowire without interfaces). See your error message for more details. – msangel Sep 27 '13 at 18:05
10

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
        ...
stivlo
  • 83,644
  • 31
  • 142
  • 199
  • 3
    Yeah but I'm using annotations. I have quite a lot of spring beans to write in the applicationContext.xml. Is there an annotation equivalent with ? – spauny Sep 29 '11 at 14:17
  • try to look at this question http://stackoverflow.com/questions/4503606/annotation-equivalent-of-aopscoped-proxy – stivlo Sep 29 '11 at 14:20
  • Still doesn't quite work...I've added @Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES), I've refactored an interface for every spring bean, but now the AutoWiring doesn't recognize my beans anymore... – spauny Sep 29 '11 at 14:50
  • what is the error message? remember that you can specify a Qualifier to help the wiring process such as: @Autowired @Qualifier("beanName") – stivlo Sep 29 '11 at 14:52
  • Yes I have used the Qualifier; The exception is: Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [creazione.util.FTPOperations] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=ftpOperations)} – spauny Sep 29 '11 at 14:58
  • some stupid questions: creazione.util.FTPOperations is an interface now? Is there a bean implementing it defined as @Component and its package is in context:component-scan? – stivlo Sep 29 '11 at 15:03
  • FTPOperations it's not an interface (is implementing an interface called Operations) and yes is defined as @Component and yes its package is in component-scan... It's very strange.... – spauny Sep 30 '11 at 05:23
  • Not quite... I managed to add the session scope to my beans by adding the @Scope(value = "session") annotation and an each interface for every spring bean, but for that to work I deleted the ScopedProxyMode attribute from the Scope annotation - I don't need it for now but I'll need it in the future; Maybe then I'll write the aspect config. in the applicationContext.xml – spauny Sep 30 '11 at 10:16
  • If you use the ScopedProxyMode.INTERFACES variant you need to change all bean members that are autowired to use the interface instead of the concrete class. – Jon Jan 22 '14 at 13:24