首页 > 解决方案 > E4 无法在类变量上自动注入 MApplication

问题描述

我有一个 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 类中解决这个问题?我需要那里所有注入的类,但找不到将它带到那里的方法。

标签: javaeclipse-rcpe4

解决方案


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

根据您想要使用该类的确切方式,您可以:

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

推荐阅读