Hello,
in GUTS-Events I am using Guice TypeListener (and InjectionListener) in
order to process annotations of Guice-injected objects.
But my TypeListener itself requires some service, which is created by
Guice (and I need it injected by Guice then).
Unfortunately, Guice Binder.bindListener() only takes a TypeListener
instance and never ever tries to inject its dependencies.
I have tried to circumvent this Guice limitation by explicitly requiring
to inject the TypeListener instance with binder.requestInjection().
This works in some situations but not all. This morning I have faced a
new situation where this crashed, this happened because my TypeListener
instance was injected AFTER another Guice-injected object which was
passed to my (incomplete) TypeListener for processing.
Here is a small test case I have written (I made it as simple as I
could) that shows my problem:
@Test public class TypeListenerProblemTest
{
public void checkTypeListenerIsInjected()
{
final InjectedMain main = new InjectedMain();
// Create Guice injector
Injector injector = Guice.createInjector(new AbstractModule()
{
@Override protected void configure()
{
InjectedTypeListener typeListener = new
InjectedTypeListener();
requestInjection(typeListener);
Matcher<TypeLiteral<?>> matcher = new
AbstractMatcher<TypeLiteral<?>>()
{
public boolean matches(TypeLiteral<?> type)
{
return
(type.getRawType().equals(InjectedMain.class));
}
};
bindListener(matcher, typeListener);
bind(InjectedMain.class).toInstance(main);
bind(String.class).toInstance("Dummy");
}
});
}
static public class InjectedTypeListener implements TypeListener
{
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I>
encounter)
{
assert _something != null;
}
@Inject String _something;
}
static public class Dependency
{
}
static public class InjectedMain
{
@Inject Dependency _dependency;
}
}
This a TestNG test case, but it is easily adaptable to JUnit, or even no
test lib at all (as a main class).
The interesting stuff is the module configure() content and the
InjectedTypeListener implementation. The assert in hear() will always throw!
Now my question is: how can I circumvent this problem? I would be glad
to hear ideas to dig further because I feel a little bit lost now.
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
-~----------~----~----~----~------~----~------~--~---