× 注册服务
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); } }