Update spring beans dynamically. Is it possible?

Is there a way to update Spring bean dynamically if Spring beans configuration changes?

eg. Suppose I have a Spring bean with the boolean property x , and Spring beans is true when the application starts.

So, Spring creates a bean with property x set to true.

Is there a way that if I change the property to x (while the application is running), the property will be updated, for example. to false ?

+4
source share
4 answers

Calling the set method for the x method setX() will do this.

But it should not be a prototype bean.

+1
source

possibly with jrebel - spring integration. it tracks your configuration and TRIES to reconnect your beans at runtime.

Although I would not use it in production ... only for games, testing, etc.

+1
source

Spring reads configuration files at startup. If you really need to update configurations while the application is running, you must manually implement the whole chain: detect configuration changes, check configuration, detect changed beans, update beans in context.

0
source

Spring beans can be initialized using applicationContext.xml or even programmatically. In your case; you will need to remove the configurations from xml and add to the java program. You can get an idea from How to programmatically create a bean definition using nested properties? . Other good links are also available on Google.

0
source

Source: https://habr.com/ru/post/1012114/


All Articles