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

編輯推薦:OSGi入門與實(shí)踐全攻略 Spring開(kāi)源框架技術(shù)
將OSGi和Spring結(jié)合能充分發(fā)揮二者各自的特長(zhǎng),更好地滿足企業(yè)級(jí)應(yīng)用開(kāi)發(fā)的需求。Spring開(kāi)發(fā)組織在2008年發(fā)布了將OSGi和Spring結(jié)合的第一個(gè)版本:Spring-DM。
dmServer是一個(gè)完全模塊化部署的,基于OSGi的Java服務(wù)器,為運(yùn)行企業(yè)Java應(yīng)用和Spring應(yīng)用提供更加強(qiáng)大的靈活性和可靠性。SpringSource應(yīng)用平臺(tái)是構(gòu)建在Spring、OSGi和ApacheTomcat之上的應(yīng)用服務(wù)器,這個(gè)新的應(yīng)用服務(wù)器摒棄了原有的JavaEE服務(wù)器標(biāo)準(zhǔn),自然而然地將Spring編程模型展現(xiàn)其中,隨之而來(lái)的還有一套基于OSGi內(nèi)核構(gòu)建的全新部署和打包系統(tǒng)。
實(shí)例教程:
一、.指定TargetPlatform到所用到的所有的bundle包的目錄中。
二、創(chuàng)建一個(gè)Service接口bundle
新建一個(gè)接口類:com.infotech.test.common.ShowMsgInfo;
同時(shí)新加一個(gè)接口方法:publicStringGetMsgInfo();
打開(kāi)這個(gè)接口bundle工程的MANIFEST.MS文件,在Runtime/ExprotedPackages中添加剛剛新建的接口類,使之對(duì)外提供這個(gè)服務(wù)。
三、創(chuàng)建一個(gè)接口bundle的實(shí)現(xiàn)bundle
打開(kāi)這個(gè)接口bundle工程的MANIFEST.MS文件,在Dependencies/ImportedPackages中添加上面新建的接口類:
新建一個(gè)接口實(shí)現(xiàn)類:ShowMsgInfo:
在這個(gè)類中,實(shí)現(xiàn)接口中的方法:
- packagecom.infotech.test.service;
- importcom.infotech.test.common.IShowMsgInfo;
- publicclassShowMsgInfoimplementsIShowMsgInfo{
- @Override
- publicStringGetMsgInfo(){
- return"HelloWord!!!";
- }
- }
接下來(lái),我需要將這個(gè)實(shí)現(xiàn)類發(fā)布成為一個(gè)OSGI服務(wù):在工程新一個(gè)目錄OSGI-INF,并新建一個(gè)components.xml文檔。
打開(kāi)這個(gè)接口bundle工程的MANIFEST.MS文件,添加一行:
- Service-Component:OSGI-INF/components.xml
#p#
四、接下來(lái),我們創(chuàng)建一個(gè)WEB應(yīng)用bundle:
1.新建一個(gè)網(wǎng)頁(yè)bundle工程:
2.在工程目錄中創(chuàng)建WEB-INF/lib、WEB-INF/classes兩個(gè)目錄。
并在WEB-INF目錄中,創(chuàng)建Spring、jsf、及web配置文件:
3.在MANIFEST.MF文件中的配置項(xiàng):Runtime/Classpath中添加剛才創(chuàng)建的兩個(gè)目錄。
4.點(diǎn)擊Add添加我們將要使用的jar包。
5.新建一個(gè)網(wǎng)頁(yè)就的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.打開(kāi)這個(gè)接口bundle工程的MANIFEST.MS文件,在Dependencies/ImportedPackages中添加上面新建的接口服務(wù)類及WEB服務(wù)類。
8.新建一個(gè)OSGI-INF/components.xm文件,我們來(lái)引用上面發(fā)布出來(lái)的OSGI服務(wù)。
- bind="setMsginfoService"unbind="unsetMsginfoService"
- cardinality="0..1"policy="dynamic"/>
9.打開(kāi)這個(gè)接口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.導(dǎo)入三個(gè)工程:
- Catalina.config
- Server.config
- Org.springframework.osgi.log4j.config
12.好了,寫一個(gè)測(cè)試頁(yè):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)建一個(gè)Debug環(huán)境。
運(yùn)行結(jié)果:
文章名稱:基于OSGi和Spring開(kāi)發(fā)企業(yè)級(jí)Web應(yīng)用
網(wǎng)站URL:http://fisionsoft.com.cn/article/cohpopp.html


咨詢
建站咨詢
