1. 程式人生 > >spring boot配置檔案:application.yml (yml與properties檔案優缺對比)

spring boot配置檔案:application.yml (yml與properties檔案優缺對比)

現在我們的application.properties檔案內容是:

  1. server.port=8090  
  2. server.session-timeout=30  
  3. server.context-path=  
  4. server.tomcat.max-threads=0  
  5. server.tomcat.uri-encoding=UTF-8  
  6. spring.datasource.url = jdbc:mysql://localhost:3306/newbirds  
  7. spring.datasource.username = root  
  8. spring.datasource.password = mymysql  
  9. spring.datasource.driverClassName = com.mysql.jdbc.Driver  
  10. # Specify the DBMS  
  11. spring.jpa.database = MYSQL  
  12. # Show or not log for each sql query  
  13. spring.jpa.show-sql = true  
  14. # Hibernate ddl auto (create, create-drop, update)  
  15. spring.jpa.hibernate.ddl-auto = update  
  16. # Naming strategy  
  17. spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy  
  18. # stripped before adding them to the entity manager)  
  19. spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect  

而官方給的很多demo,都是用yml檔案配置的。

yml檔案的好處,天然的樹狀結構,一目瞭然。不過當時把application.properties 改成 application.yml還是痛苦了一會兒。

下面是置換後的application.yml內容:

  1. server:  
  2.   port: 8090  
  3.   session-timeout: 30  
  4.   tomcat.max-threads: 0  
  5.   tomcat.uri-encoding: UTF-8  
  6. spring:  
  7.   datasource:  
  8.     url : jdbc:mysql://localhost:3306/newbirds  
  9.     username : root  
  10.     password : mymysql  
  11.     driverClassName : com.mysql.jdbc.Driver  
  12.   jpa:  
  13.     database : MYSQL  
  14.     show-sql : true  
  15.     hibernate:  
  16.       ddl-auto : update  
  17.       naming-strategy : org.hibernate.cfg.ImprovedNamingStrategy  
  18.     properties:  
  19.       hibernate:  
  20.         dialect : org.hibernate.dialect.MySQL5Dialect  

注意點:

1,原有的key,例如spring.jpa.properties.hibernate.dialect,按“.”分割,都變成樹狀的配置

2,key後面的冒號,後面一定要跟一個空格

3,把原有的application.properties刪掉。然後一定要執行一下  maven -X clean install