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

          轉(zhuǎn)載請注明出處 http://www.aygfsteel.com/fireflyk/

           

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

          1.       同樣方法創(chuàng)建helloworldweb Bundle,用Maven方式創(chuàng)建并轉(zhuǎn)為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,引入相關(guān)的Bundle,示意圖如下,


          Validate Bundle,可以看到還缺少哪些包。個人理解,這里應該包含import package中的Bundle和運行時調(diào)用的Bundle

          啟動后,可以看到Console中的輸出日志。訪問以下Url查看效果,

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

          http://127.0.0.1:8080/helloworldweb/

           



          專注于Java,數(shù)據(jù)庫性能,Web Server負載,數(shù)據(jù)挖掘,機器學習等方向
          posted on 2011-10-09 13:12 柳桐 閱讀(3388) 評論(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 | 柳桐
          @啊啊
          版本對嗎?  回復  更多評論
            

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 巫溪县| 武隆县| 南宫市| 龙州县| 自治县| 农安县| 咸阳市| 宿松县| 永顺县| 绵阳市| 仁寿县| 祥云县| 滦平县| 绥滨县| 柘荣县| 仪征市| 哈尔滨市| 祥云县| 株洲县| 郴州市| 桐城市| 泽州县| 宜川县| 石门县| 定襄县| 贡嘎县| 芒康县| 五大连池市| 芜湖市| 南木林县| 神农架林区| 仙桃市| 天镇县| 永顺县| 正宁县| 满城县| 平和县| 钟山县| 黄大仙区| 寿阳县| 宣城市|