1、在項目開發過程中需要考慮不同的運行環境:開發環境(dev)、測試環境(beta)和生產環境(product)。在以往的開發過程中通常使用Maven構建工具進行控制,但卻需要進行大量的配置。SpringBoot考慮到此類問題,專門設計了profile支持。
備注:尤其是項目開發完畢,在正式環境部署的時候出現問題,進行本地調試的時候就頭疼了,因為正式環境的參數配置都和本地不一樣,所以使用Springboot的profile配置就極大節省了時間,也簡化了操作,方便自己使用,避免了搞不清那些配置是正式的,那些是本地環境的。
2、首先,創建開發環境,測試環境,生產環境的三種配置文件,外加application.properties配置文件,如下所示:
application-beta.properties、application-dev.properties、application-product.properties三個配置文件分別指定項目的端口號為8082、8083、8084,application.properties配置文件指定激活的環境,如下所示:
1 spring.profiles.active=beta
為了使項目正常進行打包,還需要修改pom.xml文件,追加resource配置,主要的功能是進行源文件夾中內容的打包輸出,配置完成后可以將配置文件打包到*.jar文件中。,如下所示:
1 <?xml version="1.0"?> 2 <project 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 4 http://maven.apache.org/xsd/maven-4.0.0.xsd" 5 xmlns="http://maven.apache.org/POM/4.0.0" 6 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 7 <modelVersion>4.0.0</modelVersion> 8 9 <parent> 10 <groupId>com.bie</groupId> 11 <artifactId>springboot-base</artifactId> 12 <version>0.0.1-SNAPSHOT</version> 13 </parent> 14 15 <!-- 父項目已經指定,這里可以省略 --> 16 <!-- <groupId>com.bie</groupId> --> 17 <artifactId>springboot-tentent</artifactId> 18 <!-- <version>0.0.1-SNAPSHOT</version> --> 19 <name>springboot-tentent</name> 20 <url>http://maven.apache.org</url> 21 22 <properties> 23 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 24 </properties> 25 26 <dependencies> 27 <dependency> 28 <groupId>org.springframework.boot</groupId> 29 <artifactId>spring-boot-starter-web</artifactId> 30 </dependency> 31 <dependency> 32 <groupId>org.springframework.boot</groupId> 33 <artifactId>spring-boot-starter-test</artifactId> 34 <scope>test</scope> 35 </dependency> 36 <dependency> 37 <groupId>junit</groupId> 38 <artifactId>junit</artifactId> 39 <scope>test</scope> 40 </dependency> 41 <dependency> 42 <groupId>org.springframework.boot</groupId> 43 <artifactId>spring-boot-starter-jetty</artifactId> 44 </dependency> 45 </dependencies> 46 47 <build> 48 <plugins> 49 <!-- 該插件的主要功能是進行項目的打包發布處理 --> 50 <plugin> 51 <groupId>org.springframework.boot</groupId> 52 <artifactId>spring-boot-maven-plugin</artifactId> 53 <!-- 設置程序執行的主類 --> 54 <configuration> 55 <mainClass>org.springboot.tentent.Springboot01Application</mainClass> 56 </configuration> 57 <executions> 58 <execution> 59 <goals> 60 <goal>repackage</goal> 61 </goals> 62 </execution> 63 </executions> 64 </plugin> 65 </plugins> 66 <resources> 67 <resource> 68 <directory>src/main/resources</directory> 69 <includes> 70 <include>**/*.properties</include> 71 <include>**/*.yml</include> 72 <include>**/*.xml</include> 73 <include>**/*.tld</include> 74 </includes> 75 <filtering>false</filtering> 76 </resource> 77 <resource> 78 <directory>src/main/java</directory> 79 <includes> 80 <include>**/*.properties</include> 81 <include>**/*.xml</include> 82 <include>**/*.tld</include> 83 </includes> 84 <filtering>false</filtering> 85 </resource> 86 </resources> 87 </build> 88 89 90 </project>
修改完pom.xml配置文件之后,記得更新項目,Maven -> Update Project。此時開始,為項目打包,這里直接通過Eclipse進行打包配置,如下所示:
由於在application.properties指定的是spring.profiles.active=beta,所以如果直接運行打好的jar包,那么運行的就是測試環境的端口號,項目打包完成后運行程序,如果不做出任何的指定,那么默認配置的活躍profile(beta)就將直接起作用(java -jar xxx.jar)。
如果想要切換到不同的profile環境,可以在啟動時動態配置(java -jar .\springboot-parent.jar --spring.profiles.active=product),如下所示:
演示效果,如下所示:
注意:*.properties與*.yml配置不同。
使用application.yml進行多profile配置的時候,只需要在一個配置文件中使用“---”分割不同的profile配置。但是此類模式不適合於application.properties配置,此時應該采用不同的*.properties保存不同的配置,才可以實現多profile。