OSGi中的Spring组件扫描一无所获

Art*_*son 5 integration spring osgi

使用Spring-Context MANIFEST定义,我正在尝试component-scan搜索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>
Run Code Online (Sandbox Code Playgroud)

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

我相信这种情况正在发生,因为我们正在扫描的类路径是不同的捆绑包.这些bundle不直接导入包含Spring注释的类的包.令人困惑的是,为什么Spring没有选择从组件扫描开始的主包的类路径?在进行类路径扫描时,似乎正在使用每个bundle的类路径.有没有办法让类路径扫描使用扫描开始的包的类路径?

编辑

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

  1. 将每个模块的配置放在Spring 3 @Configurationbean中.
  2. 在顶级包中使用初始化@Configurationbean 的XML文件.
  3. 在顶级@Configurationbean中使用@Import导入其他配置文件.
  4. 确保Require-Bundle在您的MANIFEST中确保您导入的配置可用.

Dan*_*hev 7

OSGi完全是模块化的,因此在捆绑包之间进行明确的分离是非常重要的.如果Spring可以在单个ApplicationContext下将它们联合起来,那么与通常的Spring应用程序不同,它们在单个类路径中都可用.像这样的东西.

发生的事情是每个bundle都会收到自己的ApplicationContext.这些ApplicationContexts可以使用OSGi Service Registry交换bean.您需要将bean标记为已导出并将其导入其他ApplicationContexts,否则它们彼此不可见.

这可以解释为什么你不能用单个Spring上下文配置所有东西,并期望从一个bundle开始它会找到所有bean.Spring上下文仅扫描单个bundle,并且可以选择将bean导入/导出为OSGi服务.

从这里解释:第8章打包和部署基于Spring的OSGi应用程序