1

I am trying to do the following. If possible I want to avoid creating them using the 'new' keyword, and rely on Dependency Injection. Is this possible given the following scenario?

public class One {

    @Inject
    private Two two;

    @PostConstruct
    public void createComposite(final Composite parent) {
        final Composite container = doSomethingWithParent(parent);

        // How do I call Twos constructor using injection and pass it the value for container?
        // new Two(context, container);
    }

}

@Creatable
@Singleton
public class Two
    @Inject
    public Two(final IEclipseContext context, final Composite containerFromOne) {
        context.set(Two.class.getName(), this);
        doSomethingImportantThatNeedsComposite(containerFromOne);
    }
}

public class Three
    @Inject
    private Two two;

    // I would like be able to use two anywhere in this class 
    // once two is instantiated by One    
}

I was also looking at the ContextInjectionFactory.make() but I don't see a way to pass in constructor arguments.

ekjcfn3902039
  • 1,673
  • 3
  • 29
  • 54

1 Answers1

1

You can use ContextInjectFactory.make using the variant with two IEclipseContext parameters. The second IEclipseContext is a temporary context where you can put the extra parameters. Something like:

IEclipseContext tempContext = EclipseContextFactory.create();

tempContext.set(Composite.class, composite);

// ... more parameters

Two two = ContextInjectionFactory.make(Two.class, context, tempContext);

Note: You don't use the @Creatable and @Singleton annotations with this.

Once you have created it you can put it in to the Eclipse Context with:

context.set(Two.class, two);

Note that there can be several Eclipse Contexts. You may need to find the correct one.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • Unfortunately that doesn't work. Three can't pick it up during application start and barfs with a InjectionException error because it hasn't been created yet. If I sit on the ContextInjectionFactory.make with a debugger I see the error come up well before the debug point hits – ekjcfn3902039 Dec 04 '15 at 14:46
  • Added code to set the value in the Eclipse Context. – greg-449 Dec 04 '15 at 14:53
  • I still get the same results. The debugger shows that that the context.set inside of class One hits well after the error comes up. Is One the correct place to put that code? – ekjcfn3902039 Dec 04 '15 at 14:57
  • Since I don't know how you are using class Three I can't say. If you are trying to use Three before explicitly creating Two where is the Composite parameter for the Two constructor supposed to come from? – greg-449 Dec 04 '15 at 15:08
  • I have multiple versions of class 'Three'. I guess that kinda adds another question to the first one about how to ensure that class One loads before any other classes that use class Two. Is that possible? – ekjcfn3902039 Dec 04 '15 at 15:13
  • Regarding your question, One creates a composite that Two needs in its constructor – ekjcfn3902039 Dec 04 '15 at 15:19
  • You cannot create any instance of Three until Two has been constructed and put in the context. – greg-449 Dec 04 '15 at 15:48
  • Correct, I think we are saying the same thing. So is there a way to programmatically ensure no classes can inject Two until it is created in One? – ekjcfn3902039 Dec 04 '15 at 16:52
  • No, that is up to you to arrange your code so you don't create anything that uses Two until it has been created. – greg-449 Dec 04 '15 at 17:46
  • So to clarify, it sounds like injection is not the way to go in this case. Thanks! – ekjcfn3902039 Dec 04 '15 at 19:01