OSGi中的Spring组件扫描未发现任何内容

OSGi中的Spring组件扫描未发现任何内容,spring,integration,osgi,Spring,Integration,Osgi,使用,我尝试进行组件扫描,以在包中搜索带Spring注释的bean。我的Spring XML配置如下所示: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.sprin

使用,我尝试进行
组件扫描
,以在包中搜索带Spring注释的bean。我的Spring XML配置如下所示:

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

    <!-- Scans the classpath of this application for @Components to deploy as 
        beans -->
    <context:component-scan
        base-package="com.some.other.module.one,com.another.module.two" />

    <context:annotation-config />
....

</beans>

....
在清单中,我导入包含带有Spring注释的类的包。但是,当我检查ApplicationContext时,它没有任何带注释的bean

我相信这是因为我们扫描的类路径在不同的包中。这些包不会直接导入包含Spring注释的类的包。令人困惑的是,为什么Spring没有获取组件扫描开始的主包的类路径?在进行类路径扫描时,它似乎正在使用每个包的类路径。有没有办法让类路径扫描使用扫描开始时所在包的类路径

编辑

正如Danail Nachev在下面所说的,当Spring进行类路径扫描时,它只发生在类路径所在的模块中。解决方法是使用:

  • 将每个模块的配置放在Spring3
    @Configuration
    bean中
  • 在顶级包中使用一个XML文件来初始化
    @Configuration
    bean
  • 在顶级
    @Configuration
    bean中,使用
    @Import
    导入其他配置文件
  • 确保清单中的
    Require Bundle
    ,以确保导入的配置可用

  • OSGi完全是模块化的,因此在捆绑包之间进行清晰的分离非常重要。若Spring可以在单个ApplicationContext下将它们联合起来,那个么它将和通常的Spring应用程序并没有什么不同,在Spring应用程序中,所有东西都可以在单个类路径中使用。像这样的

    发生的情况是,每个bundle都接收自己的ApplicationContext。这些ApplicationContexts可以使用OSGi服务注册表交换bean。您需要将bean标记为已导出,并在其他应用程序上下文中导入它们,否则它们彼此不可见

    这应该可以解释为什么不能用单个Spring上下文配置所有内容,并期望从一个包开始,它会找到所有bean。Spring上下文只扫描单个包,并且可以选择将bean作为OSGi服务导入/导出


    从这里解读:

    谢谢,这正是我们观察到的行为。我已经设法找到了一个无需使用类路径扫描的解决方法。