一、重构DictQueryProject 删除Activator类
二、删除LocalDictQuery里面的Activator类,新建文件夹OSGI-INF,在此文件夹里面创建一个component.xml文件。
修改component.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?> <component name="DictQueryService"> <implementation class="com.grocal.localdictquery.local.impl.LocalDictQueryServiceImpl" /> <service> <provide interface="com.grocal.dictquery.query.QueryService" /> </service> </component>implementation标记:指定接口的实现类
service标记:接口的定义类
同理修改DictWeb里面的DictServlet,删除BoundContext相关的应用,修改后的DictServlet如下:
package com.grocal.dictweb.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.osgi.service.http.HttpService; import org.osgi.service.http.NamespaceException; import com.grocal.dictquery.query.QueryService; public class DictServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; private QueryService queryService; private HttpService httpService; public void setHttpService(HttpService httpService) { this.httpService = httpService; try { httpService.registerServlet("/dict/queryServlet", this, null, null); httpService.registerResources("/dict/page", "page", null); System.out.println("注册service /dict/page/dictquery.html"); } catch (ServletException e) { e.printStackTrace(); } catch (NamespaceException e) { e.printStackTrace(); } } public void unsetHttpService(HttpService httpService) { if (httpService != this.httpService) { return; } this.httpService.unregister("/dict/queryServlet"); this.httpService.unregister("/dict/page"); System.out.println("取消service注册"); this.httpService = null; } public void setQueryService(QueryService queryService) { this.queryService = queryService; } public void unsetQueryService(QueryService queryService) { if (queryService != this.queryService) { return; } this.queryService = null; } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); doGet(req, resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); String queryWord = req.getParameter("query_word"); resp.setContentType("text/html;charset=utf-8"); ServletOutputStream output = resp.getOutputStream(); if (queryService == null) { output.println("No available dictquery service"); output.close(); return; } try { output.println("Result is " + queryService.query(queryWord)); output.close(); return; } catch (Exception e) { output.println("Error occurs"); output.println(e.toString()); output.close(); return; } } }component的内容如下:
<?xml version="1.0" encoding="UTF-8"?> <component name="DictQueryServlet"> <implementation class="com.grocal.dictweb.servlet.DictServlet" /> <reference name="QueryService" interface="com.grocal.dictquery.query.QueryService" bind="setQueryService" unbind="unsetQueryService" policy="dynamic" cardinality="0..1"/> <reference name="HttpService" interface="org.osgi.service.http.HttpService" bind="setHttpService" unbind="unsetHttpService" policy="dynamic"/> </component>运行工程,在这里值得注意的一点是需要加入另外两个bundle,以及设置bundle的start level 为2
相关推荐
OSGI原理与最佳实践的完整版,共12章 第1 章OSGi 简介 第2 章OSGi 框架简介 第3 章基于Spring-DM 实现Petstore 第4 章基于Apache CXF 实现分布式Petstore 第5 章构建OSGI Bundle Repositor'y 第6 章OSGi 规范解读 ...
资源名称:OSGI原理与最佳实践内容简介:国内第一本OSGi图书OSGi国内推广者林昊多年经验的结晶涵盖OSGi从入门到深入的知识体系引领OSGi国内研究和普及本书基于作者多年使用OSGi的经验而编写,涵盖了OSGi从入门到深入...
OSGi原理与最佳实践完整版,OSGI简介 OSGI框架简介 基于Spring-DM实现Petstore
OSGi原理与最佳实践.pdf .
OSGi原理与最佳实践(完整版下载),找了好久,绝对是完整版
OSGi原理与最佳实践例子 pdf书籍
InfoQ - OSGi原理与最佳实践精选版.zip
OSGi原理与最佳实践完整版_源码
OSGi原理与最佳实践(两章那种)与源代码
OSGi原理与最佳实践(完整版).PDF
本书基于作者多年使用OSGi的经验而编写,涵盖了...最后对OSGi知识进行深入讲解,通过对OSGi规范和实现框架(Equinox、Felix、Spring-DM和Apache CXF)的分析,以及最佳实践的介绍,帮助读者更好地掌握如何使用OSGi。
包括OSGi原理与最佳实践完整版、精简版以及书中的源代码
OSGi原理与最佳实践基于作者多年使用0SGi的经验而编写,涵盖了0SGi从/kfqN深入的知识体系,从OSGi的简介开始,介绍OSGi的作用及基本概念;其后进入OSGi实战,结合实例讲解如何基于OSGi框架编写模块化、动态化的各种...
本书基于作者多年使用0SGi的经验而编写,涵盖了0SGi从入门到深入的知识体系,从OSGi的简介开始,介绍0SGi的作用及基本概念;其后进入OSGi实战。
网上收集的OSGI资料. 包括: OSGi原理与最佳实践(精选版).pdf OSGI实战和源码.rar osgi进阶.pdf Introduce.OSGi.ppt OSGi.in.action.ppt r4.cmpn.pdf r4.core.pdf r4.enterprise.pdf