By default guice binds everything into a global "scope" (note this is not a scope in the sence guice uses this word).
If you want to have a module scope you should extend PrivateModule and use the expose() method to expose bindings to the global scope.

If you do want to configure different servers you may want to have a look at the multibindings extension.
It allows for having multiple bindings of the same interface/class. You get a set (or a map) injected instead of a singel instance.



On 10/10/2012 12:20 AM, Rob Withers wrote:

Hi Olivier,

 

I am still having trouble understanding how to contextualize a module with some data, whether value objects or not.  Here is my attempt (I am a long way from being able to test it):

 

public class MurmurModule extends AbstractModule {

       private final WhisperIdentity serverId;

 

       public MurmurModule(String nickname, String hostname, Integer port) {

              TcpIdentity tcpId = new TcpIdentity(hostname, port);

              this.serverId = new WhisperIdentity(nickname, tcpId);

       }

 

       @Override

       protected void configure() {

        bind(WhisperIdentity.class).toInstance(serverId);

        bind(SwissTable.class);

        bind(MurmurServer.class);

        bind(MurmurTerminal.class);

       }

}

 

So I pass in the context by values, build an instance of a non-value class, and bind to that instance within the scope of this module.  I can instantiate more than one module.  When I build an injector from each instance of the module, that injector gets appropriately contextualized.  Is the the best way to go?  Are there other good ways to go, perhaps with Guice properties or something?

 

Regarding AssistedInject, from the wiki page, how does the Date and Money get passed in?  It is the same problem, but one layer of indirection over, isn’t it?

 

Thanks,
Rob

 

 

From: [email protected] [mailto:[email protected]] On Behalf Of Olivier Grégoire
Sent: Tuesday, October 09, 2012 5:39 PM
To: [email protected]
Subject: Re: injecting contextual parameters - newbie alert

 

Hi,

 

 

Two things on that topic.

 

1. Usually, value objects shouldn't be created by Guice. The keyword "new" is still very useful for that.

2. If you still have to inject that, try using the assisted injection extension. Take a look at it here: http://code.google.com/p/google-guice/wiki/AssistedInject

 

 

Regards,

Olivier

2012/10/9 Rob Withers <[email protected]>

I am somewhat familiar with Guice and am reading the wiki, but I have a question.

 

Let's say I have a Person class, with a constructor that takes a name.  How can I use a Guice module to create 2 instance, one with "Rob" and one with "Sara"?  Are there different approaches?  What are the implications, pros and cons of each approach?

 

Thanks,
Rob

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/vNoxnL6YbtcJ.
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.



 

--
Olivier Grégoire

--
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.

--
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.

--
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.

Reply via email to