Announcement Announcement Module
Collapse
No announcement yet.
Autowiring session-scoped bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowiring session-scoped bean

    I'm having difficulties with a new session-scoped bean I've created, userPreferences. I get the following lengthy exception when I try to autowire it one of my Controllers:

    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'welcomeController': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: com.foo.web.UserPreferences com.foo.web.WelcomeController.userPreferences; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'userPreferences': 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've defined the userPreferences bean as follows:

    Code:
    @Component
    @Scope("session")
    public class UserPreferences {
      ...
    }
    And I autowired it into my Controller as follows

    Code:
    @Controller
    public class WelcomeController {
    
       @Autowired
       UserPreferences userPreferences;
    
    }
    I configured my dispatcherServlet.xml with the following to recognize annotations:

    Code:
    <context:annotation-config />
    <context:component-scan base-package="com.foo.web"/>
    Any ideas what I'm missing?
    Last edited by mark_m; Jan 6th, 2009, 10:50 AM.

  • #2
    Change your component-scan tag as follows:

    Code:
    <context:component-scan base-package="com.foo.web" scoped-proxy="targetClass" />
    This will add a scope proxy as needed. You should make sure you have CGLIB in your classpath, since it is required.

    Comment


    • #3
      Thank you. That resolved the issue.

      Comment

      Working...
      X