隨筆-7  評論-3  文章-0  trackbacks-0
          轉載請注明出處 http://www.aygfsteel.com/fireflyk/archive/2011/09/18/358922.html

          網上找了很多例子,看了很多資料發現都不能完全重現一個OSGi的Web Demo
          本文結合了《OSGi原理與最佳實踐》+公司同事的《OSGi開發》+自己的理解。

          環境WindowXP + eclipse3.4 + Spring OSGi1.2.1
          1. 創建timeservice Bundle,提供輸出時間的功能
          1.1 eclipse內創建Maven Project,選quickstart project:timeservice,package org.osgichina.demo.timeservice
          1.2 創建好后,remove maven的jar包,刪掉project中的App和AppTest,在項目上右鍵->PDE Tools->Convert
          1.3
          package org.osgichina.demo.timeservice;

          public interface TimeService {
              String getCurrentTime();
          }

          public class TimeServiceImpl implements TimeService {

              
          public String getCurrentTime() {
                  
          return (new Date()).toString();
              }

          }

          1.4 Spring配置
          配置放在META-INF/spring目錄下,框架啟動時會按照這個路徑去尋找配置文件
          timeservice.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"
              xsi:schemaLocation
          ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

              
          <!-- timeService bean -->
              
          <bean name="timeService" class="org.osgichina.demo.timeservice.impl.TimeServiceImpl" />

          </beans>

          timeservice-osgi.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"
          >

              
          <!-- 將timeService 這個bean發布為OSGi服務的定義-->
              
          <osgi:service id="osgiTimeService" ref="timeService" interface="org.osgichina.demo.timeservice.TimeService">
              
          </osgi:service>

          </beans>

          這個Bundle到此已經完成了,下一節會講解helloworldweb Bundle,涉及Import Package,OSGi的啟動,Spring+Web的配置等

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

          評論:
          # re: [OSGi] OSGi + Spring + Web Demo [1] 2011-09-19 08:53 | tb
          先收藏了   回復  更多評論
            
          主站蜘蛛池模板: 白山市| 吴忠市| 永登县| 安徽省| 湖北省| 嘉峪关市| 永仁县| 克山县| 汝城县| 凌海市| 周口市| 长沙县| 三门峡市| 昌乐县| 奉节县| 渝北区| 咸丰县| 酒泉市| 水城县| 贵港市| 清镇市| 衡东县| 永川市| 信丰县| 临泉县| 吕梁市| 文昌市| 临邑县| 宝坻区| 玉山县| 铜梁县| 关岭| 贵港市| 泰州市| 子长县| 林西县| 远安县| 洪雅县| 临城县| 乌拉特后旗| 南华县|