我有一个E4应用程序,需要将MApplication注入第一个创建的类。该类本身已在我的Activator的start方法中创建,并在其中调用。

我的课称为FrameworkModule,如下所示:

public class FrameworkModule implements ISubscription {

    private static final Logger logger = LoggerFactory.getLogger(FrameworkModule.class);

    private @Inject IEclipseContext context;
    protected @Inject EPartService partService;

    protected @Inject MApplication application;

    protected @Inject EModelService modelService;
...
}


激活器将创建上述类并运行其方法。激活器的启动方法如下所示:

@Override
    public void start(BundleContext bundleContext) throws Exception {
        Activator.context = bundleContext;

        IEclipseContext eContext = EclipseContextFactory.getServiceContext(bundleContext);

        ExecutorService service = Executors.newSingleThreadExecutor();

        service.execute(() -> {
            framework = ContextInjectionFactory.make(FrameworkModule.class, eContext);
            framework.startup();
        });

        service.execute(() -> CacheUtil.getManager());

        service.shutdown();
    }


代码启动时出现以下错误:

 org.eclipse.e4.core.di.InjectionException: Unable to process "FrameworkModule.application": no actual value was found for the argument "MApplication".
    at org.eclipse.e4.core.internal.di.InjectorImpl.reportUnresolvedArgument(InjectorImpl.java:488)
    at org.eclipse.e4.core.internal.di.InjectorImpl.resolveRequestorArgs(InjectorImpl.java:479)
    at org.eclipse.e4.core.internal.di.InjectorImpl.internalInject(InjectorImpl.java:128)
    at org.eclipse.e4.core.internal.di.InjectorImpl.internalMake(InjectorImpl.java:411)
    at org.eclipse.e4.core.internal.di.InjectorImpl.make(InjectorImpl.java:333)
    at org.eclipse.e4.core.contexts.ContextInjectionFactory.make(ContextInjectionFactory.java:202)
    at com.xxx.client.eclipse.Activator.lambda$0(Activator.java:84)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)


如何在FrameworkModule类中解决此问题?我在那里需要所有注入的类,却找不到找到它的方法。

最佳答案

EclipseContextFactory.getServiceContext返回的服务上下文的内容非常有限,并且不包含MApplication。您不能在激活器中像这样创建您的类。

根据您要如何使用该类,您可以:


在应用程序中指定的“附加组件”中创建类。e4xmi或fragment.e4xmi
只需使用类上的@Creatable批注自动创建即可。如果需要该类的单个实例,也可以使用@Singleton批注。
在LifeCycle类中创建它
使用ContextFunction创建它

10-02 03:06
查看更多