OSGI:构建Spring DM开发环境

来源:互联网 发布:成都直通车淘宝 编辑:程序博客网 时间:2024/05/06 15:30

spring dm又称spring动态模型,是spring对于osgi的实现,它仍然以Equinox为集成,但提供了大量与sping pojo相关的bundle,极大的方便了与现有基于spring开发的应用的整合。此次依旧使用SpringSource Tool Suite进行开发。

 

一、下载spring dm

       开发中使用了spring-osgi-1.2.1-with-dependencies.zip,该压缩包中包含了spring osgi所依赖的类库。解压后dist目录是spring osgi的bundle,lib是可能依赖的类库,也以bundle的形式存在。

 

二、引用spring bundle

       在STS中import/Plug-in Development/Plug-ins and Frgments,选择解压后的dist和list,将com.springsource.org.aopalliance,

org.springframework.aop,

org.springframework.beans,

org.springframework.core,

org.springframework.context,

org.springframework.osgi.core,

org.springframework.osgi.extender,

org.springframework.osgi.io

这些bundle导入进来。

 

三、建立helloworld

      1. 建立Plug-in Development/Plug-in Project工程helloworld,不生成Activator;

      2. 在META-INF目录中建立spring文件夹,使用spring dm会自动加载此文件夹中的.xml文件;

      3. 分别通过spring/Spring Bean Configuration File建立helloworld.xml和helloworld-osgi.xml,并在helloworld-osgi.xml文件中添加osgi命名空间的支持;

      4. 建立HelloWorld接口和HelloWorldImpl作为实现类:

 

 

      5. 编辑helloworld.xml,用于配置spring的pojo模型,根据spring的建议-osgi表示osgi服务等相关的配置,而没有-osgi的文件用于配置spring的pojo逻辑,但这只是建议,可以根据自己的喜好明明,因为spring dm会默认自动加载此文件夹下的所有xml文件:

 

通过osgi:service将bean发布为服务:

 

注意osgi的名称空间不要使用http://www.springframework.org/schema/osgi/spring-osgi-2.0-m1.xsd,因为STS默认使用了次空间,但目前使用会报错(在ubuntu中可以正常使用),因此需要修改为:http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd,如果不更改所报异常如下:

 

 

四、运行

    要想能正常运行还有添加org.eclipse.osgi_3.5.2.R35x_v20100126和org.apache.commons.logging_1.0.4.v200904062259两个bundle,启动后发现helloworld已经处于ACTIVE状态了,此时stop和start都没有问题,说明运行成功。

 

 

 

 

 

原创粉丝点击