简体   繁体   中英

DI and inheritance

Another question appeared during my migration from an E3 application to a pure E4.

I got a Structure using inheritance as in the following pic. 在此处输入图片说明

There I have an invocation sequence going from the AbstractRootEditor to the FormRootEditor to the SashCompositeSubView to the TableSubView. There I want to use my EMenuService, but it is null due to it can´t be injected.

The AbstractRootEditor is the only class connected to the Application Model (as a MPart created out of an MPartDescriptor).

I´d like to inject the EMenuService anyway in the AbstractSubView, otherwise I would´ve the need to carry the Service through all of my classes. But I don´t have an IEclipseContext there, due to my AbstractSubView is not connected with Application Model (Do I ?).

I there any chance to get the service injected in the AvstractSubView?

EDIT: I noticed that injecting this in my AbstractSubView isn´t possible (?), so I´m trying to get it into my TableSubView.

After gregs comment i want to show some code: in the AbstractRootEditor:

    @PostConstruct
    public final void createPartControl(Composite parent, @Active MPart mPart) {
...
ContextInjectionFactory.make(TableSubView.class, mPart.getContext());

First I got an Exception, saying that my TableSubView.class got an invalid constructor, so now the Constructor there is:

public TableSubView() {
        this.tableInputController=null;
    }

as well as my Field-Injection:

@Inject EMenuService eMenuService

This is kind of not working, eMenuService is still null

If you create your objects using ContextInjectionFactory they will be injected. Use:

MyClass myClass = ContextInjectionFactory.make(MyClass.class, context);

where context is an IEclipseContext (so you have to do this for every class starting from one that is injected by Eclipse).

There is also a seconds version of ContextInjectionFactory.make which lets you provide two contexts the second one being a temporary context which can contain additional values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM