5

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

E.g. assume that I have a spring bean with boolean property x and the spring beans has the value true when the application starts.

So spring creates the bean with the property x set to true.

Is there a way so that if I changes the property to x (while the application is running) that the property will be updated e.g. to false?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Jim
  • 18,826
  • 34
  • 135
  • 254
  • See also http://stackoverflow.com/questions/534030/reloading-refreshing-spring-configuration-file-without-restarting-the-servlet-co – artbristol Jan 18 '13 at 10:08

4 Answers4

1

Calling the setter for x setX() method will do that.

But it should not be a prototype bean.

Mawia
  • 4,220
  • 13
  • 40
  • 55
  • I am asking about modifying the xml configuration file. – Jim Jan 18 '13 at 08:35
  • Oh! What is that? Is that possible? Yes, overwrite your xml file but that won't change anything. It's already loaded, no more effect on your running instance in Random Access Memory. :( – Mawia Jan 18 '13 at 09:39
1

it is possible with the jrebel-spring integration. it monitors your configuration and TRIES to re-wire your beans at runtime.

Though i would not use it in production...only for playing around, testing etc

wrm
  • 1,898
  • 13
  • 24
0

Spring reads configuration files at startup. If you really need to update configs while application running, you should manually implement all the chain: detecting config changes, validating config, detecting changed beans, updating beans in context.

Myroslav
  • 91
  • 1
  • 1
  • 2
  • It is hard long way. You can look at spring sources to find validation and beans loading functionality, but other step should be implemented by you. Do you really need this? Other ways are to update beans without updating configs or create new Spring context from config and replace existent – Myroslav Jan 18 '13 at 08:49
0

Spring beans can be initialized using applicationContext.xml or even programatically. In your case; you will need to remove configurations from xml and add into java program. You can get some idea from How to programmatically create bean definition with injected properties? . Other good links were also available on google.

Community
  • 1
  • 1
Deepak Singhal
  • 10,568
  • 11
  • 59
  • 98
  • I am asking about the administrator modifying the xml configuration file manually – Jim Jan 18 '13 at 09:04