基于OSGi和Spring開發企業級Web應用
作為一個新的事實上的工業標準,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內核構建的全新部署和打包系統。
實例教程:
一、.指定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文檔。
- <?xmlversionxmlversion="1.0"encoding="UTF-8"?>
- <componentnamecomponentname="ShowMsgInfo"immediate="true">
- <implementationclassimplementationclass="com.infotech.test.service.ShowMsgInfo"/>
- <service>
- <provideinterfaceprovideinterface="com.infotech.test.common.IShowMsgInfo"/>
- </service>
- </component>
打開這個接口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服務。
- <?xmlversionxmlversion="1.0"encoding="UTF-8"?>
- <componentnamecomponentname="TestBean"immediate="true">
- <implementationclassimplementationclass="com.infotech.test.control.TestBeanControl"/>
- <referencenamereferencename="msginfoService"interface="com.infotech.test.common.IShowMsgInfo"
- bind="setMsginfoService"unbind="unsetMsginfoService"
- cardinality="0..1"policy="dynamic"/>
- </component>
9.打開這個接口bundle工程的MANIFEST.MS文件,添加一行。
- Service-Component:OSGI-INF/components.xml
10.修改Application-test.xml。
- <?xmlversionxmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <beanidbeanid="TestControl"class="com.infotech.test.control.TestBeanControl"></bean>
- </beans>
- 修改faces-config.xml
- <?xmlversionxmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEfaces-configPUBLIC
- "-//SunMicrosystems,Inc.//DTDJavaServerFacesConfig1.1//EN"
- "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
- <faces-config>
- <application>
- <message-bundle>xmanager_web_resources</message-bundle>
- <locale-config>
- <default-locale>zh_CN</default-locale>
- </locale-config>
- <variable-resolver>
- org.springframework.web.jsf.DelegatingVariableResolver
- </variable-resolver>
- </application>
- <managed-bean>
- <managed-bean-name>TestBean</managed-bean-name>
- <managed-bean-class>com.infotech.test.bean.TestBean</managed-bean-class>
- <managed-bean-scope>session</managed-bean-scope>
- <managed-property>
- <property-name>testControl</property-name>
- <value>#{TestControl}</value>
- </managed-property>
- </managed-bean>
- <navigation-rule>
- <description>index</description>
- <from-view-id>*</from-view-id>
- <navigation-case>
- <from-outcome>index</from-outcome>
- <to-view-id>/index.jsp</to-view-id>
- <redirect/>
- </navigation-case>
- </navigation-rule>
- </faces-config>
修改web.xml。
- <?xmlversionxmlversion="1.0"encoding="UTF-8"?>
- <web-appidweb-appid="WebApp_ID"version="2.4"
- 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">
- <display-name>XmanagerWeb</display-name>
- <context-param>
- <param-name>javax.faces.CONFIG_FILES</param-name>
- <param-value>/WEB-INF/faces-config.xml</param-value>
- </context-param>
- <context-param>
- <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
- <param-value>false</param-value>
- </context-param>
- <context-param>
- <param-name>org.apache.myfaces.PRETTY_HTML</param-name>
- <param-value>true</param-value>
- </context-param>
- <context-param>
- <param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
- <param-value>true</param-value>
- </context-param>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/Application*.xml</param-value>
- </context-param>
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
- <filter>
- <filter-name>MyFacesExtensionsFilter</filter-name>
- <filter-class>
- org.apache.myfaces.webapp.filter.ExtensionsFilter
- </filter-class>
- <init-param>
- <param-name>maxFileSize</param-name>
- <param-value>100m</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>MyFacesExtensionsFilter</filter-name>
- <servlet-name>FacesServlet</servlet-name>
- </filter-mapping>
- <filter-mapping>
- <filter-name>MyFacesExtensionsFilter</filter-name>
- <url-pattern>/faces/myFacesExtensionResource/*</url-pattern>
- </filter-mapping>
- <filter>
- <filter-name>SetCharacterEncoding</filter-name>
- <filter-class>
- org.springframework.web.filter.CharacterEncodingFilter
- </filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>SetCharacterEncoding</filter-name>
- <url-pattern>*.jsf</url-pattern>
- </filter-mapping>
- <servlet>
- <servlet-name>FacesServlet</servlet-name>
- <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>FacesServlet</servlet-name>
- <url-pattern>*.jsf</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsf</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
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"%>
- <html>
- <head>
- <metahttp-equivmetahttp-equiv="Content-Type"content="text/html;charset=utf-8">
- <title></title>
- </head>
- <body>
- <f:view>
- <h:outputTextvalueh:outputTextvalue="#{TestBean.showMsg}"></h:outputText>
- </f:view>
- </body>
- </html>
13.最后創建一個Debug環境。
運行結果:
【編輯推薦】