Service Amdatu和DependencyManager错误注入服务

Service Amdatu和DependencyManager错误注入服务,service,osgi,code-injection,bndtools,amdatu,Service,Osgi,Code Injection,Bndtools,Amdatu,我安装了Amdatu引导程序并创建了一个项目 我尝试使用dependecyManager进行简单的服务注入,但运行时出现了一个错误 代码和我的错误 这是我的服务的实现: package cco.bgen.scanner.XmlParser.implService; import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers

我安装了Amdatu引导程序并创建了一个项目

我尝试使用dependecyManager进行简单的服务注入,但运行时出现了一个错误

代码和我的错误

这是我的服务的实现:

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 {

    }

}
因此,当我在运行描述符中运行这个时,我有一个错误==>

! 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中,我看到运行时中同时有依赖关系管理器3和4。确保您的捆绑包导入了正确的版本,例如通过显式设置生成路径的版本。

请确定如何设置生成路径的版本??啊,好的,非常感谢:)嗨@Paul Bakker。我刚刚开始使用OSGi,但我真的被卡住了。请你看看我的问题,看看你能不能帮我。如果您能提供任何帮助,我将不胜感激。先谢谢你。附言:我也一直在看你们的视频教程。他们帮助我更好地掌握OSGi。谢谢你。