在文章一中,引用了Ralf Ebert的文章,讲述了整个实现的机制和原理,我在实际的实验工程中,遇到了一些问题,真实的记录下来,为后来者戒。
1、在把org.eclipse.springframework.util插件导入自己的workspace中时,如果没有在自己的eclipse runtime中设置spring bundle的位置,工程会报错,找不到导入的包。于是找到spring 3.0.4版本的bundle,加入到runtime中;第二部分要加入runtime的是spring osgi 的bundle,现在的版本是2.0,而比较稳定的是1.2.1;第三部分需要加入runtime的bundle是spring和spring osgi这些bundle的依赖bundle,和你在开发osgi bundle一样,通过不停的启动环境,然后加入进去就可以。
2、建立测试工程org.tkxing.rcp.spring,按照eclipse rcp 的生成一个View的wizard生产即可,具体的代码如附件中所示。在项目中import org.eclipse.springframework.util包。在osgi bundle中,spring osgi会去bundle的META-INF/spring目录下寻找spring的congtext文件,在工程中建立一个springContext.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" 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"> <bean id ="org.tkxing.spring.view.one" class ="org.tkxing.rcp.spring.View" scope="prototype"> </bean> </beans>
按照文章一中的介绍设置设置扩展点:
<extension point="org.eclipse.ui.views"> <view name="View" class="org.eclipse.springframework.util.SpringExtensionFactory" id="org.tkxing.spring.view.one"> </view> </extension>
3、启动实验工程,按照正常的情况下,应当系统运行成功,可是在我启动的过程中,工程一直报错无法正确运行。经过调试发现,在系统获得spring osgi在bundle中设置的的ApplicationContext服务时,总是不成功。可是在传统的方法中,无法看到在运行的工程中,到底哪些bundle成功运行,在bundle中提供出来哪些服务。于是,进行了诸多的网上搜索,发现在eclipse rcp的启动项中增加 -console选项,就会有osgi的交换出现在控制台上。在增加了这个参数后,应用bundle指令查看org.tkxing.rcp.spring插件,的确没有提供所需的服务,于是继续查找原因,发现系统启动过程中spring、spring osgi并不是默认启动,所以这些bundle的状态都是resolve的。经过在配置中把所用的bundle设置为默认启动,在系统系统,实验工程运行成功。