Re: Possible to use Guice when constructor takes third-party objects as parameters?

273 views
Skip to first unread message

Christian Gruber

unread,
Nov 16, 2012, 5:00:55 PM11/16/12
to google...@googlegroups.com
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

Jeff

unread,
Nov 16, 2012, 5:04:39 PM11/16/12
to google...@googlegroups.com
I'll take a stab at this (though I'm a Guice n00b too! :-)...yes you can.  I've done this using a provider in a custom module such as:

  @Provides
  protected Engine myEngineProvider(MyPart part1, MyPart part2, ...) {
    return new Engine(part1, part2, ...);
  }

where Guice injects needed parts into the @Provider method then you use them to call the Engine() constructor directly.  

If one of the parts is similarly not controlled by you, you can create providers for them as well in a similar fashion.


On Fri, Nov 16, 2012 at 2:48 PM, Thomas <thomas...@gmail.com> 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/-/ONyOZzo67sMJ.
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 ♥ DropBox !! 

Reply all
Reply to author
Forward
0 new messages