0

I have a bean BeanA with injected property private int url:

class BeanA { @Value(${"db.url"}) private String url; private DbConnection connection; }

Let us say this Value annotation is similar to Spring Value. During initialization, connection will be initialized by using injected property into url field. And also there is some PostConstruct and PreDestroy methods in BeanA. My question is: is it possible to dynamically reinstantiate BeanA when url property changed. I have mechanism of detecting property change. For now, I just re-inject this url only, but I want to recreate this bean, initialize new connection and re-inject this bean in all dependetn beans. I dont use Spring Cloud Config.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Igor Dumchykov
  • 347
  • 1
  • 15
  • The @RefreshScope looks helpful in this answer: (But it's depending on Spring Cloud) https://stackoverflow.com/questions/51218086/how-to-reinitialize-a-spring-bean – Pam Stums Mar 21 '19 at 00:31
  • Yes, but it is from Spring. I dont use Spring. The only way i see now is implementing my own RefreshScope with Context. – Igor Dumchykov Mar 21 '19 at 00:32

1 Answers1

0

If you don't use spring at all, I suggest:

  1. Leave the "bean" as is. (So it will serve as Singleton).
  2. add a method in the bean: getConnection()
  3. When the property changes, recreate a new connection inside that bean.
  4. Any component that needs the connection will always call the bean's getConnection() and it will always get the most updated connection instance.

OR

You might want to use a Proxy design pattern where your bean is used by the client but internally refers to another connection bean (the "target" bean) and the target may be replaced with a new instanse of a completely new bean. But always, the client/user holds the same reference to the proxy.

Pam Stums
  • 176
  • 8
  • Thanks, but the problem is that I`m writing a lib. So if client will use it, I just want to re-load his bean once property of that bean will change and I think it should be possible to do in some generic way. – Igor Dumchykov Mar 21 '19 at 00:41
  • 1. So is this a pure Java class? 2. Is the bean part of the lib or is it a user class? I mean, it can still be a class where the user always have the same reference to it but the state of the class changes. (as described in the answer above). Otherwise, you might want to use a Proxy design pattern where your bean is used by the client but internally refers to another connection bean (the "target" bean) and the target may be replaced with a new instanse of a completely new bean. But always, the client/user holds the same reference to the proxy. – Pam Stums Mar 21 '19 at 00:59
  • Sorry, this is just an example. I meant that I want to somehow reload beans (re-instantiate and re-inject in all dependent beans) once their properties changed and I re-injected those properties, like I did with this url. – Igor Dumchykov Mar 21 '19 at 01:02
  • Ok, if I understood you correctly, I should wrap my instance in some proxy and inject this proxy to all dependent beans and once internal state of actual instance will change, I re-instantiate only target instance but all dependent beans will not change dependency on proxy. Smth like that? – Igor Dumchykov Mar 21 '19 at 01:05
  • Yes, exactly! I'll also revise my answer to include this proxy option... – Pam Stums Mar 21 '19 at 02:18
  • Ok, I`ll try this solution and will leave my comment after that. Thanks! – Igor Dumchykov Mar 21 '19 at 11:45