你是否爲SpringBoot一個功能多個yml和多個properties文件區分不一樣運行環境配置,常常爲這些配置文件的管理而頭疼,如今經過這篇文章,將完全解決你的煩惱,這篇文篇介紹,怎麼經過yml文件構建多文檔塊,區分不一樣環境配置,自由切換不一樣環境啓動項目,一個配置文件搞定。php
YAML簡介
YAML
是YAML不是一種標記語言的外語縮寫(見前方參考資料原文內容);但爲了強調這種語言以數據作爲中心,而不是以置標語言爲重點,而用返璞詞從新命名。它是一種直觀的可以被電腦識別的數據序列化格式,是一個可讀性高而且容易被人類閱讀,容易和腳本語言交互,用來表達資料序列的編程語言。html
它是相似於標準通用標記語言的子集XML的數據描述語言,語法比XML
簡單不少。前端
基本語法結構
多行縮進java
數據結構能夠用相似大綱的縮排方式呈現,結構經過縮進來表示,連續的項目經過減號「-」
來表示,map
結構裏面的key/value
對用冒號「:」
來分隔。樣例以下:python
house: family: name: Doe parents: - John - Jane children: - Paul - Mark - Simone address: number: 34 street: Main Street city: Nowheretown zipcode: 12345
注意:mysql
1.字串不必定要用雙引號標識; 2.在縮排中空白字符的數目並非很是重要,只要相同階層的元素左側對齊就能夠了(不過不能使用`TAB`字符); 3.容許在文件中加入選擇性的空行,以增長可讀性; 4. 在一個檔案中,可同時包含多個文件,並用`「——」`分隔; 5.選擇性的符號`「...」`能夠用來表示檔案結尾(在利用串流的通信中,這很是有用,能夠在不關閉串流的狀況下,發送結束訊號)。
單行縮寫git
YAML
也有用來描述好幾行相同結構的數據的縮寫語法,數組用'[]'
包括起來,hash
用'{}'
來包括。所以,上面的這個YAML
可以縮寫成這樣:程序員
house: family: { name: Doe, parents: [John, Jane], children: [Paul, Mark, Simone] } address: { number: 34, street: Main Street, city: Nowheretown, zipcode: 12345 }
準備工做
環境:web
windows jdk 8 maven 3.0 IDEA
構建工程
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>cn.zhangbox</groupId> <artifactId>spring-boot-study</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>cn.zhangbox</groupId> <artifactId>spring-boot-02-config</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-02-config</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--導入配置文件處理器,配置文件進行綁定就會有提示--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
修改YML配置
#選擇哪個環境的配置 #這裏能夠在每一個環境配置redis,數據庫(mysql),消息(kafka)等相關的組件的配置 spring: profiles: active: prod #文檔塊區分爲三個--- --- server: port: 8081 spring: profiles: test #文檔塊區分爲三個--- --- server: port: 8082 spring: profiles: test #文檔塊區分爲三個--- --- server: port: 8083 spring: profiles: prod
建立啓動類
@SpringBootApplication public class SpringBootConfigApplication { public static void main(String[] args) { SpringApplication.run(SpringBootConfigApplication.class, args); } }
控制檯打印
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.9.RELEASE) 2018-07-04 15:07:26.214 INFO 14812 --- [ main] c.z.s.SpringBootConfigApplication : Starting SpringBootConfigApplication on MS-20180428GSYE with PID 14812 (C:\Users\Administrator\Desktop\spring-boot-02-config\target\classes started by Administrator in C:\Users\Administrator\Desktop\spring-boot-02-config) 2018-07-04 15:07:26.219 INFO 14812 --- [ main] c.z.s.SpringBootConfigApplication : The following profiles are active: prod 2018-07-04 15:07:26.281 INFO 14812 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3f57bcad: startup date [Wed Jul 04 15:07:26 GMT+08:00 2018]; root of context hierarchy 2018-07-04 15:07:28.988 INFO 14812 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8083 (http) 2018-07-04 15:07:29.029 INFO 14812 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-07-04 15:07:29.031 INFO 14812 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.23 2018-07-04 15:07:29.184 INFO 14812 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-07-04 15:07:29.184 INFO 14812 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2909 ms 2018-07-04 15:07:29.419 INFO 14812 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2018-07-04 15:07:29.424 INFO 14812 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2018-07-04 15:07:29.424 INFO 14812 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-07-04 15:07:29.424 INFO 14812 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2018-07-04 15:07:29.424 INFO 14812 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2018-07-04 15:07:30.605 INFO 14812 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3f57bcad: startup date [Wed Jul 04 15:07:26 GMT+08:00 2018]; root of context hierarchy 2018-07-04 15:07:30.731 INFO 14812 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2018-07-04 15:07:30.732 INFO 14812 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2018-07-04 15:07:30.777 INFO 14812 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-07-04 15:07:30.777 INFO 14812 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-07-04 15:07:30.833 INFO 14812 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-07-04 15:07:31.166 INFO 14812 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-07-04 15:07:31.470 INFO 14812 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8083 (http) 2018-07-04 15:07:31.475 INFO 14812 --- [ main] c.z.s.SpringBootConfigApplication : Started SpringBootConfigApplication in 5.897 seconds (JVM running for 7.324)
至此YML多文檔塊多環境配置是否是很是簡單,切換環境只須要修改面試
spring: profiles: active: prod
中active對應的環境的值便可。
源碼地址
寫在最後
歡迎關注、喜歡、和點贊後續將推出更多的SpringBoot教程,敬請期待。 歡迎關注個人微信公衆號獲取更多更全的學習資源,視頻資料,技術乾貨!
公衆號回覆「學習」,拉你進程序員技術討論羣,乾貨資源第一時間分享。
公衆號回覆「視頻」,領取800GJava視頻學習資源。
公衆號回覆「全棧」,領取1T前端,Java,產品經理,微信小程序,Python等資源合集大放送。
公衆號回覆「慕課」,領取1T慕課實戰學習資源。
公衆號回覆「實戰」,領取750G項目實戰學習資源。
公衆號回覆「面試」,領取8G面試實戰學習資源。