服务

× 注册服务

public void start(BundleContext context) throws Exception { //BundleContext context.registerService(clazz, service, properties); context.registerService(clazzes, service, properties); }

clazz 服务接口

service 服务实现

properties 如果服务接口相同,此属性区别


* 使用服务

import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; importorg.osgi.framework.BundleActivator; importorg.osgi.framework.BundleContext; importorg.osgi.framework.ServiceReference; importcom.javaworld.sample.service.HelloService; public class Activator implements BundleActivator { //服务的引用 ServiceReference helloServiceReference; public void start(BundleContext context)throws Exception { System.out.println("HelloWorld!!"); //获取服务的引用 helloServiceReference = context.getServiceReference(HelloService.class.getName()); //获取服务实例 HelloService helloService=(HelloService)context.getService(helloServiceReference); System.out.println(helloService.sayHello()); } public void stop(BundleContext context)throws Exception { System.out.println("Goodbye World!!"); //注销服务引用 context.ungetService(helloServiceReference); } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值