新聞中心
作為一個新的事實上的工業(yè)標準,OSGi已經(jīng)受到了廣泛的關注,就在不久前EclipseCon也發(fā)布企業(yè)級OSGi標準,而IBM以及Eclipse也宣稱將大力發(fā)展Java模塊化。Spring是一個著名的輕量級Java EE開發(fā)框架,其特點是面向接口編程和非侵入式的依賴注入。

編輯推薦:OSGi入門與實踐全攻略 Spring開源框架技術
將OSGi和Spring結(jié)合能充分發(fā)揮二者各自的特長,更好地滿足企業(yè)級應用開發(fā)的需求。Spring開發(fā)組織在2008年發(fā)布了將OSGi和Spring結(jié)合的第一個版本:Spring-DM。
dmServer是一個完全模塊化部署的,基于OSGi的Java服務器,為運行企業(yè)Java應用和Spring應用提供更加強大的靈活性和可靠性。SpringSource應用平臺是構(gòu)建在Spring、OSGi和ApacheTomcat之上的應用服務器,這個新的應用服務器摒棄了原有的JavaEE服務器標準,自然而然地將Spring編程模型展現(xiàn)其中,隨之而來的還有一套基于OSGi內(nèi)核構(gòu)建的全新部署和打包系統(tǒng)。
實例教程:
一、.指定TargetPlatform到所用到的所有的bundle包的目錄中。
二、創(chuàng)建一個Service接口bundle
新建一個接口類:com.infotech.test.common.ShowMsgInfo;
同時新加一個接口方法:publicStringGetMsgInfo();
打開這個接口bundle工程的MANIFEST.MS文件,在Runtime/ExprotedPackages中添加剛剛新建的接口類,使之對外提供這個服務。
三、創(chuàng)建一個接口bundle的實現(xiàn)bundle
打開這個接口bundle工程的MANIFEST.MS文件,在Dependencies/ImportedPackages中添加上面新建的接口類:
新建一個接口實現(xiàn)類:ShowMsgInfo:
在這個類中,實現(xiàn)接口中的方法:
- packagecom.infotech.test.service;
- importcom.infotech.test.common.IShowMsgInfo;
- publicclassShowMsgInfoimplementsIShowMsgInfo{
- @Override
- publicStringGetMsgInfo(){
- return"HelloWord!!!";
- }
- }
接下來,我需要將這個實現(xiàn)類發(fā)布成為一個OSGI服務:在工程新一個目錄OSGI-INF,并新建一個components.xml文檔。
打開這個接口bundle工程的MANIFEST.MS文件,添加一行:
- Service-Component:OSGI-INF/components.xml
#p#
四、接下來,我們創(chuàng)建一個WEB應用bundle:
1.新建一個網(wǎng)頁bundle工程:
2.在工程目錄中創(chuàng)建WEB-INF/lib、WEB-INF/classes兩個目錄。
并在WEB-INF目錄中,創(chuàng)建Spring、jsf、及web配置文件:
3.在MANIFEST.MF文件中的配置項:Runtime/Classpath中添加剛才創(chuàng)建的兩個目錄。
4.點擊Add添加我們將要使用的jar包。
5.新建一個網(wǎng)頁就的Bean類TestBean。
- packagecom.infotech.test.bean;
- importcom.infotech.test.control.TestBeanControl;
- publicclassTestBean{
- privateTestBeanControltestControl;
- publicStringgetShowMsg(){
- returntestControl.getShowMsg();
- }
- publicTestBeanControlgetTestControl(){
- returntestControl;
- }
- publicvoidsetTestControl(TestBeanControltestControl){
- this.testControl=testControl;
- }
- }
6.創(chuàng)建一下控制類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文件,我們來引用上面發(fā)布出來的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.最后創(chuàng)建一個Debug環(huán)境。
運行結(jié)果:
網(wǎng)站欄目:基于OSGi和Spring開發(fā)企業(yè)級Web應用
分享URL:http://m.5511xx.com/article/cohpopp.html


咨詢
建站咨詢
