Intellij Idea 9是個(gè)原生支持OSGI的IDE, 不需要在額外安裝插件.
最簡(jiǎn)約的步驟:(以Apache Felix為例)
1. 下載Felix 發(fā)布包
http://felix.apache.org/site/downloads.cgi 中 下載 Felix Framework Distribution
3.0.x ,解壓到一個(gè)目錄中
2. 在Intellij 中添加 Felix目錄
Settings --> OSGI --> IDE Settings --> Framework Defininations: 選擇 類(lèi)型為 Felix, 然后瀏覽選中Felix 的解壓目錄, 就添加成功了.
3. 創(chuàng)建一個(gè)Project, 當(dāng)然也創(chuàng)建一個(gè)Module
在module屬性右鍵添加 OSGI支持
在module依賴(lài)設(shè)置中添加 Osmorc自動(dòng)生成的Felix庫(kù) 作為本模塊的依賴(lài).
4. 創(chuàng)建一個(gè)類(lèi)
1 package com.selftest.osgi;
2
3 import org.osgi.framework.BundleActivator;
4 import org.osgi.framework.BundleContext;
5
6 public class HelloWorldActivator implements BundleActivator
7 {
8 public void start(BundleContext bundleContext) throws Exception
9 {
10 System.out.println("Hello World Bundle started!");
11 }
12
13 public void stop(BundleContext bundleContext) throws Exception
14 {
15 System.out.println("Hello World Bundle stop!");
16 }
17 }
18
4. 在module 的屬性O(shè)SGI中, 設(shè)置 Bundle Activator為
HelloWorldActivator , 名字自己隨意填寫(xiě)一個(gè).

5. 新建一個(gè) Configuration來(lái)測(cè)試 OSGI Bundle
- "ADD" 剛剛創(chuàng)建的模塊,
- 選中 "Start OSGI Console"
- Run time directory: recreate each time 最簡(jiǎn)單
- 自動(dòng)啟動(dòng) 隨意選擇
6. 啟動(dòng)測(cè)試
啟動(dòng), 經(jīng)過(guò)"漫長(zhǎng)"的等待, 界面出來(lái)了. 如果你選擇了"Start OSGI Console" 則可以命令行交互.
輸入help可以查看所有命令, 輸入ps可以查看所有啟動(dòng)的bundle.
start, stop 加上相應(yīng)id即可 啟動(dòng)/停止一個(gè)組件.