SpringDM笔记4-OSGi组件中使用Spring

1.Embedding the Spring application context within components

   SpringDM引入了一种新类型的Bundle:Spring-powered bundles,SpringDM会自动的给这种Bundle提供一个Spring

   应用上下文,此时可使用Spring框架所有的特性,如:依赖注入,AOP,事物管理等特性;同时该类型的Bundle可

   Import或Export Package而不需要考虑服务初始化,组件依赖等问题,应为SringDM提供了XML文件配置或基于

   Annotation的机制来实现这些工作。

2.Spring DM’s OSGi-aware application context:

   OsgiBundleXmlApplicationContext,OsgiBundleXmlWebApplicationContext,

   Bean Scope: singleton,prototype

   SpringDM新增加的Scope:Bundle,When a bundle-scoped bean is exported to the OSGi service registry, 

   one instance of the bean will be created for each bundle importing it.

3.Spring DM’s extender mechanisms

   The Spring DM extender is the equivalent of the ContextLoaderListener, which loads the root  

   application context of a web application.

   The Spring DM extender creates application contexts asynchronously, on a different thread than the

   one handling the bundle start event. This ensures that bundle startup is fast and doesn’t block the

   entire OSGi container. The asynchronous creation of application contexts is also vital to the ability of

   the extender to handle dependent bundles, especially if there are cycles. This is the default  

   behavior, and it can be overridden.
   The Spring DM extender also takes care of application context destruction when the corresponding  

   bundle is being shut down. In contrast to creation, destruction is done synchronously.

   实现SpringDM extend Bundle方式:

   ■ Contain a META-INF/spring directory with Spring XML files in it. Spring DM will scan all files with an 

      .xml extension in lexical order and try to create a single application context from them.
   ■ Contain a Spring-Context header in its META-INF/MANIFEST.MF file. The header value specifies the

      configuration file location and can use the Spring resource syntax.

4. 支持的Bundle类型

    Standard Spring-powered bundles 和 Web Bundles,其中SpringDM extend bundle支持OSGi Bundle, 

    SpringDM web extend bundle支持OSGi Web Bundle。

5. SpringDM OSGi's Namespace

    Spring DM provides an osgi namespace whose primary goal is to interact with the OSGi service

    registry. 有以下两种使用方法:

    (1) Spring DM osgi namespace declaration nested in Spring beans namespace:

    <?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">
    <osgi:service id="myServiceOsgi" ref="myService" interface="com.manning.sdmia.MyService" />
    </beans>

    (2)Spring DM osgi namespace declared as the top-level namespace:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans
    xmlns="http://www.springframework.org/schema/osgi"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/osgi
    http://www.springframework.org/schema/osgi/spring-osgi.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
    <service id="myServiceOsgi" ref="myService" interface="com.manning.sdmia.MyService" />
    </beans:beans>

6. The main Spring DM osgi namespace tags

    ■ service: Publishes a bean in the OSGi service registry
    ■ reference: Retrieves a service from the OSGi service registry
    ■ list/set: Retrieves a list or set of services published in the OSGi service registry, matching a  

    predefined criteria
    ■ bundle: Defines a bean representing a bundle object

猜你喜欢

转载自springsfeng.iteye.com/blog/1147996
今日推荐