作为一个新的事实上的工业标准,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环境。
运行结果:
【编辑推荐】