Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I replace a Spring bean definition at runtime?
    primarykey
    data
    text
    <p>Consider the following scenario. I have a Spring application context with a bean whose properties should be configurable, think <code>DataSource</code> or <code>MailSender</code>. The mutable application configuration is managed by a separate bean, let's call it <code>configuration</code>.</p> <p>An administrator can now change the configuration values, like email address or database URL, and I would like to re-initialize the configured bean at runtime.</p> <p>Assume that I can't just simply modify the property of the configurable bean above (e.g. created by <code>FactoryBean</code> or constructor injection) but have to recreate the bean itself.</p> <p>Any thoughts on how to achieve this? I'd be glad to receive advice on how to organize the whole configuration thing as well. Nothing is fixed. :-)</p> <p><strong>EDIT</strong></p> <p>To clarify things a bit: I am not asking how to update the configuration or how to inject static configuration values. I'll try an example:</p> <pre><code>&lt;beans&gt; &lt;util:map id="configuration"&gt; &lt;!-- initial configuration --&gt; &lt;/util:map&gt; &lt;bean id="constructorInjectedBean" class="Foo"&gt; &lt;constructor-arg value="#{configuration['foobar']}" /&gt; &lt;/bean&gt; &lt;bean id="configurationService" class="ConfigurationService"&gt; &lt;property name="configuration" ref="configuration" /&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>So there's a bean <code>constructorInjectedBean</code> that uses constructor injection. Imagine the construction of the bean is very expensive so using a prototype scope or a factory proxy is not an option, think <code>DataSource</code>.</p> <p>What I want to do is that every time the configuration is being updated (via <code>configurationService</code> the bean <code>constructorInjectedBean</code> is being recreated and re-injected into the application context and dependent beans.</p> <p>We can safely assume that <code>constructorInjectedBean</code> is using an interface so proxy magic is indeed an option.</p> <p>I hope to have made the question a little bit clearer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload