基于OSGi和Spring开发企业级Web应用

开发 后端
目前OSGi和Spring已经受到了广泛的关注,如果我们将OSGi和Spring结合,更能充分发挥二者各自的特长,同时更好地满足企业级应用开发的需求。

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

51CTO编辑推荐:OSGi入门与实践全攻略 Spring开源框架技术

将OSGi和Spring结合能充分发挥二者各自的特长,更好地满足企业级应用开发的需求。Spring开发组织在2008年发布了将OSGi和Spring结合的第一个版本:Spring-DM。

dmServer是一个完全模块化部署的,基于OSGi的Java服务器,为运行企业Java应用和Spring应用提供更加强大的灵活性和可靠性。SpringSource应用平台是构建在Spring、OSGi和ApacheTomcat之上的应用服务器,这个新的应用服务器摒弃了原有的JavaEE服务器标准,自然而然地将Spring编程模型展现其中,随之而来的还有一套基于OSGi内核构建的全新部署和打包系统。

Spring编程模型

实例教程:

一、.指定TargetPlatform到所用到的所有的bundle包的目录中。

bundle包的目录

二、创建一个Service接口bundle

新建一个接口类:com.infotech.test.common.ShowMsgInfo;

创建一个Service接口bundle

同时新加一个接口方法:publicStringGetMsgInfo();

打开这个接口bundle工程的MANIFEST.MS文件,在Runtime/ExprotedPackages中添加刚刚新建的接口类,使之对外提供这个服务。

打开这个接口bundle工程

三、创建一个接口bundle的实现bundle

打开这个接口bundle工程的MANIFEST.MS文件,在Dependencies/ImportedPackages中添加上面新建的接口类:

创建一个接口bundle 

在Dependencies/ImportedPackages中添加上面新建的接口类

新建一个接口实现类:ShowMsgInfo:

ShowMsgInfo

在这个类中,实现接口中的方法:

  1. packagecom.infotech.test.service;  
  2. importcom.infotech.test.common.IShowMsgInfo;  
  3. publicclassShowMsgInfoimplementsIShowMsgInfo{  
  4.  @Override  
  5.  publicStringGetMsgInfo(){  
  6.   return"HelloWord!!!";  
  7.  }  

接下来,我需要将这个实现类发布成为一个OSGI服务:在工程新一个目录OSGI-INF,并新建一个components.xml文档。

  1. <?xmlversionxmlversion="1.0"encoding="UTF-8"?> 
  2. <componentnamecomponentname="ShowMsgInfo"immediate="true"> 
  3.  <implementationclassimplementationclass="com.infotech.test.service.ShowMsgInfo"/> 
  4.  <service> 
  5.   <provideinterfaceprovideinterface="com.infotech.test.common.IShowMsgInfo"/> 
  6.  </service> 
  7. </component> 

打开这个接口bundle工程的MANIFEST.MS文件,添加一行:

  1. Service-Component:OSGI-INF/components.xml
  2.  

#p#
四、接下来,我们创建一个WEB应用bundle:

1.新建一个网页bundle工程:

新建一个网页bundle工程

2.在工程目录中创建WEB-INF/lib、WEB-INF/classes两个目录。

创建WEB-INF/lib、WEB-INF/classes两个目录

并在WEB-INF目录中,创建Spring、jsf、及web配置文件:

创建Spring、jsf、及web配置文件

3.在MANIFEST.MF文件中的配置项:Runtime/Classpath中添加刚才创建的两个目录。

在Runtime/Classpath中添加刚才创建的两个目录

4.点击Add添加我们将要使用的jar包。

点击Add添加我们将要使用的jar包

5.新建一个网页就的Bean类TestBean。

  1. packagecom.infotech.test.bean;  
  2. importcom.infotech.test.control.TestBeanControl;  
  3. publicclassTestBean{  
  4.  privateTestBeanControltestControl;  
  5.  publicStringgetShowMsg(){  
  6.   returntestControl.getShowMsg();  
  7.  }  
  8.  
  9.  publicTestBeanControlgetTestControl(){  
  10.   returntestControl;  
  11.  }  
  12.  publicvoidsetTestControl(TestBeanControltestControl){  
  13.   this.testControl=testControl;  
  14.  }  

6.创建一下控制类TestBeanControl

  1. packagecom.infotech.test.control;  
  2. importcom.infotech.test.common.IShowMsgInfo;  
  3. publicclassTestBeanControl{  
  4.  privatestaticIShowMsgInfomsginfoService;  
  5.  
  6.  publicStringgetShowMsg(){  
  7.   returnmsginfoService.GetMsgInfo();  
  8.  }  
  9.  
  10.  publicvoidsetMsginfoService(IShowMsgInfomsginfoService){  
  11.   this.msginfoService=msginfoService;  
  12.  }  
  13.  publicvoidunsetMsginfoService(IShowMsgInfomsginfoService){  
  14.   if(this.msginfoService==msginfoService)  
  15.    this.msginfoService=null;  
  16.  }  

7.打开这个接口bundle工程的MANIFEST.MS文件,在Dependencies/ImportedPackages中添加上面新建的接口服务类及WEB服务类。

添加上面新建的接口服务类及WEB服务类

8.新建一个OSGI-INF/components.xm文件,我们来引用上面发布出来的OSGI服务。

  1. <?xmlversionxmlversion="1.0"encoding="UTF-8"?> 
  2. <componentnamecomponentname="TestBean"immediate="true"> 
  3.  <implementationclassimplementationclass="com.infotech.test.control.TestBeanControl"/> 
  4.  <referencenamereferencename="msginfoService"interface="com.infotech.test.common.IShowMsgInfo"  
  5.   bind="setMsginfoService"unbind="unsetMsginfoService"  
  6.   cardinality="0..1"policy="dynamic"/> 
  7. </component> 

9.打开这个接口bundle工程的MANIFEST.MS文件,添加一行。

  1. Service-Component:OSGI-INF/components.xml 
  2.  

10.修改Application-test.xml。

  1. <?xmlversionxmlversion="1.0"encoding="UTF-8"?> 
  2. <!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"> 
  3. <beans> 
  4.  <beanidbeanid="TestControl"class="com.infotech.test.control.TestBeanControl"></bean> 
  5. </beans> 
  6. 修改faces-config.xml  
  7. <?xmlversionxmlversion="1.0"encoding="UTF-8"?> 
  8. <!DOCTYPEfaces-configPUBLIC  
  9. "-//SunMicrosystems,Inc.//DTDJavaServerFacesConfig1.1//EN"  
  10. "http://java.sun.com/dtd/web-facesconfig_1_1.dtd"> 
  11.  
  12. <faces-config> 
  13.  
  14.  <application> 
  15.   <message-bundle>xmanager_web_resources</message-bundle> 
  16.   <locale-config> 
  17.    <default-locale>zh_CN</default-locale> 
  18.   </locale-config> 
  19.   <variable-resolver> 
  20.    org.springframework.web.jsf.DelegatingVariableResolver  
  21.   </variable-resolver> 
  22.  </application> 
  23.    
  24.  <managed-bean> 
  25.   <managed-bean-name>TestBean</managed-bean-name> 
  26.   <managed-bean-class>com.infotech.test.bean.TestBean</managed-bean-class> 
  27.   <managed-bean-scope>session</managed-bean-scope> 
  28.   <managed-property> 
  29.    <property-name>testControl</property-name> 
  30.    <value>#{TestControl}</value> 
  31.   </managed-property> 
  32.  </managed-bean> 
  33.  
  34. <navigation-rule> 
  35. <description>index</description> 
  36. <from-view-id>*</from-view-id> 
  37. <navigation-case> 
  38.  
  39. <from-outcome>index</from-outcome> 
  40. <to-view-id>/index.jsp</to-view-id> 
  41. <redirect/>     
  42. </navigation-case> 
  43. </navigation-rule> 
  44. </faces-config> 

修改web.xml。

  1. <?xmlversionxmlversion="1.0"encoding="UTF-8"?> 
  2. <web-appidweb-appid="WebApp_ID"version="2.4"  
  3.  xmlns="http://java.sun.com/xml/ns/j2ee" 
  4.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  5.  xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
  6.  <display-name>XmanagerWeb</display-name> 
  7.  
  8.  <context-param> 
  9.   <param-name>javax.faces.CONFIG_FILES</param-name> 
  10.   <param-value>/WEB-INF/faces-config.xml</param-value> 
  11.  </context-param> 
  12.  
  13.  <context-param> 
  14.   <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name> 
  15.   <param-value>false</param-value> 
  16.  </context-param> 
  17.  
  18.  <context-param> 
  19.   <param-name>org.apache.myfaces.PRETTY_HTML</param-name> 
  20.   <param-value>true</param-value> 
  21.  </context-param> 
  22.  
  23.  <context-param> 
  24.   <param-name>org.apache.myfaces.AUTO_SCROLL</param-name> 
  25.   <param-value>true</param-value> 
  26.  </context-param> 
  27.  
  28.  <context-param> 
  29.   <param-name>contextConfigLocation</param-name> 
  30.   <param-value>/WEB-INF/Application*.xml</param-value> 
  31.  </context-param> 
  32.  
  33.  <listener> 
  34.   <listener-class> 
  35.    org.springframework.web.context.ContextLoaderListener  
  36.   </listener-class> 
  37.  </listener> 
  38.  
  39.  <filter> 
  40.   <filter-name>MyFacesExtensionsFilter</filter-name> 
  41.   <filter-class> 
  42.    org.apache.myfaces.webapp.filter.ExtensionsFilter  
  43.   </filter-class> 
  44.   <init-param> 
  45.    <param-name>maxFileSize</param-name> 
  46.    <param-value>100m</param-value> 
  47.   </init-param> 
  48.  </filter> 
  49.  
  50.  <filter-mapping> 
  51.   <filter-name>MyFacesExtensionsFilter</filter-name> 
  52.   <servlet-name>FacesServlet</servlet-name> 
  53.  </filter-mapping> 
  54.  
  55.  <filter-mapping> 
  56.   <filter-name>MyFacesExtensionsFilter</filter-name> 
  57.   <url-pattern>/faces/myFacesExtensionResource/*</url-pattern> 
  58.  </filter-mapping> 
  59.  
  60.  <filter> 
  61.   <filter-name>SetCharacterEncoding</filter-name> 
  62.   <filter-class> 
  63.    org.springframework.web.filter.CharacterEncodingFilter  
  64.   </filter-class> 
  65.   <init-param> 
  66.    <param-name>encoding</param-name> 
  67.    <param-value>UTF-8</param-value> 
  68.   </init-param> 
  69.  </filter> 
  70.  
  71.  <filter-mapping> 
  72.   <filter-name>SetCharacterEncoding</filter-name> 
  73.   <url-pattern>*.jsf</url-pattern> 
  74.  </filter-mapping> 
  75.  <servlet> 
  76.   <servlet-name>FacesServlet</servlet-name> 
  77.   <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
  78.   <load-on-startup>1</load-on-startup> 
  79.  </servlet> 
  80.  
  81.  <servlet-mapping> 
  82.   <servlet-name>FacesServlet</servlet-name> 
  83.   <url-pattern>*.jsf</url-pattern> 
  84.  </servlet-mapping> 
  85.  
  86.  <welcome-file-list> 
  87.   <welcome-file>index.jsf</welcome-file> 
  88.   <welcome-file>index.jsp</welcome-file> 
  89.  </welcome-file-list> 
  90. </web-app> 

11.导入三个工程:

  1. Catalina.config  
  2. Server.config  
  3. Org.springframework.osgi.log4j.config 

12.好了,写一个测试页:index.jsp。

  1. <%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%> 
  2. <%@taglibprefix="f"uri="http://java.sun.com/jsf/core"%> 
  3. <%@taglibprefix="h"uri="http://java.sun.com/jsf/html"%> 
  4. <%@taglibprefix="x"uri="http://myfaces.apache.org/tomahawk"%> 
  5. <%@taglibprefix="c"uri="http://java.sun.com/jstl/core"%> 
  6. <%@taglibprefix="t"uri="http://jsftutorials.net/htmLib"%> 
  7. <html> 
  8. <head> 
  9. <metahttp-equivmetahttp-equiv="Content-Type"content="text/html;charset=utf-8"> 
  10. <title></title> 
  11. </head> 
  12. <body> 
  13. <f:view> 
  14.  <h:outputTextvalueh:outputTextvalue="#{TestBean.showMsg}"></h:outputText> 
  15. </f:view>   
  16. </body> 
  17. </html> 

13.最后创建一个Debug环境。

最后创建一个Debug环境

运行结果:

运行结果

【编辑推荐】

  1. Spring事务管理高级应用难点剖析
  2. Java知识拾遗:三大框架的技术起源
  3. Spring 3.0正式发布 采用全新组件模型
  4. Hibernate批量更新与删除实例浅析
  5. 剖析Hibernate Synchronizer配置文件
责任编辑:王晓东 来源: IBM
相关推荐

2009-03-03 10:06:00

IBMJavaOSGi

2009-03-30 09:42:28

OSGiJavaJCP

2010-03-29 17:05:07

OSGi

2009-03-02 09:22:39

OSGiJ2EEEclipse

2009-11-23 20:16:25

ibmdwRational

2012-05-15 15:21:29

企业级

2009-12-14 20:13:57

IBM

2009-01-13 09:05:45

tomcatSpring框架Web服务器

2012-06-14 13:26:22

2013-05-20 10:38:02

Quartz企业级开发任务调度

2012-12-18 17:11:58

2010-08-04 15:20:15

Flex企业级开发

2009-06-23 14:55:43

AJAX和JSF

2009-06-23 15:02:56

JSF和AJAX

2013-01-25 16:54:42

富士通HTML5企业级

2010-01-04 16:38:07

企业级Silverli

2021-10-11 14:28:25

TypeScript企业级应用

2015-10-15 17:17:33

云应用平台系统构建实践

2011-12-06 14:02:27

企业级移动开发

2013-09-30 10:19:46

SAP
点赞
收藏

51CTO技术栈公众号