情况
我正在将.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}"以下是带注释的字段:
@Value("${secret}")
private String ldapSecret = "secret";在这种情况下,我预计只会设置简单的字符串“机密”。
发布于 2020-02-14 00:53:12
实际上,默认值将始终被使用。为了克服这个问题,我使用了一个字符串值。
@Value("${prop}")
String propValue;//if no prop defined, the propValue is set to the literal "${prop}"
....
if("${prop}".equals(propValue)) {
propValue=defaultValue
}https://stackoverflow.com/questions/43238583
复制相似问题