Bill
Is there a way to bind from a class name (ie the class' string name) instead of a .class?
Bill
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
I tried Dhanji's idea - Class.forName returns Class<?>, which can be
cast to the interface, eg,
try {
Class<?> aClass =
Class.forName("presence.service.MemoryPresenceService");
PresenceService o = (PresenceService)aClass.newInstance();
bind(PresenceService.class).to(o.getClass());
} catch (Exception e) {
....
}
the downside isn't so much a cast as having to have '(PresenceService)'
in the code itself.
I suppose I could go OSGi, but that's a big meal to eat. I'm wondering
if scanning for module classes annotated with @Plugin or something
wouldn't be better, ie ask plugin providers to ship a guice module
instead of using text config.
Bill
http://java.sun.com/javase/6/docs/api/java/util/ServiceLoader.html
Variable o seems unnecessary, as it is only used to get the class of
MemoryPresenceService, which you already have in variable aClass.
Instantiating the class unnecessarily may lead to bugs/performance
issues. I recommend doing this instead:
try {
Class<PresenceService> aClass = (Class<PresenceService>)
Class.forName("presence.service.MemoryPresenceService");
bind(PresenceService.class).to(aClass);
} catch (Exception e) {
....
}