PDA

View Full Version : Refreshing spring managed beans


kbaum
Aug 25th, 2004, 10:30 PM
In my application, some of the singleton beans are initialized with information from the database. We are using an administration tool to refresh our cached data, but this only helps our DAO's. Is there an easy way to get all of the beans in the spring container to reinitialize themselves without restarting the servlet container? Thanks.

Karl

Colin Sampaleanu
Aug 25th, 2004, 11:16 PM
You can get the context, then cast it to ConfigurableApplicationContext, on which you can call refresh(). This will force a re-init. You should be careful when and how you do this though. Obviously if you're in an a web app serving multiple request threads the result can be unpredictable, depending on the usecase and the objects in there. Just as an example, an object which is in the middle of being used, could be holding a reference to another object which has a defined lifecycle (destroy method, etc.), and still try to use it after that other object thinks it is destroyed...

kbaum
Aug 26th, 2004, 08:01 AM
Hmmm.. you're right. It does seem unpredictable. I think the behaviour I really want is that existing requests continue to use the old application context, while a new application context is being created. Once the new application context has been successfully created, we can swap that in for all new requests to the application. Is this a possibility?

Thanks for your help!

Karl