本文介绍了如何在dockable中使用osgi服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如果我有一个通过注释用作可停靠窗口的类,我该如何在该类中使用osgi服务?最好的办法是将其作为私人成员。 If I have for example a class that is used as a dockable window by annotating it, how am I supposed to use an osgi service in that class? The best would be to have it as a private member field. 推荐答案你可以举例如。使用ServiceTracker:You can eg. use a ServiceTracker:import org.osgi.framework.BundleContext;import org.osgi.framework.FrameworkUtil;import org.osgi.framework.ServiceReference;import org.osgi.util.tracker.ServiceTracker;import org.osgi.util.tracker.ServiceTrackerCustomizer;...@ViewDocking(...)public class MyView extends SomeNode{ private final ServiceTracker<MyService, MyService> myServiceTracker; private MyService myService; public MyView(){ BundleContext bundleContext = FrameworkUtil.getBundle(MyView.class).getBundleContext(); myServiceTracker = new ServiceTracker<>(bundleContext, MyService.class, new MyServiceTrackerCustomizer(bundleContext)); myServiceTracker.open(false); } ... public void setMyService(MyService myService) { if (this.myService != null){ ... } this.myService = myService; if (this.myService != null){ ... } } ... private class MyServiceTrackerCustomizer implements ServiceTrackerCustomizer<MyService, MyService> { private final BundleContext context; public MyServiceTrackerCustomizer(BundleContext context) { this.context = context; } @Override public MyService addingService(ServiceReference<MyService> reference) { MyService myService = context.getService(reference); setMyService(myService); return myService; } @Override public void modifiedService(ServiceReference<MyService> reference, MyService service) { addingService(reference); removedService(reference, service); } @Override public void removedService(ServiceReference<MyService> reference, MyService service) { setMyService(null); context.ungetService(reference); } }}还有一个开放问题是否以及如何使用CDI。There is also an open issue if and how CDI could be used. 这篇关于如何在dockable中使用osgi服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-19 13:34