i am trying to dynamically bind things in guice. for one type known
beforehand, i would directly:
bind(new TypeLiteral<Service<Car>>() {}).toInstance(new
ServiceImpl<Car>("myapp.domain.Car"));
but now the idea is, given a list of classes in runtime, bind them to
a particular instance... something like:
// along with some reflection or guicy magic
for (Class<?> clazz : getClasses("myapp.domain")) {
TypeLiteral typeLiteral = new TypeLiteral<Service<Car>>() {};
ServiceImpl service = new ServiceImpl(clazz.getName()); // add types
(...)
bind(typeLiteral).toInstance(service);
}
in order to do the following in client code:
@Inject
Service<SomeDomainClass> service;
does guice provide an equivalent way to accomplish this? i'm also
asking here because lots of features have been added after 1.0. other
ideas on how would you do this? maybe dead simple with standard
reflection?
thanks!
francisco
how can i bind a generic type to a specific implementation, if i the
only information i have is a Class clazz?
bind(new TypeLiteral<Service< (put type here from clazz) >>()
{}).toInstance(someInstance); // you see here what i'd like to do
as i don't think this is remotely possible, i modified by reflection
the TypeLiteral's "type" actualTypeArguments. when i debug my
injector, i see the Key of the my binding is correctly set to my
clazz. but when i actually @Inject, guice says it can't find the
binding to that type. apart from the actualTypeArguments, do i need to
change something else? or there's no other way than explicitly declare
the binding like so: bind(new TypeLiteral<Service<Car>>() {})... ?
sorry if it was a noob question on generics basics. i'd be glad to get
any additional pointers if you have.
francisco
i finally found a better way to accomplish this without guice.
as i'm also using salve (http://code.google.com/p/salve), i created a
CustomLocator where i can manage salve.Keys and return appropriate
bindings as i want, even before guice is aware of all that. basically
i had to implement the Locator interface:
public Object locate(Key key) {
if (key.getType() == Repository.class) {
Type type = key.getGenericType();
// get the "actualTypeArgument" Class clazz
return new RepositoryImpl(clazz);
}
return null;
}
...and declare GuiceLocator after my own one.
francisco
my application is "profiled" for different environment, it means that
a specific interface, in stage environment, is implemented by a
specific class, that is really different from the production
implementation; so, using the apache's commons-discovery
http://commons.apache.org/discovery/
my binding module implementation is
public void configure(Binder binder) {
...
DiscoverClass discover = new DiscoverClass();
binder.bind(MyInterface.class).to(discover.find(MyInterface.class));
...
}
It's currently working in a production environment without any problem.
Best regards,
Simone
2008/8/21 francisco treacy <francisc...@gmail.com>:
--
My LinkedIn profile: http://www.linkedin.com/in/simonetripodi
My GoogleCode profile: http://code.google.com/u/simone.tripodi/
My Picasa: http://picasaweb.google.com/simone.tripodi/
My Tube: http://www.youtube.com/user/stripodi
My Del.icio.us: http://del.icio.us/simone.tripodi