SpringDM笔记19-SpringDM如何处理OSGi应用的动态行为

    ServiceTracker

1. Dealing with the appearance or disappearance of services

    (1)DEALING WITH AN INDIVIDUAL SERVICE REFERENCE:

    <bean id="contactDao" class="com.manning.sdmia.directory.dao.jdbc.ContactDaoJdbc">
        <property name="dataSource" ref="dataSource" />
    </bean>

 

    <osgi:service  id="contactDaoOsgi" interface="com.manning.sdmia.directory.dao.ContactDao"
        ref="contactDao" />

 

    <osgi:reference id="contactDao" interface="com.manning.sdmia.directory.dao.ContactDao">
    <bean id="contactService" class="com.manning.sdmia.directory.service.impl.ContactServiceImpl">
           <property name="contactDao" ref="contactDao" />
    </bean>

    如果业务服务Bundle不包含用户的业务服务,但需要关键的业务服务;而此时DAO的依赖不满足,可按照下面的方法

    解 决:

    <osgi:reference id="contactDao" interface="com.manning.sdmia.directory.dao.ContactDao"
            availability="optional" />

    (2)DEALING WITH A COLLECTION OF SERVICE REFERENCES:

    Spring DM populates the collections as needed. It adds matching services to the collection, and

    when one of the services referenced in the collection is unregistered from the OSGi registry, Spring

    DM automatically removes the reference from the collection. Note, though, that this is only valid for

    collections (java.util.List and java.util.Set) that are managed by Spring DM. Indeed, Spring DM can’t

    track service appearances and disappearances and update collections that it doesn’t totally control.

 

    This is good news: we can use our collections of service references as any other collections. We

    usually use collections by iterating over them, using Iterators, but there’s one important thing to

    know when iterating over a collection of service references managed by Spring DM: even the

    Iterator is dynamic. Imagine you start iterating over a collection of service references, and its

    content changes during the iteration because some services were unregistered and some matching

    services were registered. With Spring DM, you’ll be aware of this immediately, because the Iterator
    will reflect these changes dynamically.

    (3)REACTING TO THE APPEARANCE AND DISAPPEARANCE OF SERVICES

    参考SpringDM_Paint Application.zip

2. Providing a new version of a component

    For example, when updating the DAO API bundle, we’ll have to go through these steps:
    (1)Stop the DAO API, DAO implementation, and service implementation bundles.
    (2)Install the new versions.
    (3)Update the bundles.
    (4)Refresh the DAO API bundle (the OSGi framework will then automatically refresh all the bundles

    that import Java packages exported by the DAO API bundle).
    (5)Start the DAO API bundle.

3. 附件SpringDM_Paint Application.zip

 

你可能感兴趣的:(spring)