我可以在运行时替换Spring bean定义吗? [英] Can I replace a Spring bean definition at runtime?

查看:251
本文介绍了我可以在运行时替换Spring bean定义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下情况。我有一个Spring应用程序上下文,其bean属性应该是可配置的,想想 DataSource MailSender 。可变应用程序配置由一个单独的bean管理,我们称之为配置

Consider the following scenario. I have a Spring application context with a bean whose properties should be configurable, think DataSource or MailSender. The mutable application configuration is managed by a separate bean, let's call it configuration.

管理员现在可以更改配置值,如电子邮件地址或数据库URL,我想在运行时重新初始化配置的bean。

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.

假设我不能简单地修改属性上面的可配置bean(例如由 FactoryBean 或构造函数注入创建)但必须重新创建bean本身。

Assume that I can't just simply modify the property of the configurable bean above (e.g. created by FactoryBean or constructor injection) but have to recreate the bean itself.

有关如何实现这一点的任何想法?我很高兴收到有关如何组织整个配置的建议。没有什么是固定的。 : - )

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. :-)

编辑

稍微澄清一下:我不是询问如何更新配置或如何注入静态配置值。我将尝试一个例子:

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:

<beans>
    <util:map id="configuration">
        <!-- initial configuration -->
    </util:map>

    <bean id="constructorInjectedBean" class="Foo">
        <constructor-arg value="#{configuration['foobar']}" />
    </bean>

    <bean id="configurationService" class="ConfigurationService">
        <property name="configuration" ref="configuration" />
    </bean>
</beans>

所以有一个使用构造函数的bean constructorInjectedBean 注射。想象一下bean的构造非常昂贵,因此使用原型范围或工厂代理不是一种选择,想想 DataSource

So there's a bean constructorInjectedBean 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 DataSource.

我想要做的是每次更新配置时(通过 configurationService bean constructorInjectedBean 正在重新创建并重新注入应用程序上下文和依赖bean。

What I want to do is that every time the configuration is being updated (via configurationService the bean constructorInjectedBean is being recreated and re-injected into the application context and dependent beans.

我们可以放心地假设 constructorInjectedBean 是使用界面,所以代理魔术确实是一个选项。

We can safely assume that constructorInjectedBean is using an interface so proxy magic is indeed an option.

我希望这个问题更清楚一点。

I hope to have made the question a little bit clearer.

推荐答案

我可以想到一个'holder bean'方法(本质上是一个装饰器),持有者bean委托给holdee,它是持有者bean,它作为依赖注入其他人没有其他人提到持有者而是持有者。现在,当持有者bean的配置被更改时,它会重新创建持有者他的新配置并开始委托给它。

I can think of a 'holder bean' approach (essentially a decorator), where the holder bean delegates to holdee, and it's the holder bean which is injected as a dependency into other beans. Nobody else has a reference to holdee but the holder. Now, when the holder bean's config is changed, it recreates the holdee with this new config and starts delegating to it.

这篇关于我可以在运行时替换Spring bean定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆