我正在使用 @Value
将属性文件中的参数注入(inject)应用程序中的变量。
因为我不是应用程序中的唯一用户,而且我想确保注入(inject)安全,所以我需要在注入(inject)前验证参数。
示例:
属性文件:
example.string=str1,str2
应用程序.java
@Value(${example.string})
public String example;
例如,在这种情况下预期的行为是抛出异常,因为我假设在数组情况下使用“,”id 分隔符
最佳答案
我认为您不能直接使用 @Value
。但是你可以做这样的事情,如果验证失败,它会在启动时失败:
@Validated
@ConfigurationProperties(prefix="my.prefix")
public class AppProperties {
//Validation annotations here
//@NotEmpty
//@MyCustomValidation
private String exampleString;
// getters / setters
}
关于java - 有没有办法在 spring boot 中验证@Value?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65436119/