Ren*_*cic 10 java spring spring-annotations properties-file spring-boot
我将.properties文件中的属性注入到使用@Value注释的字段中.但是,此属性提供敏感凭据,因此我将其从存储库中删除.我仍然希望如果有人想要运行项目并且没有.properties文件,其凭据将默认值设置为字段.
即使我将默认值设置为字段本身,当.properties文件不存在时,我也会遇到异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxx': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'secret' in string value "${secret}"
Run Code Online (Sandbox Code Playgroud)
这是带注释的字段:
@Value("${secret}")
private String ldapSecret = "secret";
Run Code Online (Sandbox Code Playgroud)
我预计在这种情况下只会设置普通字符串"secret".
Ber*_*enz 10
完全回答你的问题......
@Value("${secret:secret}")
private String ldapSecret;
Run Code Online (Sandbox Code Playgroud)
以下是一些示例完整性的变化......
将String默认为null:
@Value("${secret:#{null}}")
private String secret;
Run Code Online (Sandbox Code Playgroud)
默认数字:
@Value("${someNumber:0}")
private int someNumber;
Run Code Online (Sandbox Code Playgroud)
只需使用:
@Value("${secret:default-secret-value}")
private String ldapSecret;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9925 次 |
| 最近记录: |