關於headers的細節參見OSGi規范中的節一些OSGi實現框架可能會支持一些與眾不同的jar包但是這些jar包關於OSGi的格式還是不變的
The Spring extender recognizes a bundle as Springpowered and will create an associated application context when the bundle is started if one or both of the following conditions is true:
如果滿足以下兩個條件Spring extender會通過一個bundle的具有Springpowered的驗證當這個bundle啟動時會為其創建一個相關聯的application context
The bundle classpath contains a folder METAINF/spring with one or more files in that folder with a xml extension
這個bundle的classpath包含一個目錄METAINF/spring這個目錄中有一個或多個擴展名為xml的文件
METAINF/MANIFESTMF contains a manifest header SpringContext
這個bundle中的METAINF/MANIFESTMF文件包含一個名為SpringContext的header
In addition if the optional SpringExtenderVersion header is declared in the bundle manifest then the extender will only recognize bundles where the specified version constraints are satisfied by the version of the extender bundle (BundleVersion) The value of the SpringExtenderVersion header must follow the syntax for a version range as specified in section of the OSGi Service Platform Core Specification
另外如果bundle的manifest聲明了可選header SpringExtenderVersion那麼extender將僅僅認可與之版本相符的指定版本約束的bundle(bundle的BundleVersion)SpringExtenderVersion的值必須遵循OSGi規范節中指定的版本范圍
In the absence of the SpringContext header the extender expects every xml file in the METAINF/spring folder to be a valid Spring configuration file and all directives (see below) take on their default values
在缺少SpringContext的情況下spring extender仍然認為所有METAINF/spring中xml的文件都是spring的配置文件所有的header都是默認值
An application context is constructed from this set of files A suggested practice is to split the application context configuration into at least two files named by convention modulenamecontextxml and modulenameosgicontextxml The modulenamecontextxml file contains regular bean definitions independent of any knowledge of OSGi The modulenameosgicontextxml file contains the bean definitions for importing and exporting OSGi services It may (but is not required to) use the Spring Dynamic Modules OSGi schema as the toplevel namespace instead of the Spring beans namespace
application context就是根據這些文件(METAINF/spring中xml的文件)構造的一個推薦的做法是將application context配置文件至少分割成個文件習慣上命名為 [模塊名-contextxml] 和[模塊名osgicontextxml]modulenamecontextxml文件包含了不依賴與OSGi相關的bean的定義(可以理解為普通bean定義)modulenameosgicontextxml文件則定義那些引入或輸出OSGi服務的bean這可能需要使用Spring Dynamic Modules的OSGi schema做為頂級命名空間取代spring的bean命名空間但這不是必須的
以下是 SpringContext的相關內容和配置主要意思如下
如果在SpringContext指定了配置文件那麼extender將忽略 METAINF/spring 中的配置文件除非明確指定
可以用通配符例如SpringContext: osgi*;
createasynchronously=false(默認值是true) 使用同步方式創建該bundle的application context有一點需要注意同步創建application context的過程是在OSGi的事件線程中進行的它將阻塞這個線程的事件發送直到完成application context的初始化如果這個過程中發生了錯誤那麼將出現一個FrameworkEventERROR但是bundle的狀態仍然還是ACTIVE
waitfordependencies和timeout從字面意思就能看出來了
publishcontext:=false(默認值是true)不將application context作為一個服務發布
The SpringContext manifest header may be used to specify an alternate set of configuration files The resource paths are treated as relative resource paths and resolve to entries defined in the bundle and the set of attached fragments When the SpringContext header defines at least one configuration file location any files in METAINF/spring are ignored unless directly referenced from the SpringContext header
The syntax for the SpringContext header value is:
SpringContextValue ::= context ( context ) * context ::= path ( ; path ) * (; directive) *
This syntax is consistent with the OSGi Service Platform common header syntax defined in section of the OSGi Service Platform Core Specification
For example the manifest entry:
SpringContext: config/accountdatacontextxml config/accountsecuritycontextxml
will cause an application context to be instantiated using the configuration found in the files accountdatacontextxml and accountsecuritycontextxml in the bundle jar file
A number of directives are available for use with the SpringContext header These directives are:
createasynchronously (false|true): controls whether the application context is created asynchronously (the default) or synchronously
For example:
SpringContext: *;createasynchronously=false
Creates an application context synchronously using all of the *xml files contained in the METAINF/spring folder
SpringContext: config/accountdatacontextxml;createasynchrously:=false
Creates an application context synchronously using the config/accountdatacontextxml configuration file Care must be taken when specifying synchronous context creation as the application context will be created on the OSGi event thread blocking further event delivery until the context is fully initialized If an error occurs during the synchronous creation of the application context then a FrameworkEventERROR event is raised The bundle will still proceed to the ACTIVE state
waitfordependencies (true|false): controls whether or not application context creation should wait for any mandatory service dependencies to be satisfied before proceeding (the default) or proceed immediately without waiting if dependencies are not satisfied upon startup
For example:
SpringContext: config/osgi*xml;waitfordependencies:=false
Creates an application context using all the files matching osgi*xml in the config directory Context creation will begin immediately even if dependencies are not satisfied This essentially means that mandatory service references are treated as though they were optional clients will be injected with a service object that may not be backed by an actual service in the registry initially See section for more details
timeout (): the time to wait (in seconds) for mandatory dependencies to be satisfied before giving up and failing application context creation This setting is ignored if waitfordependencies:=false is specified The default is minutes ( seconds)
For example:
SpringContext: *;timeout:=
publishcontext (true|false): controls whether or not the application context object itself should be published in the OSGi service registry The default is to publish the context
For example:
SpringContext: *;publishcontext:=false
If there is no SpringContext manifest entry or no value is specified for a given directive in that entry then the directive takes on its default value
Required Spring Framework and Spring Dynamic Modules Bundles
The Spring Dynamic Modules project provides an number of bundle artifacts that must be installed in your OSGi platform in order for the Spring extender to function correctly:
Spring Dynamic Modules提供了很多現成的bundle要使用Spring extender必須將這些bundle安裝到OSGi平台中:
The extender bundle itself orgspringframeworkosgiextender
The core implementation bundle for the Spring Dynamic Modules support orgsprire
The Spring Dynamic Modules i/o support library bundle orgspringframeworkosgiio
In addition the Spring Framework provides a number of bundles that are required to be installed As of release of the Spring Framework the Spring jars included in the Spring distribution are valid OSGi bundles and can be installed directly into an OSGi platform The minimum required set of bundles is:
另外spring還提供了很多它自己的bundle這些bundle可以直接被安裝到OSGi平台中要使用spring的最低要求需要以下幾個bundle:
springcorejar (bundle symbolic name orgre)
springcontextjar (bundle symbolic name orgntext)
springbeansjar (bundle symbolic name orgspringframeworkbeans)
springaopjar (bundle symbolic name orgspringframeworkaop)
In additional the following supporting library bundles are required OSGiready versions of these libraries are shipped with the Spring Dynamic Modules distribution
以下幾個bundle是spring的依賴bundle可以在Spring Dynamic Modules的分發包中找到它們:
aopalliance
backportutil (for JDK )
cglibnodep
commonslogging (SLFJ version highly recommended)
Importing and Exporting packages
Refer to the OSGi Service Platform for details of the ImportPackage and ExportPackage manifest headers Your bundle will need an ImportPackage entry for every external package that the bundle depends on If your bundle provides types that other bundles need access to you will need ExportPackage entries for every package that should be available from outside of the bundle
這部份是關於OSGi平台MANIFEST中ImportPackage和ExportPackage的細節描述
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28456.html