在上面已經配置了sts及virgo的環境,並且能夠成功的運行virgo服務器了。接下來我們來用sts建幾個工程。
我們模擬的是一個注冊的例子,在我們實際的案例中,有的時候會把數據寫入到數據庫,寫入到文件或者寫入到內存中,已方便不同的操作。也許這個例子不能完全說明問題,但是對於說明如何通過sts來建立工程來說已經足夠了。
我們會建立4個Bundle,一個是通過頁面進行注冊的Bundle,一個是錄入接口的Bundle,一個是將文件寫入到數據庫的Bundle(當然只是簡單的實現並沒有真正寫入數據庫),還有一個是寫入文件的Bundle。
接口工程創建
新建vigro插件工程
輸入工程名,下一步,下一步直到完成,其中選擇默認的就可以,不需要做其它的修改。
因為是接口工程,所以不需要其它額外的配置,只需要將要導出的目錄導出即可。
先創建接口IStore,用來標識接口存儲登錄信息,接口內容如下:

public interface IStore { void store(LoginBean loginBean); }
其中需要用到JavaBean,命名為LoginBean,這個是用來模擬存儲登錄信息,代碼如下:

public class LoginBean { private String name; private String pass; private String repass; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public String getRepass() { return repass; } public void setRepass(String repass) { this.repass = repass; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
然后雙擊META-INF下的MANIFEST.MF文件,設置導出的目錄,也就是其它模塊可以訪問的目錄或者說接口
選擇Runtime標簽,在里邊加上剛設置的目錄
然后看MAINIFEST.MF中的內容:

Manifest-Version: 1.0 Bundle-Version: 1.0.0 Bundle-Name: Register Bundle-ManifestVersion: 2 Bundle-Description: TGYT Bundle-SymbolicName: com.tgyt.test.register Export-Package: com.tgyt.test.register
數據庫存儲工程創建
這個工程的目的是將數據存儲到數據庫中,當然只是一個示意的工程,沒有將數據真正存入。
創建方法同上,修改工程名,然后在import package中導入上邊創建的接口
在這里需要先對上邊的工程進行引用,在新建的工程上點擊右鍵,選擇工程引用,引用上邊建立好的工程
然后添加對工程的正式引用,可以直接引用bundle
在工程中新增類StoreDB,這個用於將注冊的信息存儲到數據庫中,當然這部分可以換成真正的實現類,現在只是幾段輸出

public class StoreDB implements IStore { /* (non-Javadoc) * @see com.tgyt.register.store.IStore#store(com.tgyt.register.store.LoginBean) */ @Override public void store(LoginBean loginBean) { System.out.println("將下面的信息存入到數據庫中……"); System.out.println(loginBean.getName()); System.out.println(loginBean.getAge()); } }
因為這個是實現的bundle,所以需要將實現的配置暴露出來,發布成其它工程能引用的spring服務
在META-INF目錄下新建目錄spring,這個是osgi容器自動掃描的目錄,每次部署應用時osgi會自動掃描下邊的*.xml文件
我們在這里添加兩個文件,一個用來部署spring應用文件,一個用來對外發布接口,先看appContext.xml,這個用來注入接口

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean name="storeDB" class="com.tgyt.test.register.db.StoreDB"/> </beans>
然后看osgi-context.xml,這個用來發布服務

<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/osgi" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <service id="osgiStoreDB" ref="storeDB" interface="com.tgyt.test.register.IStore"/> </beans:beans>
ref指定引用的bean的名稱
interface指定引用的接口,這兩項必須輸入
文件存儲工程創建
同上,只是配置文件略有修改,修改成存儲文件的內容。
測試工程創建
根據上邊的步驟建立測試工程,並將上面的三個工程全部關聯。
然后在MANIFEST.MF配置中引用三個工程的Bundle
新建類TestInput,輸入的內容如下:

public class TestInput { private IStore iStore; public IStore getiStore() { return iStore; } public void setiStore(IStore iStore) { this.iStore = iStore; } public void store(){ System.out.println("進入注冊程序……"); LoginBean loginBean = new LoginBean(); loginBean.setName("zhansan"); loginBean.setAge(21); iStore.store(loginBean); System.out.println("結束注冊……"); } }
在META-INF目錄下新建目錄spring,然后加入文件appContext.xml和osgi-context.xml,兩個文件的內容分別是:
appContext.xml:注入測試類

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean name="testInput" class="com.tgyt.test.register.inputtest.TestInput" init-method="store"> <property name="iStore" ref="osgistore" /> </bean> </beans>
在這里為了測試,加入了init-method,也就是在程序部署進去的時候自動加載store這個方法,也就是我們上邊要在控制台上輸出的內容。
osgi-context.xml的內容是為了引用其它的osgi服務,內容如下:

<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/osgi" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <reference id="osgistore" bean-name="storeFile" interface="com.tgyt.test.register.IStore" /> </beans:beans>
注意:如果有多個實現,bean-name必須指定。
好了,工程建立完畢,測試一下。啟動virgo服務器。
啟動完成后,在服務器上點擊右鍵,選擇Add and Remove,先將接口和兩個實現的Bundle加入
加入后,完成,這時候控制台會顯示:
對應的這幾個bundle就會被加載到virgo中。然后我們加入測試工程,如果順利的話,應該打印出對應的提示信息:
加入后,新加入的bundle自動啟動,控制台上打印出:
這個時候如果想把應用改為存儲到數據庫中,做如下的步驟,首先移除工程
然后修改osgi-context.xml中的bean-name="storeDB",改成另一個bundle暴露出來的接口,然后再將工程添加進去。
控制台上打印出另一個實現中的內容
這樣我們就實現了熱部署,在有重啟應用的情況下,我們可以做把注冊信息轉成webservice服務、存儲到ldap上等實現並且可以動態的切換實現的內容。
遇到的問題
1、測試工程引用時只能調用一個引用
就是在測試工程引用服務的過程中,每次都是指定一個接口的引用。在我的示例里邊每次都指向的是數據存儲的工程。
經過查找是在引用的時候沒有指定"bean-name"屬性,需要將

<bean id="(1)messageServiceBean" scope="bundle" class="com.xyz.MessageServiceImpl"/> <!-- service exporter --> <osgi:service id="messageServiceExporter" ref="(1)messageServiceBean" interface="com.xyz.MessageService"/> <osgi:reference id="messageService" interface="com.xyz.MessageService" bean-name="(1)messageServiceBean"/>
標1的部分指定為同一個名稱,這樣在容器內才能找到對應的引用。
2、控制台不打印System.out的信息
默認情況下控制台是不打印SysOut和SysErr的信息的,而是打印到日志文件中,這樣我們調試起來非常麻煩,我們需要修改配置文件org.eclipse.virgo.medic.properties
這個文件在virgo的解壓目錄下的config下,我們需要將下面兩項都改成false

log.wrapSysOut=false log.wrapSysErr=false
這樣控制台能正常的打印信息了。