Note that there are some explanatory texts on larger screens.

plurals
  1. POGuice Module bind Object constructed with Injected Class
    primarykey
    data
    text
    <p>I'm new to Guice, so excuse me if this is a dumb question, but I'm finding it quite frustrating.</p> <p>In my module I construct an object which is a member of a framework that I do not control. I then bind that instance so that I can access it later with injector.getInstance(FrameworkClass.class); </p> <p>When I construct that object, I want to use one of my own objects which itself is annotated to be injected by constructor.</p> <p>i.e.</p> <pre><code>public class MyClass { OtherClass dependency; @Inject Public MyClass(OtherClass dependency) { .... } } public class AModule extends AbstractModule { protected void configure() { MyClass frameworkDependency = ??? FrameworkClass fc = new FrameworkClass(frameworkDependency); bind(FrameworkClass.class).toInstance(fc); } } </code></pre> <p>In this example OtherClass is just a simple class with no annotations but a default empty constructor, so Guice should just new one up and provide it to MyClass constructor as far as I understand.</p> <p>My question is How do I get access to a new instance of MyClass in my module so that I can provide it to the framework class?</p> <p>Previously I had my own class injected via Field Injection, so it looked like this:</p> <pre><code>public class MyClass { @Inject OtherClass dependency; } public class AModule extends AbstractModule { protected void configure() { MyClass frameworkDependency = new MyClass(); requestInjection(frameworkDependency); FrameworkClass fc = new FrameworkClass(frameworkDependency); bind(FrameworkClass.class).toInstance(fc); } } </code></pre> <p>I would prefer to keep MyClass as constructor injected if possible. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload