Typically Guice needs to create objects to inject them. If you just call new StarryEyes(name)
, Guice isn't ever going to see that object so it won't be able to inject it. One thing you can do is to call injector.injectMembers(obj)
on the object after you've created it. I wouldn't recommend that, though, as you should avoid referencing the injector in your code.
What you really probably want here is Assisted Inject. With Assisted Inject, you'd declare the constructor for your class something like this:
@Inject public StarryEyes(MyValidator validator, @Assisted String name)
What that means is that validator
is a parameter that Guice should inject, while name
must be "assisted" (that is, provided at the time the instance is created).
You then create an interface like this:
public interface StarryEyesFactory {
StarryEyes create(String name);
}
With Assisted Inject, Guice can then implement that factory for you. You bind it like this:
bind(StarryEyesFactory.class).toProvider(
FactoryProvider.newFactory(StarryEyesFactory.class, StarryEyes.class));
You then inject a StarryEyesFactory
anywhere you want to create an instance of it. Where you would have called new StarryEyes(name)
previously, you now call starryEyesFactory.create(name)
instead. When you call create(name)
on the factory, it will take the name and pass it to the constructor and provide the bound validator itself.