所以我有兩個參數被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