bndtools教程
使用工具編程的確能給人們帶來很多便利,但是在不會(huì)用之前,且缺乏相應(yīng)的中文資料讓你去了解時(shí),真是一種折磨,同時(shí)也是一種挑戰(zhàn)。
bndTools其實(shí)就是用來開發(fā)OSGi的一個(gè)工具,它為開發(fā)提供了便利,具體是哪些便利,在在這里就不細(xì)說了。
花了一個(gè)星期,終于把bndtools的程序和邏輯整理清楚,不想說太多細(xì)節(jié),先看整體框架。
bndtools主要分為三部分:
1.api,也就是提供服務(wù)的一個(gè)接口(我們可以把它理解為菜單)
2.impl,對(duì)接口的具體實(shí)現(xiàn)(可以指菜單上具體某個(gè)菜的做法,人家在后廚已經(jīng)做好了)
3.command,也就是用具體命令調(diào)用此服務(wù)(可以理解為客戶點(diǎn)菜)
了解了這三部分之后我們就可以寫程序了:
1.在eclipse上安裝bndTools
2.寫一個(gè)api程序,我寫的是com.example.api
package com.example.api;
/**
* @author Joye Luo
*/
public interface Greeting {
public String sayHello(String name);
}
在bnd.bnd文件中把它添加為導(dǎo)出包,因?yàn)榈认聦懫渌黚undle的時(shí)候會(huì)需要依賴它,這其實(shí)就是OSGi的一個(gè)模塊化機(jī)制,實(shí)現(xiàn)了低耦合。
3.寫一個(gè)實(shí)現(xiàn)程序,我寫的是com.example.impls
package com.example.impls;
import org.osgi.service.component.annotations.*;
import com.example.api.Greeting;
/**
* @author Joye Luo
*/
@Component
public class GreetingImpl implements Greeting {
@Override
public String sayHello(String name) {
return "Hello " +name;
}
}
@Component注釋可以提供注冊(cè)服務(wù)的功能,因?yàn)樵谶@里依賴了api,所以要在它的bnd.bnd文件Build Path中加上com.example.api這個(gè)包
4.寫一個(gè)command程序,讓它調(diào)用這個(gè)服務(wù),我把它放在com.example.impls下的com.example.impls.command包下,其實(shí)也可以另起一個(gè)程序,我比較懶。
import org.apache.felix.service.command.CommandProcessor;
import com.example.api.Greeting;
import aQute.bnd.annotation.component.Component;
import aQute.bnd.annotation.component.Reference;
/**
* @author Joye Luo
*/
@Component(properties={
CommandProcessor.COMMAND_SCOPE+":String=example",
CommandProcessor.COMMAND_FUNCTION+":String=greet"
},
provide=Object.class)
public class Command {
private Greeting greeting;
public void greet(String name){
System.out.println("return:"+greeting.sayHello(name));
}
@Reference
public void setUserService(Greeting greeeting){
this.greeting = greeeting;
}
}
這就會(huì)出現(xiàn)一個(gè)問題,在一個(gè)目錄結(jié)構(gòu)下,怎么讓它打包出兩個(gè)jar包出來,右鍵->new->bnd descriptor file,為這兩個(gè)包配置兩個(gè)不同的bnd文件就好啦。
4.最后一步就是運(yùn)行它啦,隨便建一個(gè)項(xiàng)目,我建的是com.example.client,new->bndrun file,把你要運(yùn)行的bundle都添加進(jìn)去,點(diǎn)Run OSGi,就可以打開Console了,輸入命令:greet joye(隨便其他什么字符串都可以)
可以看到輸出為Hello joye
所有功能都完成啦,是不是很簡(jiǎn)答呢,接下來,show u the code
https://github.com/JoyeLuo/osgi-example
總結(jié)
以上是生活随笔為你收集整理的bndtools教程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GPU Gems1 - 14 透视阴影贴
- 下一篇: GPU Gems1 - 15 逐像素光照