使用Spring在OSGi包中找到带注释的类?

时间:2013-10-01 10:33:59

标签: spring annotations osgi spring-dm

我想在OSGi包中找到所有使用我的自定义注释注释的类,
实际扫描必须在捆绑包本身内部进行,或者从另一个捆绑包进行,实际扫描 我尝试过使用ClassPathScanningCandidateComponentProvider,但它会导致异常:

java.io.FileNotFoundException: URL [bundleresource://38.fwk29597962/my/base/package/] cannot be resolved to absolute file path because it does not reside in the file system: bundleresource://38.fwk29597962/my/base/package/

我99%肯定,像reflections或其他扫描库这样的解决方案也不起作用,现在不记得为什么

我可以像这样配置组件扫描:

<context:annotation-config />
<context:component-scan base-package="my.base.package" use-default-filters="false">
    <context:include-filter type="annotation" expression="path.to.my.annotation"/>
</context:component-scan>

并获得我需要的bean工厂,但这需要将@Scope(“prototype”)添加到我所有注释的类中,因此spring默认情况下不会创建单例等。
任何更好的解决方案?

-my带注释的类没有@Component或与spring相关的任何内容 -osgi框架(eclipse equinox 3.8)嵌入在Web应用程序中 - 使用弹簧3.2.3和gemini-blueprint 2.0.0.M02

1 个答案:

答案 0 :(得分:3)

如果您准备在构建时而不是在运行时进行扫描,那么bnd有一个相当不错的宏可以使用:

MyAnnotated-Classes: ${classes;CONCRETE;ANNOTATION;org.example.MyAnnotation}

...这将在清单中生成一个MyAnnotated-Classes头,其中列出了包中@MyAnnotation的所有类。在运行时扫描此标题现在很简单。

这种方法是对运行时类路径扫描的重要优化。此外,运行时扫描可能不可靠,因为您需要捕获Bundle-ClassPath的内容,但要避免任何导入/必需的类。