Eclispe 插件工程中提供了Dictionary OSGi Service 模板,寫了一下Custom端使用的代碼。
1. Dictionary OSGi Service 模板
按照Eclipse向導生成。注意Export相應包。
2. Custom 端使用
新建插件工程,Import字典服務Exprot的包。
代碼片段:
3. Custom 端使用
如果沒輸出顯示,可在console中 update custom 插件。
結果:

1. Dictionary OSGi Service 模板
按照Eclipse向導生成。注意Export相應包。
2. Custom 端使用
新建插件工程,Import字典服務Exprot的包。
代碼片段:
package demo.dictionary.custom;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import demo.dictionary.DictionaryService;
public class Activator implements BundleActivator {
ServiceReference serviceReference = null;
public void start(BundleContext context) throws Exception {
serviceReference = context.getServiceReference(DictionaryService.class.getName());
if(null != serviceReference){
DictionaryService dictionaryService = (DictionaryService)context.getService(serviceReference);
System.out.println("check word:" + dictionaryService.check("word"));
System.out.println("check osgi:" + dictionaryService.check("osgi"));
System.out.println("check equinox:" + dictionaryService.check("equinox"));
System.out.println("check eclipse:" + dictionaryService.check("eclipse"));
}
}
public void stop(BundleContext context) throws Exception {
serviceReference = null;
}
}
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import demo.dictionary.DictionaryService;
public class Activator implements BundleActivator {
ServiceReference serviceReference = null;
public void start(BundleContext context) throws Exception {
serviceReference = context.getServiceReference(DictionaryService.class.getName());
if(null != serviceReference){
DictionaryService dictionaryService = (DictionaryService)context.getService(serviceReference);
System.out.println("check word:" + dictionaryService.check("word"));
System.out.println("check osgi:" + dictionaryService.check("osgi"));
System.out.println("check equinox:" + dictionaryService.check("equinox"));
System.out.println("check eclipse:" + dictionaryService.check("eclipse"));
}
}
public void stop(BundleContext context) throws Exception {
serviceReference = null;
}
}
3. Custom 端使用
如果沒輸出顯示,可在console中 update custom 插件。
結果:

4.工程文件下載
demo.dictionary.custom.rar