我安装了Amdatu Bootstrap并创建了一个项目。
我试着用dependencyManager做一个简单的服务注入,但在运行时出现了一个错误。
代码和我的错误。
这是我的服务的实现:
package cco.bgen.scanner.XmlParser.implService;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import cco.bgen.scanner.XmlParser.contratService.XmlParserContrat;
public class XmlParserService implements XmlParserContrat {
private DocumentBuilderFactory fabrique;
private DocumentBuilder constructeur;
private Document document;
private Element rootElement;
public XmlParserService() {
super();
}
@Override
public Element parserFichier(String filePath) {
// Fabrique pour l'obtention d'une instance du documentBuilder : constructeur
this.fabrique = DocumentBuilderFactory.newInstance();
try {
//l'obtention du constructeur
this.constructeur = fabrique.newDocumentBuilder();
try {
//document à partir d'une path
this.document = constructeur.parse(new File(filePath));
//element root du document
this.rootElement = document.getDocumentElement();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
//Resultat
return this.rootElement;
}
}
这就是接口:
package cco.bgen.scanner.XmlParser.contratService;
import org.w3c.dom.Element;
public interface XmlParserContrat {
public Element parserFichier(String fichierPath);
}
此服务的激活器:
package cco.bgen.scanner.XmlParser.implService;
import org.apache.felix.dm.DependencyActivatorBase;
import org.apache.felix.dm.DependencyManager;
import org.osgi.framework.BundleContext;
import cco.bgen.scanner.XmlParser.contratService.XmlParserContrat;
public class XmlParserActivator extends DependencyActivatorBase {
@Override
public void init(BundleContext arg0, DependencyManager manager)
throws Exception {
manager.add(createComponent().setInterface(
XmlParserContrat.class.getName(), null).
setImplementation(XmlParserService.class));
}
@Override
public void destroy(BundleContext arg0, DependencyManager arg1)
throws Exception {
// TODO Auto-generated method stub
}
}
之后,我创建了一个测试包来使用xmlParserService:
package cco.bgen.test;
import org.w3c.dom.Element;
import cco.bgen.scanner.XmlParser.contratService.XmlParserContrat;
public class Test {
private volatile XmlParserContrat xmlParser;
public void start(){
Element rootE = xmlParser.parserFichier("processus.xml");
System.out.println(rootE.toString());
}
}
以及测试服务的激活器:
package cco.bgen.test;
import org.apache.felix.dm.DependencyActivatorBase;
import org.apache.felix.dm.DependencyManager;
import org.osgi.framework.BundleContext;
import cco.bgen.scanner.XmlParser.contratService.XmlParserContrat;
public class TestActivator extends DependencyActivatorBase {
@Override
public void init(BundleContext arg0, DependencyManager manager)
throws Exception {
manager.add(createComponent()
.setInterface(Object.class.getName(), null)
.setImplementation(Test.class)
.add(createServiceDependency().setService(
XmlParserContrat.class)));
}
@Override
public void destroy(BundleContext arg0, DependencyManager manager)
throws Exception {
}
}
因此,当我在run Descriptor中运行时,我会出现以下错误===>
! Failed to start bundle cco.bgen.scanner.Test-0.0.0, exception activator error org.apache.felix.dm.Component.add(Lorg/apache/felix/dm/Dependency;)Lorg/apache/felix/dm/Component; from: cco.bgen.test.TestActivator:init#18
____________________________
Welcome to Apache Felix Gogo
所以我录制lb命令以显示活动捆绑包,然后重新启动捆绑包。详细错误显示======>
g! lb
START LEVEL 1
ID|State |Level|Name
0|Active | 0|System Bundle (4.2.1)
1|Resolved | 1|cco.bgen.scanner.Test (0.0.0)
2|Active | 1|cco.bgen.scanner.XmlParser (1.0.0)
3|Active | 1|org.amdatu.template (1.0.0)
4|Active | 1|Apache Felix Configuration Admin Service (1.8.0)
5|Active | 1|Apache Felix Dependency Manager (3.1.0)
6|Active | 1|Apache Felix Dependency Manager (4.0.1)
7|Active | 1|Apache Felix Gogo Command (0.12.0)
8|Active | 1|Apache Felix Gogo Runtime (0.10.0)
9|Active | 1|Apache Felix Gogo Shell (0.10.0)
10|Active | 1|Apache Felix Servlet API (1.0.0)
11|Active | 1|Apache Felix Metatype Service (1.0.6)
g! start 1
g! org.osgi.framework.BundleException: Activator start error in bundle cco.bgen.scanner.Test [1].
at org.apache.felix.framework.Felix.activateBundle(Felix.java:2196)
at org.apache.felix.framework.Felix.startBundle(Felix.java:2064)
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955)
at org.apache.felix.gogo.command.Basic.start(Basic.java:729)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.felix.gogo.runtime.Reflective.invoke(Reflective.java:137)
at org.apache.felix.gogo.runtime.CommandProxy.execute(CommandProxy.java:82)
at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:477)
at org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:403)
at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108)
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:183)
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:120)
at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessionImpl.java:89)
at org.apache.felix.gogo.shell.Console.run(Console.java:62)
at org.apache.felix.gogo.shell.Shell.console(Shell.java:203)
at org.apache.felix.gogo.shell.Shell.gosh(Shell.java:128)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.felix.gogo.runtime.Reflective.invoke(Reflective.java:137)
at org.apache.felix.gogo.runtime.CommandProxy.execute(CommandProxy.java:82)
at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:477)
at org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:403)
at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108)
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:183)
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:120)
at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessionImpl.java:89)
at org.apache.felix.gogo.shell.Activator.run(Activator.java:75)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoSuchMethodError: org.apache.felix.dm.Component.add(Lorg/apache/felix/dm/Dependency;)Lorg/apache/felix/dm/Component;
at cco.bgen.test.TestActivator.init(TestActivator.java:18)
at org.apache.felix.dm.DependencyActivatorBase.start(DependencyActivatorBase.java:75)
at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:645)
at org.apache.felix.framework.Felix.activateBundle(Felix.java:2146)
... 32 more
java.lang.NoSuchMethodError: org.apache.felix.dm.Component.add(Lorg/apache/felix/dm/Dependency;)Lorg/apache/felix/dm/Component;
这是错误中的第18行::=>
.add(createServiceDependency().setService(
在TestActivator.java 中
我看到您的运行时中同时有Dependency Manager 3和4。确保您的捆绑包导入正确的版本,例如显式设置构建路径的版本。