gpt4 book ai didi

OSGi中的Spring组件扫描什么也没找到

转载 作者:行者123 更新时间:2023-12-04 18:13:28 24 4
gpt4 key购买 nike

使用 Spring-Context MANIFEST definitions , 我正在尝试做一个 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>

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

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

编辑

正如 Danail Nachev 下面所说,当 Spring 进行类路径扫描时,它只发生在类路径发生的模块内。解决方法是使用:
  • 将每个模块的配置放入 Spring 3 @Configuration bean 。
  • 在您的顶级捆绑包中使用一个 XML 文件来初始化 @Configuration bean 。
  • 在顶层 @Configuration bean 使用@Import导入其他配置文件。
  • 确保 Require-Bundle在您的 list 中以确保您正在导入的配置可用。
  • 最佳答案

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

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

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

    从这里解释:Chapter 8. Packaging and Deploying Spring-based OSGi applications

    关于OSGi中的Spring组件扫描什么也没找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8331583/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com