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; + } + } }