如何在设置新配置后更新所有弹簧对象? [英] How can I update all spring objects after setting new configuration?

查看:293
本文介绍了如何在设置新配置后更新所有弹簧对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I refresh previously @Autowired spring objects after dynamic configuration changes?

// 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 对象仍然包含旧值

With myApplicationContextAware.applicationContext.getBean(MyClass.class) I can get new instances by new configuration, but all @Autowired objects still contain old values

有什么办法刷新弹簧对象?

Is there any solution to refresh spring objects?

推荐答案

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

You can use AbstractRefreshableApplicationContext for this. It provides methods to reload bean configuration in run time.

如果你使用spring boot,那么你可以使用 @ RefreshScope 注释与弹簧执行器。弹簧执行器端点 / refresh 重新加载用@RefreshScope注释的bean。

If you're using spring boot then you can use @RefreshScope annotation with the spring actuator. Spring actuator endpoint /refresh reloads the beans annotated with @RefreshScope.

这篇关于如何在设置新配置后更新所有弹簧对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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