作为一个新的事实上的工业标准,OSGi已经受到了广泛的关注,就在不久前EclipseCon也发布企业级OSGi标准,而IBM以及Eclipse也宣称将大力发展Java模块化。Spring是一个著名的轻量级Java EE开发框架,其特点是面向接口编程和非侵入式的依赖注入。

编辑推荐:OSGi入门与实践全攻略 Spring开源框架技术
将OSGi和Spring结合能充分发挥二者各自的特长,更好地满足企业级应用开发的需求。Spring开发组织在2008年发布了将OSGi和Spring结合的第一个版本:Spring-DM。
dmServer是一个完全模块化部署的,基于OSGi的Java服务器,为运行企业Java应用和Spring应用提供更加强大的灵活性和可靠性。SpringSource应用平台是构建在Spring、OSGi和ApacheTomcat之上的应用服务器,这个新的应用服务器摒弃了原有的JavaEE服务器标准,自然而然地将Spring编程模型展现其中,随之而来的还有一套基于OSGi内核构建的全新部署和打包系统。
实例教程:
一、.指定TargetPlatform到所用到的所有的bundle包的目录中。
二、创建一个Service接口bundle
新建一个接口类:com.infotech.test.common.ShowMsgInfo;
同时新加一个接口方法:publicStringGetMsgInfo();
打开这个接口bundle工程的MANIFEST.MS文件,在Runtime/ExprotedPackages中添加刚刚新建的接口类,使之对外提供这个服务。
三、创建一个接口bundle的实现bundle
打开这个接口bundle工程的MANIFEST.MS文件,在Dependencies/ImportedPackages中添加上面新建的接口类:
新建一个接口实现类:ShowMsgInfo:
在这个类中,实现接口中的方法:
- packagecom.infotech.test.service;
 - importcom.infotech.test.common.IShowMsgInfo;
 - publicclassShowMsgInfoimplementsIShowMsgInfo{
 - @Override
 - publicStringGetMsgInfo(){
 - return"HelloWord!!!";
 - }
 - }
 
接下来,我需要将这个实现类发布成为一个OSGI服务:在工程新一个目录OSGI-INF,并新建一个components.xml文档。
打开这个接口bundle工程的MANIFEST.MS文件,添加一行:
- Service-Component:OSGI-INF/components.xml
 
#p#
四、接下来,我们创建一个WEB应用bundle:
1.新建一个网页bundle工程:
2.在工程目录中创建WEB-INF/lib、WEB-INF/classes两个目录。
并在WEB-INF目录中,创建Spring、jsf、及web配置文件:
3.在MANIFEST.MF文件中的配置项:Runtime/Classpath中添加刚才创建的两个目录。
4.点击Add添加我们将要使用的jar包。
5.新建一个网页就的Bean类TestBean。
- packagecom.infotech.test.bean;
 - importcom.infotech.test.control.TestBeanControl;
 - publicclassTestBean{
 - privateTestBeanControltestControl;
 - publicStringgetShowMsg(){
 - returntestControl.getShowMsg();
 - }
 - publicTestBeanControlgetTestControl(){
 - returntestControl;
 - }
 - publicvoidsetTestControl(TestBeanControltestControl){
 - this.testControl=testControl;
 - }
 - }
 
6.创建一下控制类TestBeanControl
- packagecom.infotech.test.control;
 - importcom.infotech.test.common.IShowMsgInfo;
 - publicclassTestBeanControl{
 - privatestaticIShowMsgInfomsginfoService;
 - publicStringgetShowMsg(){
 - returnmsginfoService.GetMsgInfo();
 - }
 - publicvoidsetMsginfoService(IShowMsgInfomsginfoService){
 - this.msginfoService=msginfoService;
 - }
 - publicvoidunsetMsginfoService(IShowMsgInfomsginfoService){
 - if(this.msginfoService==msginfoService)
 - this.msginfoService=null;
 - }
 - }
 
7.打开这个接口bundle工程的MANIFEST.MS文件,在Dependencies/ImportedPackages中添加上面新建的接口服务类及WEB服务类。
8.新建一个OSGI-INF/components.xm文件,我们来引用上面发布出来的OSGI服务。
- bind="setMsginfoService"unbind="unsetMsginfoService"
 - cardinality="0..1"policy="dynamic"/>
 
9.打开这个接口bundle工程的MANIFEST.MS文件,添加一行。
- Service-Component:OSGI-INF/components.xml
 
10.修改Application-test.xml。
- 修改faces-config.xml
 - "-//SunMicrosystems,Inc.//DTDJavaServerFacesConfig1.1//EN"
 - "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
 xmanager_web_resources zh_CN - org.springframework.web.jsf.DelegatingVariableResolver
 TestBean com.infotech.test.bean.TestBean session testControl #{TestControl} index * index /index.jsp 
修改web.xml。
- xmlns="http://java.sun.com/xml/ns/j2ee"
 - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 - xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 XmanagerWeb javax.faces.CONFIG_FILES /WEB-INF/faces-config.xml org.apache.myfaces.DETECT_JAVASCRIPT false org.apache.myfaces.PRETTY_HTML true org.apache.myfaces.AUTO_SCROLL true contextConfigLocation /WEB-INF/Application*.xml - org.springframework.web.context.ContextLoaderListener
 MyFacesExtensionsFilter - org.apache.myfaces.webapp.filter.ExtensionsFilter
 maxFileSize 100m MyFacesExtensionsFilter FacesServlet MyFacesExtensionsFilter /faces/myFacesExtensionResource/* SetCharacterEncoding - org.springframework.web.filter.CharacterEncodingFilter
 encoding UTF-8 SetCharacterEncoding *.jsf FacesServlet javax.faces.webapp.FacesServlet 1 FacesServlet *.jsf index.jsf index.jsp 
11.导入三个工程:
- Catalina.config
 - Server.config
 - Org.springframework.osgi.log4j.config
 
12.好了,写一个测试页:index.jsp。
- <%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%>
 - <%@taglibprefix="f"uri="http://java.sun.com/jsf/core"%>
 - <%@taglibprefix="h"uri="http://java.sun.com/jsf/html"%>
 - <%@taglibprefix="x"uri="http://myfaces.apache.org/tomahawk"%>
 - <%@taglibprefix="c"uri="http://java.sun.com/jstl/core"%>
 - <%@taglibprefix="t"uri="http://jsftutorials.net/htmLib"%>
 
13.最后创建一个Debug环境。
运行结果:
                文章标题:基于OSGi和Spring开发企业级Web应用
                
                文章转载:http://www.csdahua.cn/qtweb/news33/461733.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网