Hello Alen,
> Looks like the root problem is that TypeListner gets injected after it
> hears all the stuff.
>
Thank you for taking a look at the problem; yes this is exactly the
problem that I face.
> How about using a place holder (proxy) or something like that.
>
I'm not sure I see how this will solve the actual problem, because the
proxy will do nothing until the actual injection of my
TypeListener/InjectionListener. But all instances passed to the listener
or its proxy eventually need processing!
But this led me to a simple workaround (how comes I didn't think about
it earlier, this problem made me blind?):
Here is how I changed my ConsumerInjectionListener implementation:
public class ConsumerInjectionListener implements InjectionListener<Object>
{
@Inject public void setEventService(EventService service)
{
_service = service;
// Register all pending instances with the newly injected
EventService
for (Object injectee: _pendingInjectees)
{
registerInjectee(injectee);
}
_pendingInjectees.clear();
}
public void afterInjection(Object injectee)
{
registerInjectee(injectee);
}
private void registerInjectee(Object injectee)
{
// At Injector creation, it may happen some objects are injected
and passed
// to afterInjection() _before_ setEventService() has been
called by Guice;
// In such cases, consumer registration is deferred for those
objects until
// setEventService() gets called.
if (_service != null)
{
_service.registerConsumers(injectee);
}
else
{
_pendingInjectees.add(injectee);
}
}
private EventService _service = null;
final private List<Object> _pendingInjectees = new ArrayList<Object>();
}
Just deferring use of _service on Guice-injected instances until
_service is actually injected in the listener is a perfect (and very
simple) solution to this problem!
Deferring instances processing should have been so obvious:
- EventService will be injected at the latest just before
Guice.createInjector returns, which means that the wait duration until
instances processing is extremely short (<1s)
- in the meantime, nothing important can happen normally: I don't expect
events to be sent at objects construction time (that could be a corner
case though)
So that's it!
Thanks Alen for your proxy idea, it definitely pointed me into the right
direction!
Cheers
Jean-Francois
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---