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
- package org.jack;
- public interface HelloWorld {
- public String sayHello(String name);
- }
[java] view plaincopy
- package org.jack.impl;
- import org.jack.HelloWorld;
- public class HelloWorldImpl implements HelloWorld {
- public String sayHello(String name) {
- return "hello, " + name;
- }
- }
5. 编辑helloworld.xml,用于配置spring的pojo模型,根据spring的建议-osgi表示osgi服务等相关的配置,而没有-osgi的文件用于配置spring的pojo逻辑,但这只是建议,可以根据自己的喜好明明,因为spring dm会默认自动加载此文件夹下的所有xml文件:
[xhtml] view plaincopy
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="helloWorld" class="org.jack.impl.HelloWorldImpl" />
- </beans>
通过osgi:service将bean发布为服务:
[java] view plaincopy
- <?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:osgi="http://www.springframework.org/schema/osgi"
- xsi:schemaLocation="http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
- <osgi:service id="helloWorldService" interface="org.jack.HelloWorld" ref="helloWorld" />
- </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
- 严重: Application context refresh failed (OsgiBundleXmlApplicationContext(bundle=jack.helloworld, config=osgibundle:/META-INF/spring/*.xml))
- 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?
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1279)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
- at java.security.AccessController.doPrivileged(Native Method)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
- at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
- at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
- at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
- at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
- at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
- at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
- at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
- at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.access$1600(AbstractDelegatedExecutionApplicationContext.java:69)
- at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext$4.run(AbstractDelegatedExecutionApplicationContext.java:355)
- at org.springframework.osgi.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)
- at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:320)
- at org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:132)
- at java.lang.Thread.run(Thread.java:619)
- 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?
- at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:801)
- at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:651)
- at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:78)
- at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:59)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1276)
- ... 18 more
- 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?
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1279)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
- at
四、运行
要想能正常运行还有添加org.eclipse.osgi_3.5.2.R35x_v20100126和org.apache.commons.logging_1.0.4.v200904062259两个bundle,启动后发现helloworld已经处于ACTIVE状态了,此时stop和start都没有问题,说明运行成功。