spring+maven不同环境读取配置详解

spring+maven不同环境读取配置详解

转载:https://blog.csdn.net/xiao__miao/article/details/78460725

首先这个我看了网上很多资料,但我发现,由于自己一些技术的不熟悉,对于他人的文章有些误解,导致我打包部署失败。

话不多说,现在我们开始一步步工程

第一步,项目读取不通环境的配置文件

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xmlns:util="http://www.springframework.org/schema/util"

  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

  8. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

  9. <!-- 测试环境配置文件 -->

  10. <beans profile="local">

  11. <context:property-placeholder location="classpath:local/config.properties" />

  12. </beans>

  13.  
  14. <!-- 生产环境配置文件 -->

  15. <beans profile="product">

  16. <context:property-placeholder location="classpath:product/config.properties" />

  17. </beans>

  18.  
  19.  
  20.  
  21. </beans>

这是我定义的一个读配置文件的xml——applicationContext-profile.xml

这是由web.xml里面配置,来区分去读哪个配置文件

 
  1. <!-- 配置spring的默认profile

  2. 可选值:product(生产环境) local(本地环境) -->

  3. <context-param>

  4. <param-name>spring.profiles.active</param-name>

  5. <param-value>product</param-value>

  6. </context-param>

  7.  
  8. <!-- spring hibernate -->

  9. <context-param>

  10. <param-name>contextConfigLocation</param-name>

  11. <param-value>

  12. classpath:public/spring/applicationContext-profile.xml

  13. classpath:public/spring/spring-hibernate.xml

  14. </param-value>

  15. </context-param>

web.xml的一部分,根据配置去读product还是local,这里根据配置字段能看到对于的配置文件,这对应关系应该是能看懂的!

到这里,你应该能根据自己的配置文件去运行程序

第二步,根据pom文件打包

 
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  3. <parent>

  4. <artifactId>officialProject</artifactId>

  5. <groupId>com.wm</groupId>

  6. <version>1.0-SNAPSHOT</version>

  7. </parent>

  8. <modelVersion>4.0.0</modelVersion>

  9.  
  10. <artifactId>offical-web</artifactId>

  11. <packaging>war</packaging>

  12.  
  13. <name>offical-web</name>

  14. <url>http://maven.apache.org</url>

  15.  
  16. <properties>

  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  18. <shiro-version>1.3.2</shiro-version>

  19. </properties>

  20.  
  21. <dependencies>

  22. <dependency>

  23. <groupId>junit</groupId>

  24. <artifactId>junit</artifactId>

  25. <version>4.11</version>

  26. <scope>test</scope>

  27. </dependency>

  28. <dependency>

  29. <groupId>org.springframework</groupId>

  30. <artifactId>spring-webmvc</artifactId>

  31. <version>${spring.version}</version>

  32. </dependency>

  33. <dependency>

  34. <groupId>com.wm</groupId>

  35. <artifactId>offical-manager-service</artifactId>

  36. <type>jar</type>

  37. </dependency>

  38. <dependency>

  39. <groupId>com.wm</groupId>

  40. <artifactId>official-manager-core</artifactId>

  41. <type>jar</type>

  42. </dependency>

  43. <dependency>

  44. <groupId>com.wm</groupId>

  45. <artifactId>offical-common</artifactId>

  46. <type>jar</type>

  47. </dependency>

  48. <!-- mysql数据库驱动 -->

  49. <dependency>

  50. <groupId>mysql</groupId>

  51. <artifactId>mysql-connector-java</artifactId>

  52. <version>5.1.37</version>

  53. </dependency>

  54.  
  55. <!--后台权限控制框架-->

  56. <dependency>

  57. <groupId>org.apache.shiro</groupId>

  58. <artifactId>shiro-core</artifactId>

  59. <version>${shiro-version}</version>

  60. </dependency>

  61. <dependency>

  62. <groupId>org.apache.shiro</groupId>

  63. <artifactId>shiro-spring</artifactId>

  64. <version>${shiro-version}</version>

  65. </dependency>

  66.  
  67. </dependencies>

  68.  
  69.  
  70. <profiles>

  71. <profile>

  72. <id>local</id><!-- 本地开发环境 -->

  73. <properties>

  74. <package.env>local</package.env>

  75. </properties>

  76. <activation>

  77. <activeByDefault>true</activeByDefault>

  78. </activation>

  79. </profile>

  80. <profile>

  81. <id>product</id><!-- 生产环境 -->

  82. <properties>

  83. <package.env>product</package.env>

  84. </properties>

  85. </profile>

  86. </profiles>

  87.  
  88.  
  89. <build>

  90. <finalName>${project.artifactId}</finalName>

  91.  
  92. <resources>

  93. <resource>

  94. <directory>src/main/resources</directory>

  95. <filtering>true</filtering>

  96. <excludes>

  97. <exclude>local/*</exclude>

  98. <exclude>product/*</exclude>

  99. </excludes>

  100. <includes>

  101. <include>public/**/*</include>

  102. </includes>

  103. </resource>

  104. <resource>

  105. <directory>src/main/java</directory>

  106. <includes>

  107. <include>com/wm/back/entity/**/hbm/*.hbm.xml</include>

  108. </includes>

  109. </resource>

  110. </resources>

  111. <plugins>

  112. <!-- jetty插件 -->

  113. <plugin>

  114. <groupId>org.eclipse.jetty</groupId>

  115. <artifactId>jetty-maven-plugin</artifactId>

  116. <version>9.3.8.v20160314</version>

  117. <configuration>

  118. <webAppConfig>

  119. <contextPath>/</contextPath>

  120. <defaultsDescriptor>src/main/resources/public/webdefault.xml</defaultsDescriptor>

  121. </webAppConfig>

  122. </configuration>

  123. </plugin>

  124.  
  125.  
  126. <plugin>

  127. <groupId>org.apache.maven.plugins</groupId>

  128. <artifactId>maven-war-plugin</artifactId>

  129. <version>2.1.1</version>

  130. <configuration>

  131. <archive>

  132. <addMavenDescriptor>false</addMavenDescriptor>

  133. </archive>

  134. <warName>${project.artifactId}</warName>

  135. <webResources>

  136. <!-- 不同的环境,使用不同的配置文件 -->

  137. <resource>

  138. <directory>src/main/resources/${package.env}</directory>

  139. <targetPath>WEB-INF/classes/${package.env}</targetPath>

  140. <filtering>true</filtering>

  141. </resource>

  142. <!-- 公共的配置文件 -->

  143. <resource>

  144. <directory>src/main/resources/public</directory>

  145. <!--targetPath用来指定文件放到哪里-->

  146. <targetPath>WEB-INF/classes/public</targetPath>

  147. <filtering>true</filtering>

  148. </resource>

  149. <resource>

  150. <directory>src/main/webapp/WEB-INF/config</directory>

  151. <targetPath>WEB-INF</targetPath>

  152. <filtering>true</filtering>

  153. <includes>

  154. <include>web.xml</include>

  155. </includes>

  156. </resource>

  157. </webResources>

  158. </configuration>

  159. </plugin>

  160.  
  161.  
  162. </plugins>

  163. </build>

  164. </project>

先贴出全部代码,然后再一一作出解释

<profiles>
    <profile>
        <id>local</id><!-- 本地开发环境 -->
        <properties>
            <package.env>local</package.env>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>product</id><!-- 生产环境 -->
        <properties>
            <package.env>product</package.env>
        </properties>
    </profile>
</profiles>

properties里配置的,下文都是可以使用的,我这里配置package.env  ,所以下面使用package.env就是代表local或者product根据我传入的 -P后面的那个值来决定
打包语句是mvn clean package -P +(我传入的值 profile的id)来决定package.env的变量

完整的打包语句是mvn clean package -P local

<activeByDefault>true</activeByDefault>

来决定激活哪个profile

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <excludes>
            <exclude>local/*</exclude>
            <exclude>product/*</exclude>
        </excludes>
        <includes>
            <include>public/**/*</include>
        </includes>
    </resource>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>com/wm/back/entity/**/hbm/*.hbm.xml</include>
        </includes>
    </resource>
</resources>

在我的理解下,不加resources的话,所有项目里的resources文件都会被加载,但java里面的配置文件可能还是加载不了的
但你定义了resources,你就要把你需要通过加载的文件都要写出来,不然他不会去加载。

我这里把各环境的配置文件都取消了加载,把通用的资源文件进行了加载。

忘记提一句,这样下,项目本地启动的时候会报错,因为你把local或product都去掉了,程序找不到里面的文件。这只是打包的时候才试用

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
    <configuration>
        <archive>
            <addMavenDescriptor>false</addMavenDescriptor>
        </archive>
        <warName>${project.artifactId}</warName>
        <webResources>
            <!-- 不同的环境,使用不同的配置文件 -->
            <resource>
                <directory>src/main/resources/${package.env}</directory>
                <targetPath>WEB-INF/classes/${package.env}</targetPath>
                <filtering>true</filtering>
            </resource>
            <!-- 公共的配置文件 -->
            <resource>
                <directory>src/main/resources/public</directory>
                <!--targetPath用来指定文件放到哪里-->
                <targetPath>WEB-INF/classes/public</targetPath>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/webapp/WEB-INF/config</directory>
                <targetPath>WEB-INF</targetPath>
                <filtering>true</filtering>
                <includes>
                    <include>web.xml</include>
                </includes>
            </resource>
        </webResources>
    </configuration>
</plugin>

打war包的时候需要这样配置
directory:代表你要加载的目录
targetPath:代表你要把你加载的文件放在哪里

<filtering>true</filtering>

这个网上说一定要设置为true,在我看来没什么关系,因为我根据命令打包,也能把文件打出来

  <resource>
                <directory>src/main/webapp/WEB-INF/config</directory>
                <targetPath>WEB-INF</targetPath>
                <filtering>true</filtering>
                <includes>
                    <include>web.xml</include>
                </includes>
            </resource>

我这里是为了把web.xml选择读哪个配置文件给替换掉

为什么要这样设计能,因为如果不这样,我们本地启动项目,pom是不会吧${package.env}这个替换的。所以这样会报错。

但我们打包的时候希望灵活替换。所以就写一个config文件目录,里面放过web.xml,当要打包的时候替换WEB-INF下的web.xml文件就可以了

到此,一个完整的流程就走完了

猜你喜欢

转载自blog.csdn.net/u013488847/article/details/82560544