Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在未启动框架的其他类中获取BundleContext?_Java_Osgi_Apache Felix_Osgi Bundle - Fatal编程技术网

Java 如何在未启动框架的其他类中获取BundleContext?

Java 如何在未启动框架的其他类中获取BundleContext?,java,osgi,apache-felix,osgi-bundle,Java,Osgi,Apache Felix,Osgi Bundle,我最近开始使用OSGi框架。我正在尝试从Java主应用程序启动OSGi框架。我遵循这一点将OSGI容器嵌入到我的项目中 下面是我用来启动OSGi容器的Java主应用程序。在下面的类中,我可以使用framework对象获取BundleContext,然后我可以使用这个BundleContext来安装实际的OSGi bundles public class OSGiBundleTest1 { public static Framework framework = null; pu

我最近开始使用OSGi框架。我正在尝试从Java主应用程序启动OSGi框架。我遵循这一点将OSGI容器嵌入到我的项目中

下面是我用来启动OSGi容器的Java主应用程序。在下面的类中,我可以使用
framework
对象获取
BundleContext
,然后我可以使用这个
BundleContext
来安装实际的
OSGi bundles

public class OSGiBundleTest1 {

    public static Framework framework = null;

    public static void main(String[] args) throws BundleException {

        FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
        Map<String, String> config = new HashMap<String, String>();

        framework = frameworkFactory.newFramework(config);
        framework.start();

        callMethod();
        callMethodOfAnotherClass();
    }

    private static void callMethodOfAnotherClass() {
        OSGiBundleTest2 ss = new OSGiBundleTest2();
        ss.someMethod();

    }

    private static void callMethod() throws BundleException {

        BundleContext context = framework.getBundleContext();

        System.out.println(context);
    }
}
}

CallMethodOtherClass
将从OSGiBundleTest1类调用。我不想将
framework
对象传递给OSGiBundleTest2类的构造函数,或者传递一些方法来使用framework对象,然后从那里获取BundleContext。。。还有别的办法吗


确保所有类仅由OSGI类加载器加载的方法是什么?

确保类由OSGI类加载器加载的方法是将它们放在捆绑包中,并实际使用OSGI加载它们。

“确保所有类仅由OSGI类加载器加载的方法”嗯,是的,将它们放在OSGI捆绑包中。很抱歉,这不是很明显吗?TechGeeky,请阅读“OSGi正在运行”或跟随网络上的许多教程。我不认为Stackoverflow是为那些在您学习了一些初学者教程之后就变得非常明显的问题而设计的?另外,从bndtools开始,而不是从底层启动API开始,这样您就可以在尝试修改概念之前先了解它们。
public class OSGiBundleTest2 {

public OSGiBundleTest2() {

}


public static void callMethodOfAnotherClass() {

    System.out.println(FrameworkUtil.getBundle(OSGiBundleTest2.class));

    BundleContext bundleContext = FrameworkUtil.getBundle(OSGiBundleTest2.class).getBundleContext();
}