一般情况下,服务对象在注册后,任何其它的Bundle在请求该服务时,OSGi容器都是返回同一个对象。如果我们需要为每一个Bundle消费者返回不同的服务对象,或者,在真正需要该服务对象时才创建。对于这些情况,我们可以创建一个实现ServiceFactory接口的类,把该类的对象注册为服务(不是注册实际的服务对象),这样,当Bundle请求该服务时,ServiceFactory实现类将接管该请求,为每个Bundle新建一个实际的服务对象。以下是服务工厂的使用范例源码:
1、服务接口及其实现类
public interface HelloService {
public String sayHello(String name);
}
public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
return "Hello " + name;
}
}
2、服务工厂类
public class HelloServiceFactory implements ServiceFactory {
//当请求服务时,OSGi容器会调用该方法返回服务对象。
//当服务对象不为null时,OSGi框架会缓存这个对象,即对于同一个消费者,将返回同一个服务对象。
public Object getService(Bundle bundle, ServiceRegistration serviceRegistration) {
System.out.println("创建HelloService对象:" + bundle.getSymbolicName());
HelloService helloService = new HelloServiceImpl();
return helloService;
}
//当Bundle释放服务时,OSGi容器会调用该方法
public void ungetService(Bundle bundle, ServiceRegistration serviceRegistration, Object service) {
System.out.println("释放HelloService对象:" + bundle.getSymbolicName());
}
}
3、Bundle激活器类
public class Activator implements BundleActivator {
ServiceRegistration serviceRegistration;
public void start(BundleContext context) throws Exception {
//注册服务
HelloServiceFactory helloServiceFactory = new HelloServiceFactory();
serviceRegistration = context.registerService(HelloService.class.getName(), helloServiceFactory, null);
//获取服务(通过服务工厂取得)
ServiceReference serviceReference = context.getServiceReference(HelloService.class.getName());
HelloService selloService = (HelloService)context.getService(serviceReference);
System.out.println("1: " + selloService.sayHello("cjm"));
//第二次取得的服务对象与之前取得的是同一个对象
serviceReference = context.getServiceReference(HelloService.class.getName());
selloService = (HelloService)context.getService(serviceReference);
System.out.println("2: " + selloService.sayHello("cjm"));
}
public void stop(BundleContext context) throws Exception {
serviceRegistration.unregister();
}
}
分享到:
相关推荐
用ServiceFactory只能保证每个bundle获得不同的服务对象,当如果在同一个bundle中获得某个服务的多个对象就不行,还是获得同一个对象。 注册一个对象工厂,当需要获得一个服务对象时,生成该服务的对象,返回出去。
您可以创建自己的Magento服务实例,也可以使用Lusitanian OAuth库中的服务工厂,以确保将所有依赖项注入服务中: <?php use OAuth \ Common \ Storage \ Session ; use OAuth \ Common \ Consumer \ ...
ReflectionServiceFactoryBean serviceFactory = new ReflectionServiceFactoryBean(); serviceFactory.getServiceConfigurations().add(config); ClientProxyFactoryBean factory = new ...
通过oauth您可以方便的获取登录用户的信息以及服务商提供的其它资料和服务; ##目前支持的服务 baidu weibo qq (后续还在增加中) 使用方式 use Slince\OAuth\Certificate\Certificate; use Slince\OAuth\...
ServiceFactory 基于 XML 文件初始化服务并将所有 URL 与服务对象映射。 Facade 在所有验证完成后调用 servicDAO。 ServiceDAO调用Services.xml中配置的View名称Service DAO根据列和记录准备Json String返回给控制...
boolean flag = ServiceFactory.getVoteService().delete(gidNumber); if (flag) { req.getRequestDispatcher("/admin/goods?action=list").forward( req, resp); return; } req....
Eclipse中的将工作台部分...IWorkbenchPartFactory的两个方法:createView(String,Composite,IMemento,ServiceFactory)和 createEditor(String,Composite,IMemento,ServiceFactory负责创建Eclipse中工作台的抽象接口
客户关系系统,使用的技术:maven Git、前端原型:搭建的 HTML 页面、前端:Bootstrap(UI)+ jQuery、后端:SpringMVC + Spring + MyBatis 框架、工具包:UUIDUtil、ServiceFactory、DataTimeUtil 等、服务器:...
java工具包:可支持自动生成xml/mapper/service/controller/domain/serviceFactory等于项目中,不同于生成到本地文件再copy到对应目录的生成工具,可有效的减少机械式开发作业。 核心 基于项目架构:核心框架Spring ...
serviceFactory = ServiceFactory.getInstance(); JsapiService jsapiService = serviceFactory.getOpenService(JsapiService.class); JsapiTicket JsapiTicket = jsapiService.getJsapiTicket(accessToken, ...
工厂工厂 import FactoryInterface from '@chubbyjs/chubbyjs-container/dist/FactoryInterface' ; import Logger from 'some-logger/dist/Logger' ; import MinimalContainer from '@chubbyjs/chubbyjs-container/...
factory文件夹存储您需要实现以创建服务实例的ServiceFactory抽象类。 你的任务 实现service和factory接口以传递基本测试用例。 设计您的 (PostgreSQL) 数据库以满足接口的要求。 分析您的实施并找到加快速度的方法...
LeaRun.ServiceFactory --未用 LeaRun.WCFHost --未用 LeaRun.WCFLibrary --未用 LeaRun.WebService --接口层 LeaRun.Business --业务层 LeaRun.Entity --实体层 LeaRun.Repository --数据仓库层 LeaRun.WebApp --UI...
ILiceseNumberMsgSV sv = (ILiceseNumberMsgSV) ServiceFactory.getService(ILiceseNumberMsgSV.class); Map redMap = new HashMap(); Map result = (Map) sv.queryLicenseNumber(in); redMap.put("OUTDATA",...