Announcement Announcement Module
Collapse
No announcement yet.
One interface with multiple impl classes?? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • One interface with multiple impl classes??

    Is it possible to link one interface with multiple impl classes in Spring? In other words, I have several similar classes, each handles similar yet slightly different tasks. I want to use factory pattern so I group them under one interface. Now I run into problem of wiring them in the xml file since Spring only allows one bean id with one impl class. Spring loads all bean definition up front when server starts, so it's kind of like a "static" binding. How can I solve this problem?

    Thanks in advance,
    Danny

  • #2
    I'm not sure, but I think what you're looking for can be solved by using a FactoryBean[1], described in the Pro Spring book by Rob Harrop on p. 127-132. A Google on "Spring" and "FactoryBean" can point you to some blog entries (such as [2]) that may help determine if the FactoryBean will be useful for you.

    Glen

    [1] http://static.springframework.org/sp...ctoryBean.html

    [2] http://blog.arendsen.net/index.php/2...e-uncreatable/

    Comment


    • #3
      I found this article:

      http://blog.arendsen.net/index.php/2...i-hotswapping/

      Unfortunately, I couldn't get it to work after following the HotSwappableTargetSource example. Does anyone know how it works? I kept getting exception thrown during the startup indicating "Error creating bean".

      Thanks
      Last edited by dsyliu; Jun 30th, 2006, 05:58 PM.

      Comment


      • #4
        I'm not sure what you mean by
        since Spring only allows one bean id with one impl class
        You can have many beans that use one impl class.

        Comment


        • #5
          You can have many beans that use one impl class.
          What I mean is that in the xml file you have to specify one bean id with one class.

          <bean id="xyz" class="com.abc.xyzImpl"/>

          I am wondering if there is a way to associate multiple impl classes with one bean id (imagine how factory pattern works: one interface with multiple impl classes), and I can decide which impl classes that I want to use during runtime.

          I found articles talking about HowSwappableTargetSource class, but I failed to follow the example. Does anyone have similar experience or has successfully get that class to work?

          Comment


          • #6
            Now I understand the question. Sure Hot swapping is an approach, but why not just create a 'normal' factory class and get that out of the Spring context?

            I did use the HowSwappableTargetSource to work while learning Spring, followed the reference guide and some books.

            Comment


            • #7
              why not just create a 'normal' factory class and get that out of the Spring context?
              It goes back to my original question, how can we associate one bean id to multiple impl classes in xml file? Let's say I have 2 impl classes all use the same interface. I have a factory to decide which impl class to use at runtime. It will be something like

              Code:
              if (x) return new ImplOne(); else return new ImplTwo();
              My caller class will be like

              Code:
              Interface a = factory.getImpl();
              where Interface should bind as <bean id="a" class="???"/> Spring's xml definition has to be set up beforehand, and I need a solution for dynamic change.

              I tried to follow the Spring doc regarding to ProxyFactoryBean (a section that describes HowSwappableTargetSource) and kept getting errors.

              Comment


              • #8
                1st of all, ask yourself a question: how would I do it without spring?

                2nd, in your place I would pass to a "client" a reference (spring's bean) not to the interface, but to the factory. In that way, the only thing you would need to do is just call a getImpl() method of your factory bean.

                also, IMHO, the use of hotSwapable thing is not the best solution here, because whenever you swap the implementation behind the proxy, it affects ALL clients of you bean. I don't think it fits well into your idea of a "dynamic change".

                on the other hand, the factory (not Spring's FactoryBean) gives you everything you need, whithout any restrictions as with HotSwapableTS.

                Comment


                • #9
                  Originally posted by dsyliu
                  It goes back to my original question, how can we associate one bean id to multiple impl classes in xml file? Let's say I have 2 impl classes all use the same interface. I have a factory to decide which impl class to use at runtime. It will be something like

                  Code:
                  if (x) return new ImplOne(); else return new ImplTwo();
                  Did you try implementing this in a FactoryBean? That piece of logic can be put in your FactoryBean. And in the bean definition just use the FactoryBean impl class for the class attribute.

                  Comment

                  Working...
                  X