Hello OSGi – starts with FelixHello world 系列的OSGi部分。
本文分为三部分,第一部分(I)给出了一个最简单的bundle示例;第二部分(II)用三个bundle分别作为接口、实现和消费三种角色,来示例一下Declarative Services;第三部分(III)给出了本文的一些参考资料(链接)。之前看过别人写的hello,觉得太hello了,一般只到本文的第一部分就结束了,为了让兄弟们在hello阶段了解的更多一点,这里特地加了第二部分。
注意本文为了简单不涉及unit test。
I
先整个maven工程出来。
执行命令 mvn archetype:generate -DgroupId=osteching.osgi.steps.hello -DartifactId=osgi.steps.hello 。
“choose a number”的时候就用默认的15,要最简单的工程。version随便整,喜欢什么都成,我这里用0.0.1。可以输入想要的package,我这里就用osteching.osgi.steps.hello了。OK,maven工程搞定,再顺手生成Eclipse工程吧,这年头,有IDE不用用Emacs/Vi的人纯粹没事找事。先进入刚才生成的maven工程目录(这里是osgi.steps.hello),执行命令 mvn eclipse:eclipse 。在Eclipse中用import,导入工程。
修改pom,要用Felix嘛,添加Felix的dependency:
<dependency> <groupId>org.apache.felix</groupId> <artifactId>org.osgi.core</artifactId> <version>1.2.0</version> </dependency>
这部分我们只要个最简单的bundle,所以其实直接jar 就行了。但是为了说明Felix确实加载了这个bundle,我们写一个Activator,这个Activator在Felix加载它的时候,打印Hello world!出来。下面是代码示例:
public class Activator implements BundleActivator { @Override public void start(BundleContext context) throws Exception { System.out.println(“->– bundle starts —”); [...]
Recent Comments
- Ken on SLF4J MDC and Marker
- Ken on SLF4J MDC and Marker
Archives
- February 2012 (1)
- December 2011 (1)
- August 2011 (1)
- April 2011 (1)
- March 2011 (1)
- January 2011 (14)


