1. 程式人生 > >dubbo的main方法啟動

dubbo的main方法啟動

想到一個問題:公司專案是dubbo實現服務治理的,啟動dubbo是通過自帶的main方法,這個方式 要求spring配置檔案存放至

classpath*:META-INF/spring/*.xml路徑底下。那麼我們公司的是如何實現的,好像並沒有將spring配置檔案放在META-INF中呀。。。

怎麼回事???

原來是在pom.xml裡面配置的,可以將配置檔案拷貝到META-INF/spring/下


網友的一篇解釋(感謝,侵刪):

http://blog.csdn.net/u013168445/article/details/71194666

------------------------------------------------------------------------------------------------------------------------------------------

一:執行spring容器的方式有三種

1:使用tomcat、jetty等servlet容器執行

2:自己寫一個Main方法執行

3:使用dubbo框架提供的Main方法執行

前面兩種有一定的侷限性,很多缺點,具體就不一一列舉,有興趣的同學可以研究一下,今天就給大家講如何實現第三種方式

二:Main配置詳講

優點:由框架本身提供,可實現優雅關機

spring路徑存放路徑

 按照dubbo官網解說,要使得Main能正確的啟用Spring容器,要求spring配置檔案存放至

classpath*:META-INF/spring/*.xml路徑底下

dubbo官方網址 http://dubbo.io/User+Guide-zh.htm

專案結構

專案是maven專案,框架是spring+mybatis+dubbo


maven配置

  1. <span style="font-size:18px;"><build>  
  2.     <!--jar包名稱-->  
  3.     <finalName>asset-server</finalName>  
  4.     <resources>  
  5.         <!--recources資料夾下的所有檔案都打進jar包-->  
  6.         <resource>  
  7.             <targetPath>${project.build.directory}/classes</targetPath>  
  8.             <directory>src/main/resources</directory>  
  9.             <filtering>true</filtering>  
  10.             <includes>  
  11.                 <include>**/*.xml</include> 
  12.                 <include>**/*.properties</include>  
  13.             </includes>  
  14.         </resource>  
  15.         <!-- 上文有說過,由於我的applicationContext.xml檔案不是在META-INF下,這一段程式碼的作用就是將 applicationContext.xml檔案拷貝到META-INF目錄下-->  
  16.         <resource>  
  17.             <targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>  
  18.             <directory>src/main/resources/spring</directory>  
  19.             <filtering>true</filtering>  
  20.             <includes>  
  21.                 <include>applicationContext.xml</include>  
  22.             </includes>  
  23.         </resource>  
  24.     </resources>  
  25.     <pluginManagement>  
  26.         <plugins>  
  27.             <!-- 解決Maven外掛在Eclipse內執行了一系列的生命週期引起衝突 -->  
  28.             <plugin>  
  29.                 <groupId>org.eclipse.m2e</groupId>  
  30.                 <artifactId>lifecycle-mapping</artifactId>  
  31.                 <version>1.0.0</version>  
  32.                 <configuration>  
  33.                     <lifecycleMappingMetadata>  
  34.                         <pluginExecutions>  
  35.                             <pluginExecution>  
  36.                                 <pluginExecutionFilter>  
  37.                                     <groupId>org.apache.maven.plugins</groupId>  
  38.                                     <artifactId>maven-dependency-plugin</artifactId>  
  39.                                     <versionRange>[2.0,)</versionRange>  
  40.                                     <goals>  
  41.                                         <goal>copy-dependencies</goal>  
  42.                                     </goals>  
  43.                                 </pluginExecutionFilter>  
  44.                                 <action>  
  45.                                     <ignore />  
  46.                                 </action>  
  47.                             </pluginExecution>  
  48.                         </pluginExecutions>  
  49.                     </lifecycleMappingMetadata>  
  50.                 </configuration>  
  51.             </plugin>  
  52.         </plugins>  
  53.     </pluginManagement>  
  54.     <plugins>  
  55.         <!-- 打包jar檔案時,配置manifest檔案,加入lib包的jar依賴 -->  
  56.         <plugin>  
  57.             <groupId>org.apache.maven.plugins</groupId>  
  58.             <artifactId>maven-jar-plugin</artifactId>  
  59.             <configuration>  
  60.                 <classesDirectory>target/classes/</classesDirectory>  
  61.                 <archive>  
  62.                     <manifest>  
  63.                         <mainClass>com.alibaba.dubbo.container.Main</mainClass>  
  64.                         <!-- 打包時 MANIFEST.MF檔案不記錄的時間戳版本 -->  
  65.                         <useUniqueVersions>false</useUniqueVersions>  
  66.                         <addClasspath>true</addClasspath>  
  67.                         <classpathPrefix>lib/</classpathPrefix>  
  68.                     </manifest>  
  69.                     <manifestEntries>  
  70.                         <Class-Path>.</Class-Path>  
  71.                     </manifestEntries>  
  72.                 </archive>  
  73.             </configuration>  
  74.         </plugin>  
  75.         <plugin>  
  76.             <groupId>org.apache.maven.plugins</groupId>  
  77.             <artifactId>maven-dependency-plugin</artifactId>  
  78.             <executions>  
  79.                 <execution>  
  80.                     <id>copy-dependencies</id>  
  81.                     <phase>package</phase>  
  82.                     <goals>  
  83.                         <goal>copy-dependencies</goal>  
  84.                     </goals>  
  85.                     <configuration>  
  86.                         <type>jar</type>  
  87.                         <includeTypes>jar</includeTypes>  
  88.                         <useUniqueVersions>false</useUniqueVersions>  
  89.                         <outputDirectory>  
  90.                             ${project.build.directory}/lib  
  91.                         </outputDirectory>  
  92.                     </configuration>  
  93.                 </execution>  
  94.             </executions>  
  95.         </plugin>  
  96.     </plugins>  
  97. </build></span>