由于公司需要开始学习OSGI。看了些文档,想写个小型demo跑跑看,但是程序跑不起来。运行时出现错误。
希望有经验的朋友半忙看看,也在学习这东西的朋友一起交流一下。
  
  环境 Equinox容器 + Spring dm 1.21下面上具体代码//计算的接口
package com.zjhcsoft.compute;public interface Compute {
public String comput(int x,int y);
}接口的MANIFEST.MF文件
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Comput Plug-in
Bundle-SymbolicName: com.zjhcsoft.compute
Bundle-Version: 1.0.0
Bundle-Vendor: ZJHCSOFT
Eclipse-LazyStart: true
Import-Package: org.osgi.framework;version="1.3.0"
Export-Package: com.zjhcsoft.compute
//接口的实现类,加法
package com.zjhcsoft.add;import com.zjhcsoft.compute.Compute;public class ComputeAdd implements Compute{ public String comput(int x, int y) {
System.out.println("做了加法");
return (x+y)+"";
}}加法实现类的MANIFEST.MF文件
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Add Plug-in
Bundle-SymbolicName: com.zjhcsoft.add
Bundle-Version: 1.0.0
Bundle-Vendor: ZJHCSOFT
Eclipse-LazyStart: true
Import-Package: com.zjhcsoft.compute,
 org.osgi.framework;version="1.3.0"
注册加法的服务的配置文件 addService.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:osgi="http://www.springframework.org/schema/osgi" 
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans.xsd 
   http://www.springframework.org/schema/osgi
   http://www.springframework.org/schema/osgi/spring-osgi.xsd
   ">
   <bean id = "computeAdd" class="com.zjhcsoft.add.ComputeAdd" >
   </bean>
   <osgi:service id="computeAddService" interface="com.zjhcsoft.compute.Compute" ref="computeAdd" context-class-loader="service-provider"></osgi:service>
</beans>
//接口的实现类,减法package com.zjhcsoft.mul;import com.zjhcsoft.compute.Compute;public class ComputeMul implements Compute { public String comput(int x, int y) {
System.out.println("做了减法");
return (x-y)+"";
}}
减法实现类的MANIFEST.MF文件
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Mul Plug-in
Bundle-SymbolicName: com.zjhcsoft.mul
Bundle-Version: 1.0.0
Bundle-Vendor: ZJHCSOFT
Eclipse-LazyStart: true
Import-Package: com.zjhcsoft.compute,
 org.osgi.framework;version="1.3.0"注册减法服务的配置文件 mulService.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:osgi="http://www.springframework.org/schema/osgi" 
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans.xsd 
   http://www.springframework.org/schema/osgi
   http://www.springframework.org/schema/osgi/spring-osgi.xsd
   ">
   <bean id = "computeMul" class="com.zjhcsoft.mul.ComputeMul" >
   </bean>
   <osgi:service id="computeMulService" interface="com.zjhcsoft.compute.Compute" ref="computeMul" context-class-loader="service-provider"></osgi:service>
</beans>
//服务的消费者package com.zjhcsoft.clent;import com.zjhcsoft.compute.Compute;
public class Clent {
public Clent(Compute compute){
System.out.println(compute.comput(6,5));
}
}消费者的MANIFEST.MF文件
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Clent Plug-in
Bundle-SymbolicName: com.zjhcsoft.clent
Bundle-Version: 1.0.0
Bundle-Vendor: ZJHCSOFT
Eclipse-LazyStart: true
Import-Package: com.zjhcsoft.compute,
 org.osgi.framework;version="1.3.0"
//消费者的配置文件
<?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/beans 
   http://www.springframework.org/schema/beans/spring-beans.xsd 
   http://www.springframework.org/schema/osgi
   http://www.springframework.org/schema/osgi/spring-osgi.xsd
   ">
   <bean id = "clent" class="com.zjhcsoft.clent.Clent">
    <constructor-arg ref="computeService">
    </constructor-arg>
   </bean>
   <osgi:reference id="computeService" interface="com.zjhcsoft.compute.Compute" cardinality="1..1" ></osgi:reference>
</beans>
下面是报错信息的一本分严重: Pre refresh error
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from URL [bundleentry://3470/META-INF/spring/reference.xml]; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:406)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:296)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.osgi.context.support.OsgiBundleXmlApplicationContext.loadBeanDefinitions(OsgiBundleXmlApplicationContext.java:221)
at org.springframework.osgi.context.support.OsgiBundleXmlApplicationContext.loadBeanDefinitions(OsgiBundleXmlApplicationContext.java:130)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:423)
at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.startRefresh(AbstractDelegatedExecutionApplicationContext.java:191)
at org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor.stageOne(DependencyWaiterApplicationContextExecutor.java:212)
at org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor.refresh(DependencyWaiterApplicationContextExecutor.java:163)
at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.refresh(AbstractDelegatedExecutionApplicationContext.java:125)
at org.springframework.osgi.extender.internal.ContextLoaderListener$2.run(ContextLoaderListener.java:630)
at java.lang.Thread.run(Thread.java:595)
Caused by: java.lang.NullPointerException
at org.apache.xerces.impl.xs.traversers.XSDAttributeTraverser.traverseLocal(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDAbstractTraverser.traverseAttrsAndAttrGrps(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDComplexTypeTraverser.processComplexContent(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDComplexTypeTraverser.traverseComplexTypeDecl(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDComplexTypeTraverser.traverseLocal(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDElementTraverser.traverseNamedElement(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDElementTraverser.traverseGlobal(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.traverseSchemas(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.parseSchema(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.findSchemaGrammar(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:382)
... 15 more
2011-3-15 16:32:56 org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor fail