我有以下 application.yml :
sftp:
sessionFactory:
host: ${HOST:127.0.0.1}
username: ${USERNAME:test}
password: ${PASSWORD: test}
密码以空格开头。启动Spring Boot应用程序时,它崩溃了:
Caused by: mapping values are not allowed here
in 'reader', line 26, column 25:
password: ${PASSWORD: test}
尝试使用 $ {PASSWORD:'test'} 时,密码的值为 password ='test'而不是 password = test
在介绍环境变量之前, application.yml 看起来像这样:
sftp:
sessionFactory:
host: 127.0.0.1
username: test
password: ' test'
一切正常。
知道如何指定以空格开头的默认值吗? 感谢。
答案 0 :(得分:4)
SpEL允许您组合原始字符串和操作符。在您的情况下,您只需要一个普通的字符串,以便您可以像这样定义您的属性:
password: ${PASSWORD:#{' test'}}
application.yaml
testing:
my:
username: ${USERNAME:user}
password: ${PASSWORD:#{' test'}}
Testing.java
@Component
public class Testing {
@Value("${testing.my.username}")
String username;
@Value("${testing.my.password}")
String password;
@PostConstruct
public void post() {
System.out.println("User: (" + username + ")");
System.out.println("Pass: (" + password + ")");
}
日志
User: (user)
Pass: ( test)