我试图在构造函数的参数中使用@Value注释,如下所示:
@Autowired
public StringEncryptor(
@Value("${encryptor.password:\"\"}") String password,
@Value("${encryptor.algorithm:\"PBEWithMD5AndTripleDES\"}") String algorithm,
@Value("${encryptor.poolSize:10}") Integer poolSize,
@Value("${encryptor.salt:\"\"}") String salt) {
...
}
当属性文件存在于类路径中时,属性将完全加载,并且测试执行正常。但是,当我从类路径中删除属性文件时,我预计将使用默认值,例如,poolSize将被设置为10或算法到PBEWithMD5AndTripleDES,但情况并非如此。
通过调试器运行代码(仅在更改@Value(“$ {encryptor.poolSize:10}”之后)时才可以使用)将整数池大小转换为@Value(“$ {encryptor.poolSize:10}”))String poolSize导致NumberFormatExceptions)我发现默认值没有设置,参数的形式如下:
poolSize = $ {encryptor.poolSize:10}或
algorithm = ${encryptor.algorithm:"PBEWithMD5AndTripleDES"}
而不是预期
poolSize = 10或
algorithm = "PBEWithMD5AndTripleDES"
基于SPR-4785,诸如$ {my.property:myDefaultValue}之类的符号应该起作用。但是,对我而言并非如此!
谢谢