阅读 springdm in action 笔记--Spring DM extenders
2011-01-27 11:25
369 查看
本文适合对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 资源定位策略
例子:
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
一个简单的办法是实现一个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-Packageheader
■ 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环境:
META-INF/MANIFEST.MF
Manifest-Version: 1.0 #标记受保护的地址,该地址只能被程序本身访问到 Private-Package: .,WEB-INF Bundle-Version: 1.0.0 Build-Jdk: 1.6.0_22 Built-By: john Tool: Bnd-0.0.311 Bnd-LastModified: 1296090877032 Bundle-Name: springdm-web-sample Bundle-ManifestVersion: 2 Created-By: Apache Maven Bundle Plugin #标记web程序的根目录 Web-ContextPath: springdm-web-sample Bundle-SymbolicName: com.manning.sdmia.springdm-web-sample
通过Spring-Context可以改变spring配置文件的默认路径,多个文件以逗号分隔
Spring-Context: config/application-context.xml, config/app-context-2.xml Spring-Context: config/*.xml Spring-Context: config/**/*.xml可以递归cofig下所有目录中的xml文件
通过osgi:service可以发布一个osgi服务,注册到osgi registry中
<bean id="myService" class="com.manning.sdmia.impl.MyServiceImpl"/> <osgi:service ref="myService" interface="com.manning.sdmia.MyService"/>
通过osgi:reference可以引用一个osgi服务到本地bundle中
<osgi:reference id="myService" interface="com.manning.sdmia.MyService"/> <bean id="webController" class="com.manning.sdmia.web.MyController"> <property name="service" ref="myService"/> </bean>
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. |
Spring-Context: osgibundle:config/application-context.xml <bean id="someBean" class="com.manning.sdmia.SomeBean"> <property name="resource" value="classpath:help/section1.html" /> </bean>
wait-for-dependencies:=false 延迟创建application context 知道他依赖的服务都是有效状态
Spring-Context: config/application-context.xml;wait-for-dependencies:=false
TIMEOUT
如果一个dependency(暂时翻译为依赖)丢失,可能严重危机application context创建,可以通过设置timeout指令来避免application context创建过程被长期挂起,通过下面的设置,application context在尝试创建失败之前
,等待他的强制依赖( mandatory dependencies)120秒。timeout仅仅对 mandatory dependencies有效,当设置wait-for-dependencies:false时该指令将被忽略
Spring-Context: config/application-context.xml;timeout:=120
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 |
DM 将自动发现它并增加到被管理的监听列表中.见如下代码
package com.manning.sdmia.ch04; import org.springframework.osgi.context.event.OsgiBundleApplicationContextEvent; import org.springframework.osgi.context.event.OsgiBundleApplicationContextListener; import org.springframework.osgi.context.event.OsgiBundleContextClosedEvent; import org.springframework.osgi.context.event.OsgiBundleContextRefreshedEvent; public class ApplicationContextObserver implements OsgiBundleApplicationContextListener<OsgiBundleApplicationContextEvent> { private transient int countRefreshed = 0; private transient int countClosed = 0; public void onOsgiApplicationEvent(OsgiBundleApplicationContextEvent evt) { if(evt instanceof OsgiBundleContextRefreshedEvent) { countRefreshed++; } else if(evt instanceof OsgiBundleContextClosedEvent) { countClosed++; } } public int getCountRefreshed() { return countRefreshed; } public int getCountClosed() { return countClosed; } }
在spring配置文件声明该监听
<?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:osgi="http://www.springframework.org/schema/osgi" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd"> <bean id="observer" class="com.manning.sdmia.ch04.ApplicationContextObserver" /> <osgi:service ref="observer" interface=" org.springframework.osgi.context.event.OsgiBundleApplicationContextListener"/> </beans>
在spring bean中使用OSGI BUNDLE CONTEXT
方法一:实现一个 implementing BundleContextAware的bean并实现setBundleContext方法
import org.osgi.framework.BundleContext; import org.springframework.osgi.context.BundleContextAware; public class OsgiAddict implements BundleContextAware { public void setBundleContext(BundleContext bundleContext) { ... } }
<bean id="osgiAddict" class="com.manning.sdmia.ch04.OsgiAddict" />
方法二:
import org.osgi.framework.BundleContext; public class PojoOsgiAddict { private BundleContext bundleContext; public void setBundleContext(BundleContext bundleContext) { this.bundleContext = bundleContext; } }
必须明确的注入bundleContext
<bean id="pojoOsgiAddict" class="com.manning.sdmia.ch04.PojoOsgiAddict"> <property name="bundleContext" ref="bundleContext" /> </bean>
简单的web程序结构:
my-web-bundle META-INF/ MANIFEST.MF spring/ application.xml WEB-INF/ web.xml
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-Packageheader
■ 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环境:
<context-param> <param-name>contextClass</param-name> <param-value>org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/applicationContext.xml,WEB-INF/applicationContext-osgi.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
相关文章推荐
- Two-Stream RNN/CNN for Action Recognition in 3D Videos-阅读笔记
- Spring in action 笔记
- Spring in Action(第三章 最小化 Spring XML配置)学习笔记
- SPRING IN ACTION 第4版笔记-第二章-001-用@Autowired\@ComponentScan、@Configuration、@Component实现自动装载bean
- SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-007-以set方法注入<property>\p-namespace\util-space
- SPRING IN ACTION 第4版笔记-第十章Hitting the database with spring and jdbc-001-Spring对原始JDBC的封装
- Spring In Action 4 学习笔记(一)Spring概览
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-005-Bean的作用域@Scope、ProxyMode
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-009-用SPEL给bean运行时注入依赖值
- mahout in action推荐系统阅读笔记(7)
- SPRING IN ACTION 第4版笔记-第五章Building Spring web applications-001-SpringMVC介绍
- End-to-end learning of action detection from frame glimpses in videos 阅读笔记
- [论文阅读笔记]Two-Stream Convolutional Networks for Action Recognition in Videos
- spring in action 学习笔记(二)通过构造器注入对象的引用
- Hibernate in action阅读笔记-吵吵冷饭-持久化对象
- SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-004-以query parameters的形式给action传参数(@RequestParam、defaultValue)
- Spring in Action 学习笔记一
- spring in action 学习笔记三:对spring 容器的理解,以及如何利用AnnotationConfigApplicationContext这个容器创建对象
- Spring in Action 学习笔记—第一章 开始Spring之旅(转贴)
- SPRING IN ACTION 第4版笔记-第九章Securing web applications-009-拦截请求()