I also had this problem when binding different EntityManagers: My
solution based on the trunk and
http://www.kamalook.de:8080/hudson/job/GuiceX/ws/GuiceX/src/main/java/de/kamalook/guice/SubInjector.java
Using this SubInjector you can...
binder.install(SubInjector.wrapWith(Names.named("Bob"), new
BindBobsPhoneModule());
binder.install(SubInjector.wrapWith(Names.named("Bill"), new
BindBillsPhoneModule());
with your types...
class MyApp {
@Inject @Named("Bob")
Person person1;
@Inject @Named("Bill")
Person person2;
}
class MyPerson {
String name;
@Inject Phone phone;
}
BindBobsPhoneModule looks like this e.g.:
binder.bind(Phone.class).to(BobsPhone.class);
SubInjector.wrapWith(annotation, modules) basically creates a
*surprise* subinjector configured with the given modules and returns a
module that binds all the subinjector's bindings with the given
annotation.
On Sep 9, 10:06 am, Johannes Schneider <[EMAIL PROTECTED]> wrote:
> I think I don't get your example completely. And I am not sure that is a
> good thing to create two PersonModules (but maybe it is).
>
> But to get the Phone out of those modules you could create an additional
> "PhoneModule" that is installed first.
>
> Therefore the PersonModules don't bind the Phone again...
>
> binder.install(new PhoneModule()); //Contains the phone binding
> binder.install(new PersonModule("Bob"));
> binder.install(new PersonModule("Bill"));
>
> Regards,
>
> Johannes Schneider
>
> On Mon, 2008-09-08 at 14:33 -0700, Mikkel Petersen wrote:
> > Sorry I'm not sure I'm explaining me well enough but the important
> > thing is that
>
> > binder.install(new PersonModule("Bob"))
> > binder.install(new PersonModule("Bill"));
>
> > will fail because both modules makes a binding to a Phone, even though
> > the Phone binding is only important inside each PersonModule.
>
> > On 8 Sep., 22:55, "Logan Johnson" <[EMAIL PROTECTED]> wrote:
> > > What do you want to happen here? One Phone that's shared among your
> > > Person
> > > instances, or each Person gets its own Phone, or something entirely
> > > different?
>
> > > On Mon, Sep 8, 2008 at 4:48 PM, Mikkel Petersen <[EMAIL PROTECTED]> wrote:
>
> > > > I dont know if this makes sense, but there seems to be a problem when
> > > > you need multiple instances of
> > > > the same object, created using the same modules.
> > > > For example:
> > > > you have the mainmodule and the sub module will create the same kind
> > > > of object, only with a slight difference.
> > > > They will bind to different places in the application though.
>
> > > > The object sto inject into :
> > > > class MyApp {
> > > > [EMAIL PROTECTED] @Named("Bob")
> > > > Person person1;
> > > > [EMAIL PROTECTED] @Named("Bill")
> > > > Person person2;
> > > > }
>
> > > > class MyPerson {
> > > > String name;
> > > > [EMAIL PROTECTED] Phone phone;
> > > > }
>
> > > > The main module :
> > > > //install other modules
> > > > binder.install(module1);
> > > > binder.install(module2);
> > > > //install person modules
> > > > binder.install(new PersonModule("Bob"))
> > > > binder.install(new PersonModule("Bill"));
>
> > > > The person module:
> > > > //..configure person
> > > > class PersoModule {
> > > > String name;
> > > > public PersonModule(String name) {
> > > > this.name = name;
> > > > }
> > > > configure(Binder binder) {
> > > > //configure other stuff
> > > > binder.bind(Phone.class).toInstance(new CellPhone());
> > > > //bind person
>
> > > > binder.bind(Person.class).annotatedWith(Names.named(name).toInstance(new
> > > > EmployeePerson(name);
> > > > }
> > > > }
>
> > > > The binding will fail because
> > > > binder.bind(Phone.class).toInstance(new CellPhone());
>
> > > > Will be called twice. I know that it could be bind once in the main
> > > > module, but this is a simple example, imagine much more complicate
> > > > configuration in the PersonModule.
> > > > Problem is that what goes on after binder.install() always touches the
> > > > calling module, even though in this case, the Phone binding is only
> > > > relevant for the PersonModule and the objects created here.
> > > > I'd like a command like bindLocally :
>
> > > > binder.bindLocally(Phone.class).toInstance(new CellPhone());
>
> > > > That binding will only touch objects created in this module, not any
> > > > calling modules or modules called.
>
> > > > I tried to look at scopes but it doesnt seem to have anything to do
> > > > with this.
>
> > > > Thanks.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---