刷新上下文而不刷新文件中的配置

时间:2013-09-20 14:22:11

标签: spring

是否可以在不刷新配置的情况下刷新上下文?

我找到了方法ConfigurableApplicationContext.refresh()但它从文件刷新配置。 我想在运行时执行以下步骤:

  1. 获取上下文和bean
  2. 更改bean属性值
  3. 刷新上下文

1 个答案:

答案 0 :(得分:1)

你想要的是有点奇怪。您的工厂THEMSELVES可能是从属性初始化的,因此如果您只是“刷新上下文”,而不关心它来自何处,您的工厂将恢复自己的初始状态。让我们来形容它。

1. Context Started
2. Factory initalized
3. Factory used to create beans
4. ..application running
5. You manually change a FACTORY prop that doesn't affect the factory, but a specific Bean. You want your bean refreshed. You think refreshing the context will do that for you
6. Boom, you refresh the context. Factories are reinitialized to their default values
7. Factories are used to create beans, which are the same beans in step 2, not the ones you would want in step 5, since the factories themselves are reverted to their default properties

解决方案是什么?

您可以更改设置方法。无论您的工厂传递给您的子bean的属性是什么,您都可以在子bean中自动装配。然后,当您更改这些属性时,它们将反映在子bean中,因此您不必刷新上下文。简单,呵呵?