So something like this:

Injector injector = Guice.createInjector(new AbstractModule()
{

  @Override
  protected void configure()
  {
bind(AppSettingsService.class).toProvider(AppSettingsServiceProvider.class).in(Singleton.class);
  }

}, new ExternalDataSourcesModule());

public class ExternalDataSourcesModule
{

  @Inject
  private AppSettingsService appSettingsService;

  @Override
  protected void configure()
  {
    // use appSettingsService to configure rest of this module
  }

}

Will AppSettingsService get injected into ExternalDataSourcesModule ?

Thanks,

Warren Bell

On 3/13/12 2:40 PM, Noctarius wrote:
> Hi Warren,
> 
> I would use a Provider and inject depending information to that
> provider.
> 
> Cheers Noc
> 
> Am 13.03.2012 22:27, schrieb Warren:
>> How do I change the following code so that both modules are configured
>> in one injector instead of two?
>>
>> Injector parentInjector = Guice.createInjector(new AbstractModule()
>> {
>>
>>      @Override
>>      protected void configure()
>>      {
>>              bind(AppSettingsService.class).in(Singleton.class);
>>      }
>>
>> });
>> AppSettingsService appSettingsService =
>> parentInjector.getInstance(AppSettingsService.class);
>> Injector childInjector = parentInjector.createChildInjector(new
>> ExternalDataSourcesModule(appSettingsService));
>>
>>
>> public class ExternalDataSourcesModule
>> {
>>
>>      @Override
>>      protected void configure()
>>      {
>>              // use AppSettingsService to configure rest of this module
>>      }
>>
>> }
>>
>> AppSettingsService needs to be bound and needs to be used in
>> ExternalDataSourcesModule. I have tried @Provides in
>> ExternalDataSourcesModule and have gotten it to work, but it doesn't
>> feel right having to set-up AppSettingsService as a singleton with
>> getInstance(), plus I would like to inject dependencies into it.
>>
>> public class ExternalDataSourcesModule
>> {
>>
>>      @Provides
>>      @Singleton
>>      AppSettingsService provideAppSettingsService()
>>      {
>>              return AppSettingsService.getInstance();
>>      }
>>
>>
>>      @Override
>>      protected void configure()
>>      {
>>              // use AppSettingsService to configure rest of this module
>>                AppSettingsService  appSettingsService =
>> AppSettingsService.getInstance()
>>      }
>>
>> }
>>
>>
>>
>>
>> I would like to do this:
>>
>> Injector injector = Guice.createInjector(new AbstractModule()
>> {
>>
>>      @Override
>>      protected void configure()
>>      {
>>              bind(AppSettingsService.class).in(Singleton.class);
>>      }
>>
>> }, new ExternalDataSourcesModule());
>>
>> public class ExternalDataSourcesModule
>> {
>>
>>         @Inject
>>         private AppSettingsService appSettingsService;
>>
>>      @Override
>>      protected void configure()
>>      {
>>              // use appSettingsService to configure rest of this module
>>      }
>>
>> }
>>
>> I think this is wanting the chicken before the egg. Using a child
>> injector caused other problems, see my previous post.
>>
>> Any help would be appreciated.
>>
>> Thanks,
>>
>> Warren
>>
> 

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