PDA

View Full Version : destroy-method and IConfigurableApplicationContext.Refresh


jbuedel
02-09-2007, 10:03 PM
I am seeing unexpected behavior (to me anyway) where calling IConfigurableApplicationContext.Refresh on my context is not calling the methods I've registered via the destroy-method attribute. Shouldn't it?

I think I've gotten around the problem by calling IApplicationContext.Dispose before calling Refresh. But that doesn't seem right as I expect a Disposed object to be "invalid". Would someone care to comment on my solution (below) and perhaps suggest a better way?

I've got a system setup where a FileSystemWatcher detects changes in my Spring.Net config file and attempt to update the context, via this function:

private void OnSpringObjectsFileChangedHandler(object state)
{
try
{
Log.Info("Spring objects definition file changed. Refreshing the IApplicationContext.");
IApplicationContext context = ContextRegistry.GetContext();
context.Dispose();
IConfigurableApplicationContext c = (IConfigurableApplicationContext) context;
c.Refresh();
}
catch (Exception ex)
{
Log.Error("Error while refreshing the IApplicationContext.", ex);
}
}Thanks for any help!
Josh

Erich Eichinger
02-11-2007, 12:03 PM
Hi Josh,

thanks for reporting this. This is (has been) a bug and I just committed a fix to CVS. You should get the fixed version with the next nightly build.

I recorded this bug in JIRA as SPRNET-479 (http://opensource.atlassian.com/projects/spring/browse/SPRNET-479)

cheers,
Erich

jbuedel
02-12-2007, 04:23 PM
Erich,

Wow, that's great. Thank you very much.

I've been using the build available from the downloads section of the web site. At this time I'm near the end of my project and as such I'm hesitant to introduce the changes that would come along with using the latest and greatest from the nightly build. Do you feel my solution (calling Dispose) is a reliable workaround in the meantime?

Thanks again,
Josh

Erich Eichinger
02-12-2007, 04:30 PM
Hi Josh,

You are right: Calling Dispose() directly on the ApplicationContext is not the best idea. It is better to call Dispose() on the underlying ObjectFactory:


((IConfigurableApplicationContext)appCtx).ObjectFa ctory.Dispose();
appCtx.Refresh();


This will dispose your objects and rebuild/replace the ObjectFactory instance.

cheers,
Erich

jbuedel
02-12-2007, 06:26 PM
Thanks again.