所以我有两个参数被Spring @Value
注解注入。 minLength
和maxLength
。逻辑上,minLength
不得高于maxLength
,反之亦然。这是我尝试过的,但失败了。Spring @Value注入字段验证注入字段
将引发异常,最大长度为0
,但在我的application.properties
我将它设置为7
(验证之前测试)
private int minLength;
private int maxLength;
@Min(5)
@Value("${ com.manudcamera.webapi.uuid.minLength : 5} ")
private void setMinLength(int minLength) {
if (minLength > this.maxLength) throw new IllegalArgumentException("minimum length cannot be greater than maximum length | min len: " + minLength + " max len: " + this.maxLength);
this.minLength = minLength;
}
@Max(50)
@Value("${ com.manudcamera.webapi.uuid.maxLength : 15 }")
private void setMaxLength(int maxLength) {
if (maxLength < this.minLength) throw new IllegalArgumentException("maximum length cannot be greather than minimum length | min len: " + minLength + " max len: " + maxLength);
this.maxLength = maxLength;
}
你如何实例化注入'@ Value'的类,使用'new'或者注入其他实例。 – dabaicai
这个类正在被Spring管理(使用'@ Component'注解) – XPLOT1ON