如何在设置新配置后更新所有 spring 对象?

How can I update all spring objects after setting new configuration?

如何在动态配置更改后刷新以前 @Autowired spring 个对象?

// Here is my updateConfig method

GenericApplicationContext context = new GenericApplicationContext();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
reader.loadBeanDefinitions(new ClassPathResource("applicationContext.xml"));
context.refresh();

myApplicationContextAware.setApplicationContext(context);

使用 myApplicationContextAware.applicationContext.getBean(MyClass.class) 我可以通过新配置获取新实例,但是所有 @Autowired 对象仍然包含旧值

是否有刷新 spring 个对象的解决方案?

您可以为此使用 AbstractRefreshableApplicationContext。它提供了在 运行 时间内重新加载 bean 配置的方法。

如果您使用 spring 启动,那么您可以使用 @RefreshScope annotation with the spring actuator。 Spring 执行器端点 /refresh 重新加载用 @RefreshScope 注释的 bean。