gpt4 book ai didi

java - E4 无法在类变量上自动注入(inject) MApplication

转载 作者:行者123 更新时间:2023-11-30 01:49:53 26 4
gpt4 key购买 nike

我有一个 E4 应用程序,我需要将 MApplication 注入(inject)到第一个创建的类中。该类本身已在我的 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 类中修复此问题?我需要那里所有注入(inject)的类,但找不到方法将其到达那里。

最佳答案

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

具体取决于您想要如何使用该类,您可以:

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

关于java - E4 无法在类变量上自动注入(inject) MApplication,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56455896/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com