Thanks for @greg-449 for the directions.
It turns out the injection system is not even initialized until PlatformUI.createAndRunWorkbench
is ran.
I have found however that my TreeModelView class is created with working DI. Which is apparently because it is mentioned in plugins.xml It is still not clear whether I have (or need) a central point where I can register my own beans, but I could do so here with the ones needed there.
First of all, to inject the OSGI service inez
I did not have to do anything beyond using @Inject
in that class. I have even deleted basically all of the start()
function of my bundle activator which was previously obtained the service.
I could also @Inject
IEclipseContext
, and use that tho create my own bean. I did that with my ModelTreeContentProvider
class, which in turn again uses inez
using injection:
public class TreeModelView extends ViewPart {
public static final String ID = "io.github.magwas.inez.ui.treeModelView";
@Inject
IWorkbench workbench;
@Inject
Inez inez;
@Inject
IEclipseContext eclipseContext;
private TreeViewer viewer;
@Override
public void createPartControl(Composite parent) {
Assert.notNull(inez, "inez is null");
ModelTreeContentProvider provider = ContextInjectionFactory
.make(ModelTreeContentProvider.class, eclipseContext);
//normal eclipse viewer stuff from here, deleted for brewity
}
@Override
public void setFocus() {
viewer.getControl().setFocus();
}
}
public class ModelTreeContentProvider implements ITreeContentProvider {
@Inject
Inez inez;
@Override
public Object[] getElements(Object inputElement) {
System.out.println("getElements " + inputElement);
return inez.root().getChildren().toArray();
}
//[...]
}
I also tried to just add a @Singleton
annotation to my ModelTreeContentProvider
class and inject it to my TreeModelView
, but that did not work. Apaprently I have to register my beans using ContextInjectionFactory
by hand. So until I find some central space where I can make all my beans early, I just use some of my views and editors for that, and factor out their business logic to some services.