Announcement Announcement Module
Collapse
No announcement yet.
Unintended Interactions Using Singlton Beans Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unintended Interactions Using Singlton Beans

    I have a question on the use of singleton beans. Our project has a Service layer with stateless capabilities. On the face of it, this is the perfect use of Singleton beans since these capabilities store no explicit state. However, the follow interaction concerns me.

    Class A depends on B

    1. A is called and does it's work. Class B has a member variable that starts as null and is filled with some value during execution. This value is only useful during the one execution context and is throwaway data after that.

    2. A is called again. A does a
    if( B.stringValue != null) doSomething;

    This will fail since the string value was set during a previous execution cycle (albeit with non-stateful data).

    My question is:

    Is there a way to force spring to run a method (much like an init() method) on Singleton beans every time they are used? Or is the only solution to declare all these Stateless capabilities as Prototypes?

  • #2
    Lookup Method Injection is probably what you are looking for.

    Comment


    • #3
      Have you thought about using scoped proxies? Have a look at this.
      http://www.springframework.org/docs/...ther-injection

      Comment


      • #4
        This doesn't answer the question, but

        Is it impractical to clean up your variable in class B each time after you're done with it?

        You could write a very simple aop implementation to accomplish this behavior too.

        Charles

        Comment


        • #5
          I would store B.member in a ThreadLocal...

          -Patrick

          Comment


          • #6
            scoped proxy is clever

            Originally posted by karldmoore View Post
            Have you thought about using scoped proxies? Have a look at this.
            http://www.springframework.org/docs/...ther-injection
            That's a very interesting part of IoC that I'd not noticed previously. If bean A always accesses B via a getB() function, is this functionally equivalent to using lookup method injection?

            Charles

            Comment


            • #7
              I'm not sure I quite understand the question. Bean A has bean B injected into it. Depending on the scope, a new B will be created when required. If you used request scope, that would probably sort out your issue.

              Lookup method injection would do the same sort of thing for you.

              Comment

              Working...
              X