spring boot 推薦使用 yaml 格式語言(yml=yaml)來編寫配置文件,從而取代 xml 以及 properties,yaml 語言具有像 json 一樣簡潔明了的特點,但同時具有能夠處理復雜類型數據、文本注釋的特點,非常適合作為配置文件使用。
本文介紹一下在實際開發過程中關於 yaml 語言字符串換行的問題。
yaml 語言在線編輯: https://www.bejson.com/validators/yaml_editor/
yaml源代碼:
multiLineString1: |
line1 hello world
line2 yaml demo
line3 welcome
multiLineString2: >
line1 hello world
line2 yaml demo
line3 welcome
multiLineString3: "line1 hello world\
line2 yaml demo\
line3 welcome"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
轉義之后的代碼:
multiLineString1: 'line1 hello world\nline2 yaml demo\nline3 welcome\n',
multiLineString2: 'line1 hello world line2 yaml demo line3 welcome\n',
multiLineString3: 'line1 hello worldline2 yaml demoline3 welcome',
1
2
3
4
5
從結果可以看出,使用 | 會保留換行符,使用 > 沒有換行符,但是兩行字符串之間會有空格,這兩種是絕大多數查閱到的 yaml 文件說明文檔的寫法,但是在 spring boot 配置文件中是不行的,因為這不再是一個完整的字符串,而是把一個字符串拆成了多段,spring boot 讀取配置文件時會報錯,因此需要使用第三種方式來將一個字符串拆成多行,解析之后仍然是一個完整的字符串。
在 spring boot 中正確配置數據源的姿勢:
## datasource
spring:
datasource:
url: "jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf8&useJDBCCompliantTimezoneShift=true\
&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=true&allowMultiQueries=true&autoReconnect=true"
username: root
password: sasa
driver-class-name: com.mysql.jdbc.Drive
1
2
3
4
5
6
7
8
9
如果是使用 | 或者 > 來處理字符串換行則可能拋出以下異常:
2018-10-10 22:16:00.362 ERROR 4620 --- [nio-8088-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: The connection property 'autoReconnect' only accepts values of the form: 'true', 'false', 'yes' or 'no'. The value 'true"' is not in this set.
### The error may exist in file [D:\develop\repository\git\springBootDemo\demo-dao\target\classes\mapper\usermapper.xml]
### The error may involve com.ljq.demo.springboot.dao.user.UserDao.queryList
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: The connection property 'autoReconnect' only accepts values of the form: 'true', 'false', 'yes' or 'no'. The value 'true"' is not in this set.] with root cause
java.sql.SQLException: The connection property 'autoReconnect' only accepts values of the form: 'true', 'false', 'yes' or 'no'. The value 'true"' is not in this set.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.validateStringValues(ConnectionPropertiesImpl.java:314) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.ConnectionPropertiesImpl$BooleanConnectionProperty.initializeFrom(ConnectionPropertiesImpl.java:91) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.initializeFrom(ConnectionPropertiesImpl.java:216) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.ConnectionPropertiesImpl.initializeProperties(ConnectionPropertiesImpl.java:2538) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.ConnectionImpl.initializeDriverProperties(ConnectionImpl.java:3143) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:762) ~[mysql-connector-java-5.1.47.jar:5.1.47]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
大致意思是 spring 在讀取數據源 url 時並沒有解析到完整的配置,因此需要使用上邊正確的方式來配置。
————————————————
版權聲明:本文為CSDN博主「Flying9001」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/Mrqiang9001/article/details/83002988