您的位置:首页 > 编程语言 > Java开发

阅读 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

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 strategyPrefixesDescription
Class spaceclasspath:
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 fileosgibundlejar:Only the bundle itself is scanned, using the same semantics as
the Bundle.getEntry method.
Bundle spaceosgibundle: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 classDescription
OsgiBundleContextRefreshedEvent;Published when an application context is successfully
started or refreshed
OsgiBundleContextFailedEventPublished when the creation of an application context
fails
OsgiBundleContextClosedEventPublished when an application context is closed, usu-
ally when a Spring-powered bundle is stopped
一个简单的办法是实现一个implements OsgiBundleApplicationContextListener 的bean并且发布成OSGI service,Spring

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐