Announcement Announcement Module
No announcement yet.
Singleton on session Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Singleton on session


    Is there a way to declare a bean to be singleton in a session not an application context?

  • #2
    What kind of session are you talking about? HTTP session, Hybernate session, ...?

    There is no way to declare the bean as having session scope in the Spring application context or anything like that, but you could maybe implement this kind of behaviour in another way.



    • #3
      I need a controller to be singleton in a HTTP session, not in an application context.


      • #4
        From spring point of view, your bean is not a singleton. You should make it a prototype in the app context and attach an intance to the user session on the login form for example. You can then use an interceptor to read this value before any controller is called and store it in a ThreadLocal and using anotehr interceptor restore this value to the user session.



        • #5
          Originally posted by catalin
          I need a controller to be singleton in a HTTP session, not in an application context.
          You could implement this with a custom TargetSource implementation class, but it depends on the web framework you are using. WebWork for example allows you to get the HttpServletRequest and thus also the session by using ServletActionContext, Echo offers a similar functionality. If you can get the session through such a mechanism inside the TargetSource implementation class, you could retrieve your object from the session if it is available and otherwise get a new prototype from spring and put it in the session.