无法注入EPartService

问题描述 投票:0回答:1

在我的bundle激活器中,我尝试注入字段'IEventBroker'和'EPartService'。但只注入第一个。代码如下:

@Inject
IEventBroker m_broker;
@Inject
EPartService m_part_service;

public void start(BundleContext context) throws Exception {

    IEclipseContext service_context = EclipseContextFactory.getServiceContext(context);
    ContextInjectionFactory.inject(this, service_context);
    boolean contains = service_context.containsKey(EPartService.class);

    // contains is always "true", but m_part_service is always "null"
    // all follows invocations returns "null" too
    //
    // service_context.get(EPartService.class); 
    // service_context.getActiveLeaf().getActive(EPartService.class);   
    // service_context.getActiveLeaf().getLocal(EPartService.class);    
    // context.getServiceReference(EPartService.class); 

    // m_broker always non-null
    m_broker.subscribe(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE, new EventHandler()
        {
            @Override
            public void handleEvent(Event event)
            {
                // ... bla bla bla
            }
        });
}

在IEclipseContext的内部列表中,我找到了EPartService。你能帮助我吗?我做错了什么?

eclipse e4
1个回答
0
投票

没有注射捆绑活化剂,所以你不能使用@Inject

EclipseContextFactory.getServiceContext返回的上下文内容非常有限,不能用于访问像EPartService这样的内容。

在任何情况下,捆绑激活器通常都不会运行,直到你的插件中的其他东西被使用,所以无论如何看起来启动完成消息为时已晚。

所以这一切都意味着你不能在束激活器start方法中做你想做的事。

要获得有关应用程序启动完成事件的通知,您可以使用应用程序LifeCycle类或定义AddOn - 这两个类都被注入。

在这些类中使用如下方法:

@Optional
@Inject
public void appStartupComplete(@UIEventTopic(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE)
                               org.osgi.service.event.Event event)
© www.soinside.com 2019 - 2024. All rights reserved.