Announcement Announcement Module
Collapse
No announcement yet.
Dynamically replace and re-wire singletons ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamically replace and re-wire singletons ?

    Hi,

    I tried google'ing for this but to no avail , please forgive me if this has been answered before.

    Let's assume a very basic configuration:

    Code:
    <beans ...>
    
       <bean id="aFactory" class="aClass">.....</bean>
    
       <bean id="bean1" class="...">
         <property name="factory"><ref bean="aFactory"/></property>
      </bean>
    
       <bean id="bean2" class="...">
         <property name="factory"><ref bean="aFactory"/></property>
      </bean>
    
    </beans>
    During my application's runtime I want to replace the singleton bean "aFactory" with a different bean (that is of course 'compatible' to 'aFactory', a subclass for example) that gets instantiated and populated by my application (because the bean's creation depends on data only available during runtime). All other singletons that have a reference to 'aFactory' should automatically be re-wired to use the new bean instance.

    This _should_ be possible because the part of my (single-threaded) application that would trigger this replacement 'owns' the ApplicationContext and it is therefore safe to re-wire (or even discard and recreate) the whole object graph / context.

    Basically, what I'm looking for is something like a

    BeanFactory#replaceAndRewireSingleton(String beanId, Object newBean)

    method.

    Any suggestions ?

    Thanks in advance,

    Tobias
    Last edited by Tobias Gierke; Feb 5th, 2009, 12:21 PM.

  • #2
    Since the bean (aFactory) which you want to replace is a singleton and is wired into other beans, so I think you need to refresh (http://static.springframework.org/sp...html#refresh()) the context when you are ready for it at runtime.

    Though it may be design change for you, but you might want to consider method lookup injection http://static.springframework.org/sp...thod-injection.

    Comment


    • #3
      First of all, thanks for your reply !

      I've been talking about this to a collegue of mine and like so often, that gave me a different perspective on my problem (or better, what I was really trying to achieve).

      My application is a framework that is used in three different scenarios ( stand-alone / 'embedded use' / 'embedded use for functional testing' ) , each of them requiring a slightly different bean wiring. Somehow I was under the impression that I should/could do this with just a single bean configuration file and dynamically manipulating the object graph during runtime.

      But it's waaaaay easier (that's what my collegue suggested) to just have 3 different Spring XMLs (one for each use-case) and configure the build tool (Maven) to create three different JARs with the corresponding configuration file.

      Regards,

      Tobi

      Comment

      Working...
      X