1.注册一个自定义的服务:
1.定义一个服务,接口以及实现类。
2.把服务注册到OSGI框架中,OSGi 框架提供了两种注册方式,都是通过 BundleContext
类实现的:
-
registerService(String,Object,Dictionary)
注册服务对象 object
到接口名 String
下,可以携带一个属性字典Dictionary
;
-
registerService(String[],Object,Dictionary)
注册服务对象 object
到接口名数组 String[]
下,可以携带一个属性字典 Dictionary
,即一个服务对象可以按照多个接口名字注册,因为类可以实现多个接口;
3.获取服务,OSGi 框架提供了两种获取服务的引用 ServiceReference
的方法:
-
getServiceReference(String)
:根据接口的名字得到服务的引用;
-
getServiceReferences(String,String)
:根据接口名和另外一个过滤器名字对应的过滤器得到服务的引用;
上面的注册服务应该跟获取服务的key保存一致,注册和获取都可以在插件的入口类的start方法中执行。
注册服务的代码:
public void start(BundleContext context) throws Exception {
System.out.println("hello world");
context.registerService(
IHello.class.getName(),
new DefaultHelloServiceImpl(),
null);
}
获取服务的代码:
public void start(BundleContext context) throws Exception {
System.out.println("hello world2");
/**
* Test hello service from bundle1.
*/
IHello hello1 =
(IHello) context.getService(
context.getServiceReference(IHello.class.getName()));
System.out.println(hello1.getHello());
}
分享到:
相关推荐
赠送jar包:osgi-resource-locator-1.0.1.jar; 赠送原API文档:osgi-resource-locator-1.0.1-javadoc.jar; 赠送源代码:osgi-resource-locator-1.0.1-sources.jar; 赠送Maven依赖信息文件:osgi-resource-locator...
maven-osgi-plugin-launcher-framework-equinox-1.0.15.jar
Spring OSGi是Spring框架与OSGi(开放服务网关倡议)技术的结合,它为开发者提供了在OSGi环境中使用Spring的能力。OSGi是一种Java模块化系统,旨在解决大型软件系统的复杂性问题,而Spring OSGi则将Spring的依赖注入...
赠送jar包:osgi-resource-locator-1.0.1.jar; 赠送原API文档:osgi-resource-locator-1.0.1-javadoc.jar; 赠送源代码:osgi-resource-locator-1.0.1-sources.jar; 赠送Maven依赖信息文件:osgi-resource-locator...
【标题】基于Eclipse的Equinox框架开发OSGi Bundle应用 在Java世界中,OSGi(Open Services Gateway Initiative)是一种模块化系统,它允许开发者创建可独立更新和依赖管理的模块,即Bundle。Eclipse的Equinox是...
spring-osgi-1.2.1-with-dependencies.zip spring-osgi-1.2.1-with-dependencies.zip spring-osgi-1.2.1-with-dependencies.zip
【压缩包子文件的文件名称】"org.eclipse.osgi-3.7.0.v20110613-sources.jar" 是这个版本的源代码包,包含有OSGi框架的源代码,这对于开发者理解和调试代码,或者进行自定义扩展非常有用。 **详细知识点:** 1. **...
1. **依赖管理**:在OSGi中,每个Bundle声明其依赖的服务和库,框架负责解析并提供这些依赖。如果某个依赖不可用,Bundle将不会被激活,避免了运行时的类加载问题。 2. **版本管理**:OSGi支持多个版本的同一服务...
在“org.eclipse.osgi-3.7.2”中,主要包含实现OSGi运行时环境所需的类库和服务,如启动、管理bundle的生命周期、处理bundle间的依赖关系等。 在Eclipse OSGi框架下,每个bundle都有自己的类加载器,这使得bundle...
在Eclipse中,最小的模块化单位是插件(Plugin),在很多情况下,插件(Plugin)和模块(Bundle)这两个术语可以互换使用。 OSGi模块和依赖性部分介绍: 1. OSGi模块:一个OSGi模块是一个单独的代码单元,它可以被...
总结来说,“spring-osgi-1.2.1-with-dependencies”是一个集成了Spring与OSGi的完整包,它提供了在OSGi环境中运行Spring应用所需的所有组件和服务。通过理解和掌握这个包,开发者可以更好地利用OSGi的模块化优势,...
Eclipse Equinox 是 Eclipse 平台的核心部分,它实现了 OSGi R4 规范中的核心框架和服务层。Equinox 是一种开源的 OSGi 实现,广泛应用于各种 Java 应用程序中,特别是 Eclipse 工具集的基础。它支持模块化架构,...
maven-osgi-plugin-launcher-framework-equinox-1.0.19.jar
maven-osgi-plugin-launcher-framework-equinox-1.0.18.jar
maven-osgi-plugin-launcher-framework-equinox-1.0.17.jar
maven-osgi-plugin-launcher-framework-equinox-1.0.16.jar
maven-osgi-plugin-launcher-framework-equinox-1.0.14.jar
maven-osgi-plugin-launcher-framework-equinox-1.0.13.jar