-
Notifications
You must be signed in to change notification settings - Fork 2
Initialization and finalization
The Spring.FluentContext offers also API to specify methods that have to be called when object is created and fully initialized, and before it is destroyed.
The CallOnInit() method allows to specify method of described object that has to be executed when object is instantiated and has all dependencies resolved.
As an opposite, the CallOnDestroy() method allows to specify method of described object that has to be executed just before object is destroyed in effect of context destruction.
//Configuration
ctx.RegisterDefault<Endpoint>();
ctx.RegisterDefault<Sender>()
.DependingOnDefault<Consumer>()
.Autowire();
ctx.RegisterDefault<Consumer>()
.Autowire()
.CallOnInit(c => c.Start())
.CallOnDestroy(c => c.Stop());
//Instantiation
Sender sender = ctx.GetObject<Sender>();
sender.Send("some text");
ctx.Dispose();Example above shows usage of those methods. When Consumer object is created, the Start() method is executed and it starts receiving messages from Sender. When ctx.Dispose() is called, whole context would be destroyed, but before Consumer destruction, Stop() method would be called.
Please note that if registered object implements IDisposable interface, instance would be properly disposed on context destruction
Continue reading: 12. Dependency checking