2016年6月29日星期三

Fuse_021:选择 Spring 还是 Blueprint ?

JBoss Fuse 内置支持两种依赖注入框架: Spring XML 和 Blueprint XML。
你可以使用任意一种, 或者两种都使用。
总的来说, Spring XML 和 Blueprint XML 提供的功能类似, 但 Blueprint 是一个更轻量级的框架, 由于 Blueprint 完全符合 OSGI 规范,因此更适合 OSGI 容器。
Spring 的配置文件位于 InstallDir/src/main/resources/META-INF/spring/*.xml。
Blueprint 的配置文件位于 InstallDir/src/main/resources/OSGI-INF/blueprint/*.xml。

Blueprint 强于 Spring 的另外一个特点是:运行时自动解决依赖。
使用 Blueprint 时,当通过 XML schema namespaces 加入新的依赖时, Blueprint 可以在运行时自动解决依赖,其原理如下:
当发现新的 XML schema namespaces 时,它搜索 classpath 中的 JAR 文件,看看谁实现了这个新的 namespace,并将其加载。
而使用 Spring 时,项目打包为 OSGi bundle 时,Spring 需要你在 maven-bundle-plugin 配置中显式增加依赖,这是因为 maven-bundle-plugin 不能通过扫描 Spring XML 文件中新增的 XML schema namespaces 自动发现依赖。

另外,Spring Dynamic Modules 可以简化应用和 OSGi container 之间的集成。Spring DM 特别提供了 XML elements 可以很容易的导出和消费 OSGi services。
但是,Spring DM 是 Spring Source(www.springsource.org/osgi/)提供的框架,目前已经处于死亡状态。
Blueprint 同样也可以简化应用和 OSGi container 之间的集成。Blueprint 也提供了 XML elements 可以很容易的导出和消费 OSGi services。

JBoss Fuse 6.2.1 版本的发布说明中明确指出:

SPRING DYNAMIC MODULES (SPRING-DM) IS DEPRECATED

Spring-DM (which integrates Spring XML with the OSGi service layer) is deprecated in 6.2.1 and you should use the Blueprint framework instead. Using Blueprint does not prevent you from using the Spring framework: the latest version of Spring is compatible with Blueprint.

结论:选择 Blueprint 作为 JBoss Fuse 依赖注入框架。

参考文献:
1. http://stackoverflow.com/questions/10008786/osgi-blueprint-vs-spring-dm
2. https://developer.jboss.org/thread/229349?tstart=0

没有评论: