隨筆-7  評論-3  文章-0  trackbacks-0

          轉載請注明出處 http://www.aygfsteel.com/fireflyk/

           

          接上文,[OSGi] OSGi + Spring + Web Demo [1]

          1.       同樣方法創建helloworldweb Bundle,用Maven方式創建并轉為PDE Tools

          2.       引入Spring OSGi 1.2.1

          下載Spring OSGi 1.2.1,將其中distlib中的jar包都copyeclipseplugins目錄中。重啟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是代碼編譯中需要用到的classpackage,包括importclass、及其父類、成員變量的類等。在運行時,需要勾選相應packageBundle

          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中的serviceWEB-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 mappingservice注入。可特別注意下,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

          勾選timeservicehelloworldweb,引入相關的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/

           



          專注于Java,數據庫性能,Web Server負載,數據挖掘,機器學習等方向
          posted on 2011-10-09 13:12 柳桐 閱讀(3389) 評論(2)  編輯  收藏 所屬分類: Java

          評論:
          # re: [OSGi] OSGi + Spring + Web Demo [2][未登錄] 2012-09-04 14:50 | 啊啊
          spring osgi的包里沒有org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext這個類啊  回復  更多評論
            
          # re: [OSGi] OSGi + Spring + Web Demo [2] 2012-09-04 23:20 | 柳桐
          @啊啊
          版本對嗎?  回復  更多評論
            
          主站蜘蛛池模板: 大渡口区| 肇东市| 耒阳市| 安图县| 广州市| 瓮安县| 嘉黎县| 常宁市| 凤山市| 新巴尔虎左旗| 专栏| 伊春市| 宜宾县| 平乡县| 宜丰县| 铜鼓县| 南靖县| 嘉祥县| 博爱县| 北碚区| 丹棱县| 丁青县| 西林县| 兰州市| 韩城市| 广河县| 名山县| 余庆县| 灵宝市| 广丰县| 徐汇区| 高清| 安多县| 海林市| 乾安县| 横山县| 平泉县| 绥滨县| 白朗县| 甘德县| 黔江区|