SpringDM笔记11-Hard dependencies on the OSGi environment

    Any bean in a Spring DM application context can easily get a reference to its bundle context by implementing the BundleContextAware interface (located in the org.springframework.osgi.context

package). When instantiating the bean, the Spring DM application context implementation detects that it implements this special interface and automatically hands the bundle context to the bean by calling the sole method of the interface, setBundleContext .

1. 自动注入BundleContext

    public class OsgiAddict implements BundleContextAware {
      public void setBundleContext(BundleContext bundleContext) {
         ...
      }
    }

    <bean id="osgiAddict" class="com.manning.sdmia.ch04.OsgiAddict" />

2. Setter注入BundleContext

    public class PojoOsgiAddict {
        private BundleContext bundleContext;
        public void setBundleContext(BundleContext bundleContext) {
             this.bundleContext = bundleContext;
        }
    }

    <bean id="pojoOsgiAddict" class="com.manning.sdmia.ch04.PojoOsgiAddict">
         <property name="bundleContext" ref="bundleContext" />
    </bean>

3. Interact declaratively with bundles

    The Spring DM osgi namespace comes with a bundle element that makes it possible to get

    references to OSGi bundles and even manipulate them. By using the bundle element in an

    application context, you define an org.osgi.framework.Bundle bean in it. For example:

    <osgi:bundle id="springDmExtenderBundle" symbolic-

             name="org.springframework.osgi.extender" />

    <osgi:bundle id="myBundle" location="file:/my/bundle/repo/somebundle.jar" action="start" />

    Attributes of the bundle element:

    (1)

    Attribute name:id

    Values:Any XML-valid ID

    Description:Unique ID of the bean in the application context.

    (2)

    Attribute name:symbolic-name

    Values:Any valid OSGi symbolic-name

    Description:The symbolic name of an already installed bundle

    (3)

    Attribute name:location

    Values:Spring resource syntax

    Description:The location to load the bundle from, usually to interact with and call some lifecycle

    methods.

    (4)

    Attribute name:start-level

    Values:Integer

    Description:The bundle start level.

    (5)

    Attribute name:action

    Values:install, start, update, stop, uninstall

    Description: An action executed on the bundle. This uses the same semantics as the methods

    from the Bundle interface, with preconditions weakened.

    (6)

    Attribute name:

    Values:install, start, update,stop, uninstall

    Description:An action executed on the bundle when the application context is destroyed. This uses

    the same semantics as the methods from the Bundle interface, with preconditions weakened.

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值