From 4ddf3d7709f1205c600b5b0834d27f324da271eb Mon Sep 17 00:00:00 2001 From: gaoqr <13665037151@163.com> Date: Tue, 27 Jan 2026 10:53:52 +0800 Subject: [PATCH] =?UTF-8?q?@NumberValid=E6=B3=A8=E8=A7=A3=E9=80=82?= =?UTF-8?q?=E9=85=8D=E5=A4=9A=E8=AF=AD=E7=A7=8D=E8=8E=B7=E5=8F=96=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E6=97=B6=E7=9A=84=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/validation/NumberValidator.java | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/validation/NumberValidator.java b/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/validation/NumberValidator.java index cc82f4da3..1db3dce95 100644 --- a/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/validation/NumberValidator.java +++ b/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/validation/NumberValidator.java @@ -5,7 +5,6 @@ import org.apache.commons.lang3.StringUtils; import jakarta.validation.ConstraintValidator; import jakarta.validation.ConstraintValidatorContext; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.stereotype.Component; @@ -32,8 +31,11 @@ public class NumberValidator implements ConstraintValidator private int fraction; - @Autowired - private MessageSource messageSource; + private final MessageSource messageSource; + + public NumberValidator(MessageSource messageSource) { + this.messageSource = messageSource; + } @Override public void initialize(NumberValid constraintAnnotation) { @@ -57,8 +59,8 @@ public class NumberValidator implements ConstraintValidator notification = handleLong(value.longValue()); } else if (value instanceof Double) { notification = handleDouble(value.doubleValue()); - } else if (value instanceof BigDecimal) { - notification = handleBigDecimal((BigDecimal) value); + } else if (value instanceof BigDecimal bigvalue) { + notification = handleBigDecimal(bigvalue); } if (StringUtils.isNotEmpty(notification)) { context.disableDefaultConstraintViolation(); // 禁用默认的 message 的值 @@ -78,7 +80,7 @@ public class NumberValidator implements ConstraintValidator if (value < Integer.parseInt(ZERO)) { Locale locale = LocaleContextHolder.getLocale(); return messageSource.getMessage(GREATER_THAN_ZERO_NOTIFICATION, new Object[]{ - messageSource.getMessage(name, null, locale) + getFieldMessage(name, locale) }, locale); } return null; @@ -94,7 +96,7 @@ public class NumberValidator implements ConstraintValidator if (value < Long.parseLong(ZERO)) { Locale locale = LocaleContextHolder.getLocale(); return messageSource.getMessage(GREATER_THAN_ZERO_NOTIFICATION, new Object[]{ - messageSource.getMessage(name, null, locale) + getFieldMessage(name, locale) }, locale); } return null; @@ -108,7 +110,7 @@ public class NumberValidator implements ConstraintValidator */ private String handleDouble(Double value) { Locale locale = LocaleContextHolder.getLocale(); - String fieldName = messageSource.getMessage(name, null, locale); + String fieldName = getFieldMessage(name, locale); if (value < Double.parseDouble(ZERO)) { return messageSource.getMessage(GREATER_THAN_ZERO_NOTIFICATION, new Object[]{fieldName}, locale); } @@ -142,7 +144,7 @@ public class NumberValidator implements ConstraintValidator */ private String handleBigDecimal(BigDecimal value) { Locale locale = LocaleContextHolder.getLocale(); - String fieldName = messageSource.getMessage(name, null, locale); + String fieldName = getFieldMessage(name, locale); if (value.compareTo(BigDecimal.ZERO) < 0) { return messageSource.getMessage(GREATER_THAN_ZERO_NOTIFICATION, new Object[]{fieldName}, locale); } @@ -164,4 +166,17 @@ public class NumberValidator implements ConstraintValidator } return null; } + + /** + * 获取属性描述语种 + * + * @return + */ + private String getFieldMessage(String name, Locale locale) { + try { + return messageSource.getMessage(name, null, locale); + } catch (Exception e) { + return name; + } + } }