OSGi @Activate breaks when method has parameters. Error: activate method not found; Component will fail

OSGi @Activate breaks when method has parameters. Error: activate method not found; Component will fail

我正在尝试为我的基于 OSGi 的应用程序实现正常关闭挂钩。我在一个包中实现它,因为我无法直接访问启动器源。

这里是 class:

/**
 * Shutdown Hook for OSGi
 * Based On: 
 */
@Component()
public class ShutdownHookActivator{

    @Activate
    public void start(ComponentContext cc, BundleContext bc, Map<String,Object> config) {
        Thread hook = new Thread() {
            @Override
            public void run() {
                System.out.println("Stopping OSGi Framework.");
            try {
                Framework systemBundle = bc.getBundle(0).adapt(Framework.class);
                systemBundle.stop();
                System.out.println("Waiting up to 2s for OSGi shutdown to complete...");
                systemBundle.waitForStop(2000);
            } catch (Exception e) {
                System.err.println("Failed to cleanly shutdown OSGi Framework: " + e.getMessage());
                    e.printStackTrace();
                }
            }
        };

        System.out.println("Installing shutdown hook.");
        Runtime.getRuntime().addShutdownHook(hook);
    }
}

但是每当我启动它时 returns 一个错误:

ERROR: [ShutdownHookActivator(8)] activate method [start] not found; Component will fail

但是当我从 start 方法中删除参数时,一切正常。所以现在我很困惑,我看到了一些例子,其中 @Activate 方法的参数是由 OSGi 框架自动注入的(http://enroute.osgi.org/services/org.osgi.service.component.html),而其他的只是空的,所以据我了解这应该工作。 (感觉OSGi全靠运气)

通过使用 DS 注释和获取 Framework BundleContext 来实现它的正确方法是什么?有可能吗?

编辑: OSGi环境: BndTools(aQute 启动器?) Apache Felix SCR 2.0.12(OSGi DS 注释 - 6.0.1) 来源可以在这里看到:https://github.com/Jafre13/ISS-Product

编辑 2: 自动生成的 ShutdownHookActivator.xml:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" name="dk.sdu.sso.sred.utils.ShutdownHookActivator" activate="start">
  <implementation class="dk.sdu.sso.sred.utils.ShutdownHookActivator"/>
</scr:component>

清单:

Manifest-Version: 1.0
Bnd-LastModified: 1512410080758
Bundle-ManifestVersion: 2
Bundle-Name: dk.sdu.sso.sred
Bundle-SymbolicName: dk.sdu.sso.sred
Bundle-Version: 0.0.0.201712041754
Created-By: 1.8.0_151 (Oracle Corporation)
Import-Package: javax.security.auth.x500,junit.framework,org.junit,org
 .xml.sax,org.xml.sax.ext,org.xml.sax.helpers
Private-Package: dk.sdu.sso.sred.cmd,dk.sdu.sso.sred.lingpipe,dk.sdu.s
 so.sred.api;version="1.0.0",dk.sdu.sso.sred,com.aliasi.test.unit.xml,
 com.aliasi.test.unit.io,com.aliasi.suffixarray,com.aliasi.test.unit.u
 til,com.aliasi.test.unit.crf,com.aliasi.matrix,com.aliasi.test.unit.s
 entences,com.aliasi.test.unit,com.aliasi.tag,com.aliasi.test.unit.hmm
 ,com.aliasi.chunk,com.aliasi.coref,com.aliasi.spell,com.aliasi.test.u
 nit.chunk,com.aliasi.test.unit.features,com.aliasi.coref.matchers,com
 .aliasi.lm,com.aliasi.test.unit.coref,com.aliasi.cluster,com.aliasi.f
 eatures,com.aliasi.test.unit.symbol,com.aliasi.io,com.aliasi.test.uni
 t.tokenizer,com.aliasi.sentences,com.aliasi.test.unit.coref.matchers,
 com.aliasi.test.unit.spell,com.aliasi.test.unit.corpus,com.aliasi.uti
 l,com.aliasi.dca,com.aliasi.symbol,com.aliasi.test.unit.lm,com.aliasi
 .dict,com.aliasi.test.unit.tag,com.aliasi.corpus,com.aliasi.classify,
 com.aliasi.test.unit.dca,com.aliasi.tokenizer,com.aliasi.test.unit.cl
 assify,com.aliasi.crf,com.aliasi.test.unit.cluster,com.aliasi.stats,c
 om.aliasi.test.unit.stats,com.aliasi.test.unit.dict,com.aliasi.test.u
 nit.matrix,com.aliasi.test.unit.suffixarray,com.aliasi.xml,com.aliasi
 .hmm,dk.sdu.sso.sred.utils,org.apache.felix.service.command,org.osgi.
 framework;version="1.8",org.osgi.service.component;version="1.3"
Provide-Capability: osgi.service;objectClass:List<String>="dk.sdu.sso.
 sred.api.SRedAPI",osgi.service;objectClass:List<String>="dk.sdu.sso.s
 red.cmd.ModelCommands",osgi.service;objectClass:List<String>="dk.sdu.
 sso.sred.cmd.SRedCommands"
Require-Capability: osgi.extender;filter:="(&(osgi.extender=osgi.compo
 nent)(version>=1.3.0)(!(version>=2.0.0)))",osgi.ee;filter:="(&(osgi.e
 e=JavaSE)(version=1.8))"
Service-Component: OSGI-INF/dk.sdu.sso.sred.SRed.xml,OSGI-INF/dk.sdu.s
 so.sred.cmd.ModelCommands.xml,OSGI-INF/dk.sdu.sso.sred.cmd.SRedComman
 ds.xml,OSGI-INF/dk.sdu.sso.sred.utils.ShutdownHookActivator.xml
Tool: Bnd-3.5.0.201709291849

从清单中可以很明显地看出您的问题发生的原因。您将 OSGi API 作为私有包。这意味着 类 嵌入到您的 jar 中,因此它们将与框架和 DS 使用的规范 类 不兼容。

所以解决方案是确保您只对真正需要嵌入的内容使用私有包。切勿将其用于 OSGi api 包。您需要导入它们才能工作。