If you need to do more complex configuration, you could use a child
injector approach (or even just two completely separate injectors if you
won't need the bindings from the injector used for the settings service
again after creating the second injector). What are the problems you've run
in to with that approach?

-- 
Colin


On Thu, Feb 23, 2012 at 3:37 PM, Warren Bell <[email protected]> wrote:

> It's not exactly what I was trying to achieve, but it did give me an idea.
>
> Can I do something like this with a single module:
>
> class MyModule extends AbstractModule
> {
>
>  public MyModule()
>  {
>    appSet = AppSettingsService.getInstance();
>  }
>
>  final AppSettingsService appSet;
>
>  public void configure()
>  {
>    // use appSet to configure this module
>  }
>
>  @Provides
>  @Singleton
>  AppSettingsService provideAppSettingsService()
>  {
>    return appSet;
>  }
>
> }
>
> I need AppSettingsService to configure this module and I also need to
> bind it for the rest of the app as a Singleton. Do you see any issues
> with this code or a better way ?
>
> Thanks,
>
> Warren Bell
>
> On 2/23/12 11:44 AM, Noctarius wrote:
> > Hi Warren,
> >
> > what I mean is something like the following (just written down,
> > could be made nice by using constants etc).
> >
> > interface Foo {
> >
> > }
> >
> > class FooImplStandard implements Foo {
> >
> > }
> >
> > class FooImplSpecial implements Foo {
> >
> > }
> >
> > class FooProvider implements Provider<Foo> {
> >
> >   @Inject @Named("Foo")
> >
> >   private String implementation;
> >
> >   @Inject
> >
> >   private Injector injector;
> >
> >
> >
> >   public Foo get() {
> >
> >     if ("Standard".equals(implementation))
> >
> >       return injector.getInstance(FooImplStandard.class);
> >
> >     else
> >
> >       return injector.getInstance(FooImplSpecial.class);
> >
> >   }
> >
> > }
> >
> > class FirstModule extends AbstractModule {
> >
> >   public void configure() {
> >
> >     bindConstant().annotatedWith(Names.named("Foo")).to("Standard");
> >
> >   }
> >
> > }
> >
> > class SecondModule extends AbstractModule {
> >
> >   public void configure() {
> >
> >     bind(Foo.class).toProvider(FooProvider.clas).in(Singleton.class);
> >
> >   }
> >
> > }
> >
> >
> > Am 23.02.2012 20:36, schrieb Warren Bell:
> >> Can you elaborate a little ?
> >>
> >> Thanks,
> >>
> >> Warren Bell
> >>
> >> On 2/23/12 10:03 AM, Noctarius wrote:
> >>> Why not use a Provider implementation with the injected depedency?
> >>>
> >>> Am 23.02.2012 18:30, schrieb Warren:
> >>>> I have 2 modules, module1 and module2. I bind a type1 in module1 and I
> >>>> then need to inject type1 into module2. I have tried the
> >>>> createChildInjector, but have run into problems doing it that way.
> >>>> What is the best way to do this?
> >>>>
> >>>> Thanks
> >>>>
> >>>> Warren Bell
> >>>>
> >
>
> --
> 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