我尝试在构造函数的参数中使用 @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}") Integer poolSize
to @Value("${encryptor.poolSize:10}") String poolSize
因为它导致 NumberFormatExceptions)我发现默认值没有被设置,参数的形式如下:
poolSize = ${encryptor.poolSize:10}
or
algorithm = ${encryptor.algorithm:"PBEWithMD5AndTripleDES"}
而不是预期的
poolSize = 10
or
algorithm = "PBEWithMD5AndTripleDES"
基于SPR-4785 https://jira.springsource.org/browse/SPR-4785${my.property:myDefaultValue} 等符号应该有效。但这并没有发生在我身上!
谢谢