本文适合对OSGI有一定了解和基础的研发人员,最初的想法是记录在学习过程中的知识点,以备将来查询。由于我e文水平实在有限,很多地方是直译或者凭着自己的理解来翻译,如果大家发现有不对的地方请指正。另外提供spingdm in action 英文版的下载地址:http://download.csdn.net/source/3002996
META-INF/MANIFEST.MF
通过Spring-Context可以改变spring配置文件的默认路径,多个文件以逗号分隔
通过osgi:service可以发布一个osgi服务,注册到osgi registry中
通过osgi:reference可以引用一个osgi服务到本地bundle中
Spring DM中OSGi 资源定位策略
OSGi search strategy | Prefixes | Description |
Class space | classpath: classpath*: | Search is delegated to the bundle classloader. The bundle itself, imported packages, and required bundles are then scanned using the same semantics as the Bundle.getResource method. |
JAR file | osgibundlejar: | Only the bundle itself is scanned, using the same semantics as the Bundle.getEntry method. |
Bundle space | osgibundle: | The bundle and its fragments (if any) are scanned. Delegates internally to the Bundle.findEntries method. This is Spring DM’s default resource-loading strategy. |
例子:
wait-for-dependencies:=false 延迟创建application context 知道他依赖的服务都是有效状态
TIMEOUT
如果一个dependency(暂时翻译为依赖)丢失,可能严重危机application context创建,可以通过设置timeout指令来避免application context创建过程被长期挂起,通过下面的设置,application context在尝试创建失败之前 ,等待他的强制依赖( mandatory dependencies)120秒。timeout仅仅对 mandatory dependencies有效,当设置wait-for-dependencies:false时该指令将被忽略
SPRING DM 事件机制
Application context events in Spring DM
Event class | Description |
---|---|
OsgiBundleContextRefreshedEvent; | Published when an application context is successfully started or refreshed |
OsgiBundleContextFailedEvent | Published when the creation of an application context fails |
OsgiBundleContextClosedEvent | Published when an application context is closed, usu- ally when a Spring-powered bundle is stopped |
一个简单的办法是实现一个implements OsgiBundleApplicationContextListener 的bean并且发布成OSGI service,Spring
DM 将自动发现它并增加到被管理的监听列表中.见如下代码
在spring配置文件声明该监听
在spring bean中使用OSGI BUNDLE CONTEXT
方法一:实现一个 implementing BundleContextAware的bean并实现setBundleContext方法
方法二:
必须明确的注入bundleContext
简单的web程序结构:
web bundle中类加载机制:
■ Its bundle space (classes in the JAR or WAR and classes from all the associated bundle fragments)
■ Classes from the packages imported with the Import-Package header
■ All the exported classes from bundles appearing in the Require-Bundle header
■ Bundle-Classpath: .,WEB-INF/classes,WEB-INF/lib/libA.jar, WEB-INF/lib/libB.jar
web.xml配置spring环境: