1

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

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

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

1 回答 1

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 中,因此您不必刷新上下文。很简单吧?

于 2013-09-20T14:56:48.560 回答