One of the challenges I have in using Guice is that there is not way
for a bound object to discern the context in which it was bound. To
get around that, I encoded the context into the class name by sub-
classing the target of a binding.
For example, I would like to configure objects of the Target class
below differently depending on whether they are bound for StageOne or
bound for StageTwo:
bind(TargetInterface.class).annotatedWith(StageOne.class).to(Target.class);
bind(TargetInterface.class).annotatedWith(StageTwo.class).to(Target.class);
To do this now, I encode the annotation into the class name by:
class StageOneTarget extends Target {
...
}
class StageTwoTarget extends Target {
...
}
bind(TargetInterface.class).annotatedWith(StageOne.class).to(StageOneTarget.class);
bind(TargetInterface.class).annotatedWith(StageTwo.class).to(StageTwoTarget.class);
Then, I can inspect the classname so that I can configure
StageOneTarget differently than StageTwoTarget.
Is it possible to inject the Key used in the binding into Target as
the following pseudo-code suggests?
class Z {
@Inject
Z( Key key, ...) {}
Class <? extends Annotation> clazz = key.getAnnotationType();
}
}
Is there another way to inject parameters so that I can distinguish
how an object was injected so that I can configure it differently?
--
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.