you can achieve that with a Multibindings [1] and MapBinder [2]

In your module:

public void configure() {
   MapBinder<String, Service> mapbinder = MapBinder.newMapBinder(binder(),
String.class, Service.class);
   
mapbinder.addBinding(SitemapListService.class.getName()).to(SitemapListService.class)
// if you do it this way SitemapListService wil be also injected
  mapbinder.addBinding(SitemapService.class.getName()).to(SitemapService.
class)
 // and so on
}

Now in your client code you can inject the map of services:

@Inject
Foo(Map<String, Service> services>) {
  this.services = services;
}

hth,
jordi

[1] https://code.google.com/p/google-guice/wiki/Multibindings
[2]
http://google-guice.googlecode.com/svn/trunk/latest-javadoc/com/google/inject/multibindings/MapBinder.html


On Thu, Aug 29, 2013 at 9:27 AM, <[email protected]> wrote:

> Hi,
>
> I've been reading quite a lot of documentation around named bindings,
> factorymodulebuilder, mapped bindings and it is still not clear if I can
> achieve something along the following lines.
>
> For example I want a Factory to be able to look up different services at
> runtime based a key. In spring you can do something like this
>
>
> public class ServiceFactory {
>
>       private Map<String, Service> services = new HashMap<String, Service>();
>       
>       @Autowired
>       private Map<String, Service> mappedServices;
>       
>       public void init() {
>               services.put(SitemapListService.class.getName(), new 
> SitemapListService());
>               services.put(SitemapService.class.getName(), new 
> SitemapService());
>               services.put(SitemapExcelService.class.getName(), new 
> SitemapExcelService());
>               services.put(CategoryService.class.getName(), new 
> CategoryService());
>               services.put(AdminDirectoryListService.class.getName(), new 
> AdminDirectoryListService());
>               services.put(CompositeConceptsService.class.getName(), new 
> CompositeConceptsService());
>       }
>       
>       public Service getService(String key) {
>               Service service = services.get(key);
>               if (service == null) {
>                       throw new IllegalArgumentException("Unknown service 
> type: " + key);
>               }
>               return service; 
>       }
> }
>
>
> Then for example the service name could be configured in a configuration file 
> or calculated at runtime? How can I achieve something similar with Guice?
>
>
> Thanks
>
> Jon
>
>  --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/google-guice.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.
  • Factories jpcook04
    • Re: Factories Jordi Gerona

Reply via email to