隨筆 - 1  文章 - 37  trackbacks - 0
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          留言簿(16)

          隨筆分類(lèi)

          隨筆檔案

          文章分類(lèi)

          文章檔案

          test

          搜索

          •  

          最新評(píng)論

          一、域模型
          先從最底層開(kāi)始,把域模型建立起來(lái),創(chuàng)建一個(gè)Plug-in project -

          org.phrancol.osgi.jpetstore.domain,不創(chuàng)建Activator,創(chuàng)建一個(gè)package - org.phrancol.osgi.jpetstore.domain,將src\org\springframework\samples\jpetstore\domain 里面的java文件copy進(jìn)去(重構(gòu)一下package的名字,java文件里的package自動(dòng)就變了,不用改每個(gè)文件),在MANIFEST.MF里面把依賴(lài)包導(dǎo)入一下并export這個(gè)package
          domain里面的logic,引用了dao,dao也引用了domain,會(huì)導(dǎo)致一個(gè)錯(cuò)誤,于是將logic做為一個(gè)獨(dú)立的bundle,就稱(chēng)為域邏輯吧

          二、域邏輯
          創(chuàng)建一個(gè)Plug-in project - org.phrancol.osgi.jpetstore.domain.logic,不創(chuàng)建Activator,創(chuàng)建一個(gè)同名package,將jpetstore\src\org\springframework\samples\jpetstore\domain\logic里面的java文件copy進(jìn)去,在MANIFEST.MF里面把依賴(lài)包導(dǎo)入

          三、DAO
          該做dao部分了,創(chuàng)建一個(gè) Plug-in project - org.phrancol.osgi.jpetstore.dao 并創(chuàng)建一個(gè)同名package,將jpetstore\src\org\springframework\samples\jpetstore\dao里面的東西全部copy進(jìn)去,在MANIFEST.MF里面導(dǎo)入依賴(lài)包(有一些包需要導(dǎo)入成bundle,例如 spring-jdbc,spring-ibatis),啟動(dòng)看看,發(fā)現(xiàn)spring-ibatis和persistence沒(méi)有啟動(dòng),手動(dòng)啟動(dòng)spring-ibatis,發(fā)現(xiàn)缺少ibatis,于是 New-> Other -> Plug-in from existing JAR archives ,導(dǎo)入ibatis,再啟動(dòng),發(fā)現(xiàn)少 javax.transaction,于是用相同方法導(dǎo)入。

          四、配置文件
           關(guān)于spring的配置文件,只有3個(gè)
          petstore-servlet.xml - 用于mvc的,定義dispatch bean
          applicationContext.xml - 用于域邏輯的
          dataAccessContext-local.xml - 用于DAO的,定義了DataSource
          (一)dataAccessContext-local.xml
          1,將 jdbc.properties和sql-map-config.xml拷貝到META-INF目錄里,sql-map-config.xml里面的資源路徑可能需要修改一下,maps里面的模型的class屬性也需要改一下
          2,在META-INF目錄中創(chuàng)建一個(gè)目錄spring,將dataAccessContext-local.xml拷貝進(jìn)去,將applicationContext.xml里面的加載jdbc.properties的bean移植過(guò)來(lái),放在DataSource的上面
          <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                  
          <property name="locations">
                      
          <list>
                          
          <value>META-INF/jdbc.properties</value>
                      
          </list>
                  
          </property>
              
          </bean>


          再把dataAccessContext-local.xml里面的dao-bean->class屬性值修改一下,再把sqlMapClient的configLocation的值修改成META-INF/sql-map-config.xml,例如
          <!-- SqlMap setup for iBATIS Database Layer -->
              
          <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
                  
          <property name="configLocation" value="META-INF/sql-map-config.xml"/>
                  
          <property name="dataSource" ref="dataSource"/>
              
          </bean>


              
          <!-- ========================= DAO DEFINITIONS: IBATIS IMPLEMENTATIONS ========================= -->

              
          <bean id="accountDao" class="org.phrancol.sogi.jpetstore.dao.ibatis.SqlMapAccountDao">
                  
          <property name="sqlMapClient" ref="sqlMapClient"/>
              
          </bean>
          3,啟動(dòng),報(bào)錯(cuò),找不到ibatis的某個(gè)類(lèi),報(bào)錯(cuò)信息如下
          org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.ibatis.SqlMapClientFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: com.ibatis.sqlmap.engine.transaction.external.ExternalTransactionConfig
          Caused by: java.lang.NoClassDefFoundError: com.ibatis.sqlmap.engine.transaction.external.ExternalTransactionConfig
              at org.springframework.orm.ibatis.SqlMapClientFactoryBean.
          class$(SqlMapClientFactoryBean.java:72)
          看來(lái)是spring-ibatis找不到com.ibatis.sqlmap.engine.transaction.external.ExternalTransactionConfig,來(lái)看看spring-ibatis的MANIFEST.MF,Dependencies的Import Packages, 加上com.ibatis.sqlmap.engine.transaction.external,再啟動(dòng),找不到DBCP,導(dǎo)入common-DBCP和common-pool,再啟動(dòng),正常,SS一下,還是正常

          (二)applicationContext.xml
          1,在org.phrancol.osgi.jpetstore.domain.logic的META-INF目錄中創(chuàng)建一個(gè)目錄spring,將applicationContext.xml拷貝進(jìn)去,并改名為 logic-context.xml,刪除propertyConfigurer這個(gè)bean,因?yàn)樗呀?jīng)被移到了dataAccessContext-local.xml里面,將里面的bean的class屬性修改一下,啟動(dòng),發(fā)現(xiàn)缺少org.aspectj,導(dǎo)入一個(gè),再啟動(dòng),報(bào)錯(cuò)
          Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'accountDao' is defined
          無(wú)法找到 accountDao,accountDao被定義在org.phrancol.osgi.jpetstore.dao里面了,于是現(xiàn)在需要使用spring-osgi 來(lái)進(jìn)行跨bundle的引用了。
          2,org.phrancol.osgi.jpetstore.dao/META-INF/spring目錄中創(chuàng)建一個(gè)spring配置文件dataAccessContext-local-osgi.xml,使用<osgi:service>將accountDao和其他被引用的DAO注冊(cè)成為OSGI 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"
              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 id="accountDaoOsgi" ref="accountDao"
                  interface
          ="org.phrancol.osgi.jpetstore.dao.AccountDao">
              
          </osgi:service>
              
              
          <osgi:service id="categoryDaoOsgi" ref="categoryDao"
                  interface
          ="org.phrancol.osgi.jpetstore.dao.CategoryDao">
              
          </osgi:service>
              
              
          <osgi:service id="productDaoOsgi" ref="productDao"
                  interface
          ="org.phrancol.osgi.jpetstore.dao.ProductDao">
              
          </osgi:service>
              
              
          <osgi:service id="itemDaoOsgi" ref="itemDao"
                  interface
          ="org.phrancol.osgi.jpetstore.dao.ItemDao">
              
          </osgi:service>
              
              
          <osgi:service id="orderDaoOsgi" ref="orderDao"
                  interface
          ="org.phrancol.osgi.jpetstore.dao.OrderDao">
              
          </osgi:service>
              
          </beans>

          3,在org.phrancol.osgi.jpetstore.domain.logic/META-INF/spring 也創(chuàng)建一個(gè)spring配置文件logic-context-osgi.xml,使用<osgi:reference>獲取服務(wù)
          <?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-2.0.xsd
              http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd"
          >

              
          <osgi:reference id="accountDaoOsgi" interface="org.phrancol.osgi.jpetstore.dao.AccountDao"/>

              
          <osgi:reference id="categoryDaoOsgi" interface="org.phrancol.osgi.jpetstore.dao.CategoryDao"/>

              
          <osgi:reference id="productDaoOsgi" interface="org.phrancol.osgi.jpetstore.dao.ProductDao"/>

              
          <osgi:reference id="itemDaoOsgi" interface="org.phrancol.osgi.jpetstore.dao.ItemDao"/>

              
          <osgi:reference id="orderDaoOsgi" interface="org.phrancol.osgi.jpetstore.dao.OrderDao"/>
              
          </beans>
          將logic-context.xml里面的bean  petStore的屬性引用修改一下
          <bean id="petStore" class="org.phrancol.osgi.jpetstore.domain.logic.PetStoreImpl">
                  
          <property name="accountDao" ref="accountDaoOsgi"/>
                  
          <property name="categoryDao" ref="categoryDaoOsgi"/>
                  
          <property name="productDao" ref="productDaoOsgi"/>
                  
          <property name="itemDao" ref="itemDaoOsgi"/>
                  
          <property name="orderDao" ref="orderDaoOsgi"/>
              
          </bean>
          再啟動(dòng)看看,還是報(bào)錯(cuò)
          org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'txAdvice': Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined
          原來(lái)是transactionManager的問(wèn)題,在DAO里面將transactionManager注冊(cè)成osgi service,在domain.logic里面引用,然后給txAdvice加上transaction-manager屬性
          <tx:advice id="txAdvice" transaction-manager="transactionManagerOsgi">
                  
          <tx:attributes>
                      
          <tx:method name="insert*"/>
                      
          <tx:method name="update*"/>
                      
          <tx:method name="*" read-only="true"/>
                  
          </tx:attributes>
              
          </tx:advice>
          OK,啟動(dòng),正常了
           
          (三)petstore-servlet.xml
          最后到spring-mvc的配置文件了,在前面已經(jīng)將這個(gè)配置文件拷貝到org.phrancol.osgi.jpetstore.springmvc/META-INF/dispatcher目錄下了,并且做了一些修改,讓DispatcherServlet加載用以顯示首頁(yè)
          1,新建一個(gè)package org.phrancol.osgi.jpetstore.springmvc.controller,將jpetstore\src\org\springframework\samples\jpetstore\web\spring里面的java文件拷貝進(jìn)去,將依賴(lài)包導(dǎo)入,然后在petstore-servlet.xml里面加上一個(gè)dispatch-bean,
          <bean name="/shop/addItemToCart.do" class="org.phrancol.osgi.jpetstore.springmvc.controller.AddItemToCartController">
                  
          <property name="petStore" ref="petStore"/>
              
          </bean>
          啟動(dòng)看看效果,報(bào)錯(cuò)
          Caused by: java.lang.ClassNotFoundException: org.phrancol.osgi.jpetstore.springmvc.controller.AddItemToCartController
          居然找不到這個(gè)類(lèi),來(lái)看看ClassLoader
          Thread.currentThread().getContextClassLoader() -> org.eclipse.core.runtime.internal.adaptor.ContextFinder
          Activator
          's ClassLoader -> org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader
          原來(lái)如此,那就這樣
          Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader())
          其實(shí)這樣也是可以的,但是為了不繞彎路,而且既然是Spring的配置文件,Spring-osgi也提供得有Bundle的ClassLoader -> org.springframework.osgi.context.support.BundleDelegatingClassLoader 交給Spring的ClassLoader吧
          2,看一段代碼 org.springframework.osgi.context.support.BundleContextAwareProcessor
          public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
                  
          if (bean instanceof BundleContextAware) {
                      
          if (this.bundleContext == null{
                          
          throw new IllegalStateException("Cannot satisfy BundleContextAware for bean '" +
                                  beanName 
          + "' without BundleContext");
                      }

                      
          if (logger.isDebugEnabled()) {
                          logger.debug(
          "Invoking setBundleContext on BundleContextAware bean '" + beanName + "'");
                      }

                      ((BundleContextAware) bean).setBundleContext(
          this.bundleContext);
                  }

                  
          return true;
              }

          3,再來(lái)看看這個(gè)接口org.springframework.osgi.context.BundleContextAware
          Interface that enables beans to find the bundle context they are defined in. Note that in most circumstances there is no need for a bean to implement this interface
          OK,那就寫(xiě)個(gè)BundleContextAware,然后配置成Bean就可以了

          4,創(chuàng)建一個(gè) Plug-in project   org.phrancol.osgi.jpetstore.util,并創(chuàng)建同名package,新建一個(gè)Interface HttpServiceRegister 用于注冊(cè)Servlet或Resource,這個(gè)過(guò)程在方法 public void serviceRegister(BundleContext context);里進(jìn)行,新建一個(gè)Class  BundleServiceRegister
          public class BundleServiceRegister implements BundleContextAware {
              
              
          private HttpServiceRegister httpServiceRegister;
              
              
          public BundleServiceRegister(HttpServiceRegister httpServiceRegister){
                  
          this.httpServiceRegister = httpServiceRegister;
              }


              
          public void setBundleContext(BundleContext context) {
                  
          this.httpServiceRegister.serviceRegister(context);
              }

          }

          5,生成一個(gè)類(lèi)org.phrancol.osgi.jpetstore.springmvc.SpringmvcHttpServiceRegister implements HttpServiceRegister
          將Activator.start方法里的內(nèi)容拷貝到SpringmvcHttpServiceRegister.serviceRegister方法里,刪除Activator(MANIFEST.MF)

          6,在org.phrancol.osgi.jpetstore.springmvc/META-INF/下建立一個(gè)目錄spring,從別的地方copy一個(gè)bean配置文件過(guò)來(lái),改名為logic-context.xml,加入如下配置
          <beans>
              
          <bean id="springHttpServiceRegister"
                  class
          ="org.phrancol.osgi.jpetstore.util.BundleServiceRegister">
                  
          <constructor-arg>
                      
          <bean class="org.phrancol.osgi.jpetstore.springmvc.SpringmvcHttpServiceRegister" />
                  
          </constructor-arg>
              
          </bean>
          </beans>
          啟動(dòng),報(bào)錯(cuò),petstore-servlet.xml里面的bean petStore 找不到,這個(gè)bean被定義在org.phrancol.osgi.jpetstore.domain.logic里面,于是使用<osgi:service>和<osgi:reference>來(lái)獲取
          在logic-context-osgi.xml里面加上
          <osgi:service id="petStoreOsgi" ref="petStore"
                  interface
          ="org.phrancol.osgi.jpetstore.domain.logic.PetStoreFacade">
              
          </osgi:service>
          在springmvc-context.xml加入如下內(nèi)容(注意namespaces)
              <osgi:reference id="petStoreOsgi"
                  interface
          ="org.phrancol.osgi.jpetstore.domain.logic.PetStoreFacade" />
          修改一下petstore-servlet.xml
          <property name="petStore" ref="petStore"/>
          改成
          <property name="petStore" ref="petStoreOsgi"/>
          OK,這次應(yīng)該沒(méi)問(wèn)題了,啟動(dòng)看看效果,還是報(bào)錯(cuò)
          org.springframework.beans.factory.BeanCreationException: Error creating bean with name '/shop/addItemToCart.do' defined in ServletContext resource [/META-INF/dispatcher/petstore-servlet.xml]: Cannot resolve reference to bean 'petStoreOsgi' while setting bean property 'petStore'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'petStoreOsgi' is defined
          Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 
          'petStoreOsgi' is defined
              at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:
          353)
              at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:
          916)
              at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:
          243)
              at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:
          160)
              at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:
          261)
              at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:
          109)
              at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:
          1100)
          居然找不到petStoreOsgi這個(gè)bean......
          posted on 2007-09-07 10:26 Phrancol Yang 閱讀(3992) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): OSGI
          主站蜘蛛池模板: 舟曲县| 鱼台县| 定西市| 阿鲁科尔沁旗| 江永县| 钦州市| 玉龙| 玉门市| 彰武县| 庐江县| 原平市| 邵阳县| 津市市| 文水县| 永嘉县| 乌兰县| 平湖市| 西贡区| 孝感市| 西藏| 青阳县| 伊宁县| 象山县| 宜城市| 屏东市| 闵行区| 阳朔县| 崇礼县| 岢岚县| 上栗县| 永川市| 鄂托克旗| 中牟县| 汨罗市| 宾阳县| 乳山市| 都匀市| 永城市| 霍山县| 大港区| 长阳|