OSGI:构建Spring DM开发环境

本文介绍如何使用SpringDM实现Spring与OSGi的整合,包括下载SpringDM、配置SpringDM来发布OSGi服务等内容。文章详细展示了创建HelloWorld示例的过程。

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都没有问题,说明运行成功。

 

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值