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

Java 设置新配置后如何更新所有spring对象?,java,spring,dependency-injection,configuration,autowired,Java,Spring,Dependency Injection,Configuration,Autowired,动态配置更改后,如何刷新以前的@Autowiredspring对象 // Here is my updateConfig method GenericApplicationContext context = new GenericApplicationContext(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context); reader.loadBeanDefinitions(new ClassPathR

动态配置更改后,如何刷新以前的
@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对象的解决方案?

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


若您使用的是SpringBoot,那个么可以将注释用于。Spring actuator endpoint
/refresh
重新加载带有@RefreshScope注释的bean。

Spring Boot工作正常,谢谢!否则AbstractRefreshableApplicationContext对我不起作用,它无论如何都会加载新的配置,但即使所有最初自动连接的bean都包含旧的值谢谢确认,我也发现RefreshableApplicationContext不会更新对旧bean的引用。