java - 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 类中解决这个问题?我需要那里所有注入的类,但找不到将它带到那里的方法。
解决方案
only返回的服务上下文EclipseContextFactory.getServiceContext
内容非常有限,不包括MApplication
. 您不能在激活器中创建这样的类。
根据您想要使用该类的确切方式,您可以:
- 在 application.e4xmi 或 fragment.e4xmi 中指定的“附加组件”中创建类
- 只需让它使用
@Creatable
类上的注释自动创建即可。@Singleton
如果您想要该类的单个实例,也可以使用注释。 - 在 LifeCycle 类中创建它
- 使用
ContextFunction
推荐阅读
- android-notifications - Android 应用程序是否可以访问由语音命令或日历设置的提醒
- javascript - 如果用户决定返回,则在 ajax 请求后检索 Post 数据
- python - python中的“{:<20}”format_string是什么意思?
- javascript - @click 在索引页面上的 Vue 项目中
- python - python排列有问题
- visual-studio - 将 2 个数据库与 .NetCore 2 同步的最佳解决方案
- javascript - 如何修复无法读取 null 的属性“forEach”
- c - 如何从 main 访问线程中完成的计算?
- php - 按日期排序数组(需要分解)
- java - 如何在 Dropwizard 中为 Jdbi 使用 UserResource