Yes, Thomas. You can bind Engine even if you don't control its constructor
source code.
The simplest way to do that is to, in a Module, write a provides method.
@Provides Engine createEngine(... any dependencies) {
return new Engine(... any parameters from dependencies);
}
For instance,
@Provides Engine createEngine(SparkPlug plug) {
return new Engine(plug);
}
Or if it depends on other things you that aren't injectable, you can either
write @Provider methods for them or (if they are only needed in Engine)
simply new them in the @Provides method.
Christian.
On Friday, November 16, 2012 4:48:32 PM UTC-5, Thomas wrote:
>
> Hello,
>
> I'm new to Guice, and I have a constructor that I want to inject. However,
> one of the parameters for this constructor is from a class that is not in
> my control. Can I still use Guice to inject this constructor?
>
> Here is an example of what I'm talking about. In this case the Engine
> class is not in my control, but the parameters for the Engine object are in
> my control. If the Engine class was in my control I would just add @Inject
> to the constructor for the Engine class, but in my case I can't do that.
>
> public class Car {
>
> private Engine engine;
> private List<Wheel> wheels;
>
> @Inject
> public Car(Engine engine, List<Wheel> wheels) {
> this.engine = engine;
> this.wheels = wheels;
> }
> }
>
> -Thomas
>
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/google-guice/-/ZWKiaJObmVQJ.
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?hl=en.