[OSGi] OSGi + Spring + Web Demo [2]

本文介绍如何使用OSGi和Spring框架集成实现模块化的Java Web应用。通过创建helloworldwebBundle项目,引入SpringOSGi 1.2.1,并配置必要的MANIFEST.MF文件及XML配置文件来实现服务注入和服务引用。

1. 同样方法创建 helloworldweb Bundle,用 Maven方式创建并转为 PDE Tools。

2. 引入 Spring OSGi 1.2.1

下载 Spring OSGi 1.2.1,将其中 dist和 lib中的 jar包都 copy到 eclipse的 plugins目录中。重启 eclipse。

3. HelloWorldController.java

public class HelloWorldController extends MultiActionController {

private TimeService timeService ;

 

public ModelAndView echoTime(HttpServletRequest request,

HttpServletResponse response) throws Exception {

System. out .println( "echoTime" );

ModelAndView mv = new ModelAndView( "result" );

mv.addObject( "result" , timeService .getCurrentTime());

return mv;

}

 

public TimeService getTimeService() {

System. out .println( "getTimeService" );

return timeService ;

}

 

public void setTimeService(TimeService timeService) {

System. out .println( "setTimeService" );

this . timeService = timeService;

}

}

4. MANIFEST.MF。个人理解, import-package是代码编译中需要用到的 class的 package,包括 import的 class、及其父类、成员变量的类等。在运行时,需要勾选相应 package的 Bundle。

Import-Package : javax.servlet.http; version ="2.5.0",

javax.servlet.jsp.jstl.core; version ="1.1.2",

org.apache.jasper.servlet; version ="5.5.17",

org.apache.taglibs.standard.tag.common.fmt; version ="1.1.2",

org.osgichina.demo.timeservice,

org.springframework.beans; version ="2.5.6.SEC01",

org.springframework.context.support; version ="2.5.6.SEC01",

org.springframework.core; version ="2.5.6.SEC01",

org.springframework.osgi.web.context.support; version ="1.2.1",

org.springframework.web.context; version ="2.5.6.SEC01",

org.springframework.web.context.support; version ="2.5.6.SEC01",

org.springframework.web.servlet; version ="2.5.6.SEC01",

org.springframework.web.servlet.handler; version ="2.5.6.SEC01",

org.springframework.web.servlet.mvc; version ="2.5.6.SEC01",

org.springframework.web.servlet.mvc.multiaction; version ="2.5.6.SEC01",

org.springframework.web.servlet.mvc.support; version ="2.5.6.SEC01",

org.springframework.web.servlet.view; version ="2.5.6.SEC01"

 

 

5. 引入 timeservice Bundle中的 service, WEB-INF/applicationContext.xml。引入的 OSGi service,注入方式和普通 service是一样的,见 WEB-INF/spring-servlet.xml。

<? xml version = "1.0" encoding = "UTF-8" ?>

< beans xmlns = "http://www.springframework.org/schema/beans"

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi = "http://www.springframework.org/schema/osgi"

xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd" >

<!-- 引入 OSGi Service -->

< osgi:reference id = "osgiTimeService" interface = "org.osgichina.demo.timeservice.TimeService" />

</ beans >

 

6. 定义 web.xml,配置 OSGi Web,配置 Spring Framework

<? xml version = "1.0" encoding = "UTF-8" ?>

< web-app 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/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

< context-param >

< param-name > contextClass </ param-name >

< param-value >

org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext

</ param-value >

</ context-param >

< listener >

< listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >

</ listener >

< servlet >

< servlet-name > spring </ servlet-name >

< servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >

< init-param >

< param-name > contextClass </ param-name >

< param-value >

org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext

</ param-value >

</ init-param >

< load-on-startup > 2 </ load-on-startup >

</ servlet >

< servlet-mapping >

< servlet-name > spring </ servlet-name >

< url-pattern > /spring/* </ url-pattern >

</ servlet-mapping >

</ web-app >

 

7. WEB-INF/spring-servlet.xml。配置 url mapping , service注入。可特别注意下, timeService的注入和普通 service的注入是一样的。

<? xml version = "1.0" encoding = "UTF-8" ?>

< beans xmlns = "http://www.springframework.org/schema/beans"

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation = "http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd" >

<!-- page config -->

< bean id = "viewResolver"

class = "org.springframework.web.servlet.view.UrlBasedViewResolver" >

< property name = "viewClass"

value = "org.springframework.web.servlet.view.JstlView" />

< property name = "prefix" value = "/" />

< property name = "suffix" value = ".jsp" />

</ bean >

<!-- controller general config -->

< bean

class = "org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >

< property name = "caseSensitive" value = "true" />

< property name = "order" value = "0" />

< property name = "pathPrefix" value = "/" />

</ bean >

<!-- controller general config -->

< bean id = "internalPathMethodNameResolver"

class = "org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver" />

 

<!-- action to url mapping -->

< bean id = "urlMapping"

class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >

< property name = "mappings" >

< props >

< prop key = "/helloWorld/*" > helloWorldController </ prop >

</ props >

</ property >

</ bean >

<!-- Bean Definition -->

< bean id = "helloWorldController" class = "org.osgichina.demo.web.HelloWorldController" >

< property name = "methodNameResolver" >

< ref bean = "internalPathMethodNameResolver" />

</ property >

< property name = "timeService" ref = "osgiTimeService" />

</ bean >

</ beans >

 

8. 启动 Demo

Run Configurations -> OSGi Framework 。

勾选 timeservice和 helloworldweb,引入相关的 Bundle,示意图如下,

点 Validate Bundle,可以看到还缺少哪些包。个人理解,这里应该包含 import package中的 Bundle和运行时调用的 Bundle 。

启动后,可以看到 Console中的输出日志。访问以下 Url查看效果,

http://127.0.0.1:8080/helloworldweb/spring/helloWorld/echoTime

http://127.0.0.1:8080/helloworldweb/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值