Spring 字符串连接可以用于包含SpEL的应用程序yml值吗?
我试图定义一个spring数据源url,如下所示:Spring 字符串连接可以用于包含SpEL的应用程序yml值吗?,spring,yaml,applicationcontext,spring-el,Spring,Yaml,Applicationcontext,Spring El,我试图定义一个spring数据源url,如下所示: spring: datasource: url: "jdbc:${vcap.services.compose-for-mysql.credentials.uri}?useSSL=true&requireSSL=true&verifyServerCertificate=true" username: ${vcap.services.compose-for-mysql.credentials.username}
spring:
datasource:
url: "jdbc:${vcap.services.compose-for-mysql.credentials.uri}?useSSL=true&requireSSL=true&verifyServerCertificate=true"
username: ${vcap.services.compose-for-mysql.credentials.username}
password: ${vcap.services.compose-for-mysql.credentials.password}
driver-class-name: com.mysql.jdbc.Driver
其中vcap.services.composeformysql.credentials.uri
设置为mysql://xxxx:xxxx@xxxx.1.dblayer.com:28018/compose
我需要url如下所示:
jdbc:mysql://xxxx:xxxx@xxxx.1.dblayer.com:28018/compose?usesssl=true&requireSSL=true&verifyServerCertificate=true
但是,Spring似乎无法处理此问题:
无法获取JDBC连接;嵌套异常是java.sql.SQLException:Driver:com.mysql.jdbc。Driver@6c6efbc8为URL返回null:jdbc:${vcap.services.compose for mysql.credentials.uri}?usesl=true&requireSSL=true&verifyServerCertificate=true
有没有一种方法可以使用yaml文件构造url,或者需要使用另一种方法,例如xml配置
更新 我试过:
url:${'jdbc:'}${vcap.services.compose for mysql.credentials.uri}{'?usesssl=true&requireSSL=true&verifyServerCertificate=true'}
但是得到错误:
java.lang.IllegalArgumentException:URL必须以“jdbc”开头
还尝试:
url:jdbc:${vcap.services.compose for mysql.credentials.uri}?usesssl=true&requireSSL=true&verifyServerCertificate=true
但是得到错误:
驱动程序:com.mysql.jdbc。Driver@567443abURL:jdbc:${vcap.services.compose for mysql.credentials.uri}?usesssl=true&requireSSL=true&verifyServerCertificate=true返回null
是同一属性文件中的vcap.services…
值吗?否,spring boot从环境变量try${env:vcap.service…}
生成驱动程序:com.mysql.jdbc。Driver@54a388bURL:jdbc:vcap.services.compose for mysql.credentials.uri?usesssl=true&requireSSL=true&verifyServerCertificate=true返回null
Maybe${sys:vcap.service…}
但这没有意义,因为它是针对系统属性而不是环境变量的。。。