【发布时间】:2019-03-21 18:07:35
【问题描述】:
我有一个注入属性private int url的bean BeanA:
class BeanA { @Value(${"db.url"}) private String url; private DbConnection connection; }
让我们说这个Value注解类似于Spring Value。在初始化过程中,将使用注入属性到 url 字段来初始化连接。在BeanA 中还有一些PostConstruct and PreDestroy 方法。
我的问题是:当url 属性更改时,是否可以动态重新实例化BeanA。我有检测属性变化的机制。现在,我只是重新注入这个 url,但我想重新创建这个 bean,初始化新连接并在所有依赖 bean 中重新注入这个 bean。我不使用 Spring Cloud Config。
【问题讨论】:
-
@RefreshScope 在这个答案中看起来很有帮助:(但它取决于 Spring Cloud)stackoverflow.com/questions/51218086/…
-
是的,但它来自 Spring。我不使用弹簧。我现在看到的唯一方法是使用 Context 实现我自己的 RefreshScope。
标签: jakarta-ee dependency-injection cdi weld