Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.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 OSGi捆绑包从另一个捆绑包访问Spring上下文文件_Java_Spring_Osgi_Apache Karaf_Apache Servicemix - Fatal编程技术网

Java OSGi捆绑包从另一个捆绑包访问Spring上下文文件

Java OSGi捆绑包从另一个捆绑包访问Spring上下文文件,java,spring,osgi,apache-karaf,apache-servicemix,Java,Spring,Osgi,Apache Karaf,Apache Servicemix,我有一个作为多个Spring项目存在的现有应用程序。项目A的Spring上下文XML文件使用 <import resource="classpath*:/META-INF/spring/BContext.xml" /> 但是,我得到一个FileNotFoundException。我假设这是因为资源没有被项目B的包公开。我可以访问类,但不能访问文件 在研究这个问题时,常见的评论是使用OSGi服务并注入服务,而不是试图直接注入bean。但是,由于这是一个现有的应用程序,我希望避免重

我有一个作为多个Spring项目存在的现有应用程序。项目A的Spring上下文XML文件使用

<import resource="classpath*:/META-INF/spring/BContext.xml" />

但是,我得到一个
FileNotFoundException
。我假设这是因为资源没有被项目B的包公开。我可以访问类,但不能访问文件

在研究这个问题时,常见的评论是使用OSGi服务并注入服务,而不是试图直接注入bean。但是,由于这是一个现有的应用程序,我希望避免重新连接整个应用程序


有没有办法告诉OSGi导出资源?我正在Karaf上运行ServiceMix。

它只是一个类路径资源,因此我假设添加一个适当的
导出包
指令就可以了。但这绝对不是正确的方法。该上下文文件的路径表明,可能包含BContext.xml的项目已经设置为使用Spring动态模块。如果是这样,那么在启动该捆绑包时,Spring ApplicationContext将作为服务导出。在OSGi控制台中查找它

编辑:回应评论中的讨论:


我自己从来没有尝试过,但理论上应该可以使用SpringDM来创建一个项目B的ApplicationContext。然后,有了一个bean,即ApplicationContext,您可以使用来使用方法从中提取bean。请注意,您可以使用
在Spring配置中为工厂方法指定参数,如图所示。

它只是一个类路径资源,因此我假设添加一个适当的
导出包
指令就可以了。但这绝对不是正确的方法。该上下文文件的路径表明,可能包含BContext.xml的项目已经设置为使用Spring动态模块。如果是这样,那么在启动该捆绑包时,Spring ApplicationContext将作为服务导出。在OSGi控制台中查找它

编辑:回应评论中的讨论:


我自己从来没有尝试过,但理论上应该可以使用SpringDM来创建一个项目B的ApplicationContext。然后,有了一个bean,即ApplicationContext,您可以使用来使用方法从中提取bean。请注意,您可以使用
在Spring配置中为工厂方法指定参数,如图所示。

从另一个模块加载Spring上下文和所有实现类是对模块封装的严重违反。如果您愿意这样做,那么将A和B作为单独的捆绑包是毫无意义的,您不妨将它们作为单个捆绑包。

从另一个模块加载Spring上下文和所有实现类是对模块封装的严重违反。如果您愿意这样做,那么将A和B作为单独的捆绑包是毫无意义的,您不妨将它们作为单个捆绑包。

您应该这样做的方式是利用OSGi服务。您可以在Spring DM中使用以下内容注册服务(通常在单独的osgi-context.xml文件中完成,以确保代码库不依赖于osgi进行测试)。在本例中,您将拥有一个bean,其id clinic在BContext.xml中定义,并作为osgi服务引用

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/osgi"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/osgi  
        http://www.springframework.org/schema/osgi/spring-osgi.xsd
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <service id="osgiClinic" ref="clinic" interface="org.springframework.petclinic.repository.Clinic" />

</beans:beans>

然后在消费包的osgi-context.xml中,您将引用该服务

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/osgi"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/osgi  
        http://www.springframework.org/schema/osgi/spring-osgi.xsd
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <reference id="clinic" interface="org.springframework.petclinic.repository.Clinic"/>

</beans:beans>


这种做法将确保您考虑捆绑包之间的依赖关系,并且只导出其他捆绑包所需的服务。

您应该这样做的方式是利用OSGi服务。您可以在Spring DM中使用以下内容注册服务(这通常在单独的osgi-context.xml文件中完成,以确保代码库不依赖于osgi进行测试。在本例中,您将有一个bean,其id clinic在BContext.xml中定义,并作为osgi服务引用

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/osgi"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/osgi  
        http://www.springframework.org/schema/osgi/spring-osgi.xsd
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <service id="osgiClinic" ref="clinic" interface="org.springframework.petclinic.repository.Clinic" />

</beans:beans>

然后在消费包的osgi-context.xml中,您将引用该服务

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/osgi"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/osgi  
        http://www.springframework.org/schema/osgi/spring-osgi.xsd
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <reference id="clinic" interface="org.springframework.petclinic.repository.Clinic"/>

</beans:beans>


这种做法将确保您考虑捆绑包之间的依赖关系,只导出其他捆绑包所需的服务。

我知道ApplicationContext是公开的,但是如果不是OSGi服务,我如何将其连接到XML文件中?对于导出包,它会是“META-INF.spring”吗作为包?@Justin:它作为OSGi服务导出到Spring DM容器中。您可以像使用其他OSGi服务一样使用它。对于导出包,“META-INF.Spring”是的,我会尝试这样做;但是,我突然想到连字符在包名中无效,因此您可能必须将文件移到其他地方。上下文中的所有bean都是作为OSGi服务导出的,还是只导出上下文本身?@Justin:我知道ApplicationContext是公开的,但是我如何将其连接到XML文件中,除非它是公开的是OSGi服务吗?对于导出包,它会是“META-INF.spring”作为包吗?@Justin:它作为OSGi服务导出到spring DM容器中。您可以像使用任何其他OSGi服务一样使用它。对于导出包,它是“META-INF.spring”是的,我会尝试这样做;但是,我突然想到连字符在包名中无效,因此您可能必须将文件移到其他地方。上下文中的所有bean是作为OSGi服务导出的,还是只导出上下文本身?@Justin: