`
erylee
  • 浏览: 108311 次
社区版块
存档分类
最新评论

OpenCore: OSGi上实现IoC容器

阅读更多
Martin Fowler有一篇文章里称:说一个面向对象系统框架或结构是基于IoC的,就好像说汽车有四个轮子一样。非常精辟 ,IoC或者DI更多的是一个设计原则,一个设计良好的面向对象的系统或多或少都有这方面的特点,这与是否有一个IoC容器的关系不大,IoC容器的作用 是简化开发,强制系统遵循IoC原则而已。

OpenCore在最初设计的时候,没有考虑IoC容器的问题,主要是解决服务端的WEB开发、数据库访问、与REST远程通信等问题,我们完全使用 OSGi本身的服务注册机制,在插件启动时用代码实现依赖注射。直到今年8月份左右,我们把依赖注射的职责分离出来,在OSGi上实现了一个分级的IoC 容器,以简化开发,避免在插件启动类写依赖注射的代码。

OpenCore IoC容器有下面几个特性:
  1. 完全基于OSGi的插件体系结构。
  2. 简单,远没有Spring IoC容器复杂,支持属性注射(Property Injection)与构造函数注册(Constructor Injection)。
  3. 分级IoC容器,分为插件级、应用程序级、网络级
  4. IoC容器管理的服务与OSGi框架管理的服务可以互访,这样保证了OpenCore IoC不会屏蔽OSGi本身的模型,使得基于OpenCore IoC的插件与大量第三方基于完全基于OSGi服务模型的插件可以协作。
  5. 支持通过动态扩展点实现1对多的依赖注册机制。
OpenCore IoC的实现机制:

1. 每个插件(bundle)增加一个IoC的自描述文件META-INF/OPENCORE.XML,例如org.opengoss.web.core中,该文件的配置如下:

<plugin version="1.0" id="WebCore"></plugin>
xml 代码
 
  1. <plugin id="WebCore" version="1.0">  
  2.   
  3.     <service id="WebServerConfiguration" scope="application" class="org.opengoss.web.internal.WebServerConfiguration">  
  4.   
  5.         <interface name="org.opengoss.web.core.IWebServerConfiguration"/>  
  6.   
  7.     </service>  
  8.   
  9.     <service id="MarshallerRegistry" scope="application" class="org.opengoss.web.internal.MarshallerRegistry">  
  10.   
  11.         <interface name="org.opengoss.web.service.IMarshallerRegistry"/>  
  12.   
  13.     </service>  
  14.   
  15.     <service id="WSContainer" scope="application" class="org.opengoss.web.internal.WSContainer">  
  16.   
  17.         <interface name="org.opengoss.web.service.IWSContainer"/>  
  18.   
  19.         <property name="marshallerRegistry" ref="MarshallerRegistry"/>  
  20.   
  21.     </service>  
  22.   
  23.     <service id="WSExporter" scope="application" class="org.opengoss.web.internal.WSExporter">  
  24.   
  25.         <interface name="org.opengoss.web.service.IWSExporter"/>  
  26.   
  27.         <constructor>  
  28.   
  29.             <param ref="WSContainer"/>    
  30.   
  31.         </constructor>  
  32.   
  33.     </service>  
  34.   
  35.     <extension-point id="Marshaller" target="MarshallerRegistry"  
  36.   
  37.         bindMethod="addMarshaller" unbindMethod="removeMarshaller"/>  
  38.   
  39.     <extension-point id="WebService" target="WSExporter"   
  40.   
  41.         bindMethod="export" unbindMethod="unexport"/>  
  42.   
  43.     <extension-point id="Class" target="MarshallerRegistry"  
  44.   
  45.         bindMethod="addAliasClass" unbindMethod="removeAliasClass"/>  
  46.   
  47. </plugin>  


2. 插件的激活类(Activator)不是直接实现OSGi本身的"BundleActivator"接口,而是继承由OpenCore提供的 "org.opengoss.core.PluginActivator",启动时,PluginActivator负责完成XML文件解析与依赖注射。 例如org.opengoss.web.core中的Activator代码如下:

java 代码
 
  1. import org.opengoss.core.IPluginContext;  
  2. import org.opengoss.core.PluginActivator;  
  3. /** 
  4.  * Plugin activator. 
  5.  * 
  6.  * @author Ery Lee(ery.lee@gmail.com) 
  7.  * @version 1.0 2006-11-20 
  8.  * @since 1.0 
  9.  */  
  10. public class Activator extends PluginActivator {  
  11.    
  12.     @Override  
  13.     protected void startPlugin(IPluginContext pluginContext)  
  14.             throws Exception {  
  15.     }  
  16.    
  17.     @Override  
  18.     protected void stopPlugin(IPluginContext pluginContext)  
  19.             throws Exception {  
  20.     }  
  21.    
  22. }   


3. 我们注意到上的的服务配置中有"scope"的属性,该属性定义一个服务的使用范围,确定一个服务注册到哪一级IoC容器。

4. 另外一点,我们在IoC基础上增加了动态扩展点功能,实现多对1的注射。

OpenCore整个项目代码发布在www.sf.net/projects/opengoss与code.google.com/p/opengoss下。
分享到:
评论
2 楼 erylee 2007-02-11  
吸血鬼 写道
大哥大,sf和google上都不能下啊!


download the sample project from: http://code.google.com/p/linktalk

The core bundle is "org.opengoss.core.osgi" that implements the simple IoC container.

We are still busy on this project but there is some different ideas on how to evolve the opencore platform.
1 楼 吸血鬼 2007-02-11  
大哥大,sf和google上都不能下啊!

相关推荐

Global site tag (gtag.js) - Google Analytics