OSGI:构建Spring DM开发环境

 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作为实现类:

 

[java] view plaincopy

  1. package org.jack;  
  2. public interface HelloWorld {  
  3.     public String sayHello(String name);  
  4. }  

 

 

 

[java] view plaincopy

  1. package org.jack.impl;  
  2. import org.jack.HelloWorld;  
  3. public class HelloWorldImpl implements HelloWorld {  
  4.     public String sayHello(String name) {  
  5.         return "hello, " + name;  
  6.     }  
  7. }  

 

 

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

 

[xhtml] view plaincopy

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  
  5.     <bean id="helloWorld" class="org.jack.impl.HelloWorldImpl" />  
  6. </beans>  

 

 

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

 

[java] view plaincopy

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:osgi="http://www.springframework.org/schema/osgi"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd  
  6.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  
  7.     <osgi:service id="helloWorldService" interface="org.jack.HelloWorld" ref="helloWorld" />  
  8. </beans>  

 

 

注意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,如果不更改所报异常如下:

 

[xhtml] view plaincopy

  1. 严重: Application context refresh failed (OsgiBundleXmlApplicationContext(bundle=jack.helloworld, config=osgibundle:/META-INF/spring/*.xml))  
  2. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloWorldService': Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'cacheTarget' of bean class [org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean]: Bean property 'cacheTarget' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?  
  3.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1279)  
  4.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)  
  5.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)  
  6.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)  
  7.     at java.security.AccessController.doPrivileged(Native Method)  
  8.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)  
  9.     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)  
  10.     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)  
  11.     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)  
  12.     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)  
  13.     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)  
  14.     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)  
  15.     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)  
  16.     at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.access$1600(AbstractDelegatedExecutionApplicationContext.java:69)  
  17.     at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext$4.run(AbstractDelegatedExecutionApplicationContext.java:355)  
  18.     at org.springframework.osgi.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)  
  19.     at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:320)  
  20.     at org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:132)  
  21.     at java.lang.Thread.run(Thread.java:619)  
  22. Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'cacheTarget' of bean class [org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean]: Bean property 'cacheTarget' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?  
  23.     at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:801)  
  24.     at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:651)  
  25.     at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:78)  
  26.     at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:59)  
  27.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1276)  
  28.     ... 18 more  
  29. Exception in thread "SpringOsgiExtenderThread-4" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloWorldService': Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'cacheTarget' of bean class [org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean]: Bean property 'cacheTarget' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?  
  30.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1279)  
  31.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)  
  32.     at  

 

 

 

四、运行

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值