BlueprintListener的使用

本文介绍如何在OSGi环境中使用BlueprintListener监听Blueprint容器事件。通过实现BundleActivator接口并在MANIFEST.MF中指定,可以创建并注册一个匿名内部类作为BlueprintListener。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

OSGI里面有很多种Listener,添加一个Listener一般是调用BundleContext里面的addListener,比如

BundleContext.addBundleListener();
BundleContext.addFrameworkListener();
BundleContext.addServiceListener();

但是,BlueprintListener有点不一样

下面有个简单的用法

package com.pp;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.blueprint.container.BlueprintListener;

public class BlueprintListenerExample implements BundleActivator
{
	private ServiceRegistration<BlueprintListener> registration = null;
	
	public void start(BundleContext context) throws Exception
	{
		BlueprintListener blueprintListener = (event) -> {
			System.out.println("BlueprintListener " + event.getBundle().getSymbolicName() + ", type=" + event.getType());
		};
		
		registration = context.registerService(BlueprintListener.class, blueprintListener, null); 
	}

	public void stop(BundleContext context) throws Exception
	{
		registration.unregister();
	}
}


然后,在MANIFEST.MF文件里面增加Bundle-Activator:com.pp.BlueprintListenerExample
这样,就添加了一个BlueprintListener
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值