Announcement Announcement Module
Collapse
No announcement yet.
getting Spring-instantiated bean in a non-spring bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    As soon as new is replaced with factory there are a dozen of methods to instantiate prototype bean in the code that should not have any knowledge of Spring (it really does not matter is this prototype injected with something or not). It may dynamically generated factory (as you suggested, it may be manually written factory (really, it seems to require less code+configuration then ServeceLocatorFactoryBean approach, as such manual factory needs only one line of code save headers), it may be anything else ... .

    The real challenge is to keep new in place.

    Comment


    • #17
      Hi Vasaya10,
      Thank you for your suggestion - so far it is more compelling than real AOP for the project I am working on.. I have one question though.
      In your concrete class, do you still have to do the setter/getters for the DI to work? as in the code below?


      Code:
      public class PrototypeBImpl implements PrototypeB {
        private SingletonA singletonA;
       
        public void setSingletonA(SingletonA a) {
             singletonA = a;
        }
        public SingletonA getSingletonA() {
         return singletonA;
      }
      Again, thanks for the tips - I'm new to spring..

      Comment


      • #18
        In your concrete class, do you still have to do the setter/getters for the DI to work? as in the code below?
        That is correct. The concrete Impl class (which is a prototype) will have the DI references, just like u mentioned in the code.

        All you are doing is just creating new instances of the Impl class (PrototypeBImpl in this case) via the SLFB. Although there is some extra code (interfaces, in this case) to create, it works pretty well.

        Comment


        • #19
          Thanks Vayasa10: This works great. Only thing is how can I prevent anyone from doing a new() on prototypeB? Is there a way to enforce it so that the object can only be instantiated from the factory?

          Thanks again!
          Dipita

          Comment


          • #20
            Originally posted by Dipita View Post
            Thanks Vayasa10: This works great. Only thing is how can I prevent anyone from doing a new() on prototypeB? Is there a way to enforce it so that the object can only be instantiated from the factory?

            Thanks again!
            Dipita
            There is one way only - reduce visibility of your constructor (do not make it public). It would not preclude Spring from instantiating your object as it uses reflection and call setAccessible() on Constructor object to overcome visibility limitations, you only have to hope that other users of your object would not do the same.

            Comment


            • #21
              HI all,
              Thanks for all the great suggestions and help! This forum ROCKS!!!

              Dipita

              Comment

              Working...
              X