?? osgi框架下servicefactory的實現.txt
字號:
OSGi框架下ServiceFactory的實現2007-05-15 09:10以前寫的程序直接就是通過向框架注冊服務實現,今天心血來潮,想用OSGi提供的ServiceFactory來改寫一下。剛開始肯定是困難重重,因為在網上幾乎沒搜到相關的文章,并且規范也說的比較模糊。不過在李銘哥的幫助下,還是很快完成了helloworld的改寫。現在把改寫的部分貼出來,供大家學習。
1.在實現的類中需要實現ServiceFactory接口
public class subhelloprint implements helloprint,ServiceFactory
{
public void welcome(String s)
{
System.out.println("welcome,"+s+"!");
}
public helloprint getService(Bundle bundle, ServiceRegistration reg) {
return new subhelloprint();
}//可以根據不同的Bundle返回不同的服務
public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) {
}
}
2.在服務注冊的激活器中將start改成,
public void start(BundleContext context) throws Exception {
reg = context.registerService(helloprint.class.getName(), new subhelloprint(), null);
}
3.引用的過程與Servic注冊實現的一樣
總結:就像規范里邊提到的那樣,讓framework為每個想使用該服務的bundle獲得獨享的服務對象。在返回的時候可以根據不同的calling bundle返回不同的service,達到定制服務的目的。至于進一步的應用還是得需要繼續學習。
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -