Status: New
Owner: ----

New issue 655 by [email protected]: Provider Factory and then Inject what the Provider returns
http://code.google.com/p/google-guice/issues/detail?id=655

I think this is partly related to Issue #131, FactoryModuleBuilder & Co but with a slightly different twist. I'm not interested in the Factories themselves (which seems to be the purpose of FactoryModuleBuilder and FactoryProvider) but would rather tell Guice how to create Objects for me with some arguments that I provide and with some arguments that Guice can inject for me.

Here is an example where I provide the type of my POJO, Guice injects some additional stuff to my factory method and I do something with it (e.g. use the arguments to read some resource such as JSON/XML and create a POJO from that).

public class ConfigModule extends AbstractModule {
  @Override
  protected void configure() {
bind(PicasaConfig.class).toFactory(new ConfigFactory<PicasaConfig>(PicasaConfig.class)); bind(FlickrConfig.class).toFactory(new ConfigFactory<FlickrConfig>(FlickrConfig.class));
  }
}

public class ConfigFactory<T> {

  private final Class<T> type;

  public ConfigFactory(Class<T> type) {
    this.type = type;
  }

  @Provides @Inject
public T create(@Named("mv.config.cl") ClassLoader loader, @Named("my.env") String environment) {
    return DoSomeMagic.newInstance(loader, environment, type);
  }
}

@Singleton
public class PicasaProcessor {

  private final PicasaConfig config;

  @Inject
  public PicasaProcessor(PicasaConfig config) {
    this.config = config;
  }
}

--
You received this message because you are subscribed to the Google Groups 
"google-guice-dev" 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-dev?hl=en.

Reply via email to