LocaleContextMessageInterpolator
它是个javax.validation.MessageInterpolator插值器,Spring把它和自己的LocaleContext结合起来了~
// @since 3.0 // org.springframework.context.i18n.LocaleContextHolder#getLocale() public class LocaleContextMessageInterpolator implements MessageInterpolator { private final MessageInterpolator targetInterpolator; public LocaleContextMessageInterpolator(MessageInterpolator targetInterpolator) { Assert.notNull(targetInterpolator, "Target MessageInterpolator must not be null"); this.targetInterpolator = targetInterpolator; } @Override public String interpolate(String message, Context context) { return this.targetInterpolator.interpolate(message, context, LocaleContextHolder.getLocale()); } @Override public String interpolate(String message, Context context, Locale locale) { return this.targetInterpolator.interpolate(message, context, locale); } }
Demo Show
想来想去,还是给个Demo非常简单的操作一把吧,此处我以CustomValidatorBean
为例对Bean进行校验:
@Getter @Setter @ToString public class Person { // 错误消息message是可以自定义的 @NotNull(message = "{message} -> 名字不能为null", groups = Simple.class) public String name; @Max(value = 10, groups = Simple.class) @Positive(groups = Default.class) // 内置的分组:default public Integer age; @NotNull(groups = Complex.class) @NotEmpty(groups = Complex.class) private List<@Email String> emails; @Future(groups = Complex.class) private Date start; // 定义两个组 Simple组和Complex组 public interface Simple { } public interface Complex { } }
想容器放入一个校验器:
@Configuration public class RootConfig { @Bean public CustomValidatorBean customValidatorBean() { return new CustomValidatorBean(); } }
使用此校验器校验Person对象(本文为了简单就直接new了哈,当然你也可以是容器内的Bean对象)
@Slf4j @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {RootConfig.class}) public class TestSpringBean { @Autowired private SmartValidator smartValidator; @Test public void test1() { Person person = new Person(); person.setAge(-1); person.setStart(new Date()); Errors errors = new DirectFieldBindingResult(person, "person"); ValidationUtils.invokeValidator(smartValidator, person, errors, Person.Complex.class); System.out.println(errors); } }
打印输出
org.springframework.validation.DirectFieldBindingResult: 3 errors Field error in object 'person' on field 'emails': rejected value [null]; codes [NotEmpty.person.emails,NotEmpty.emails,NotEmpty.java.util.List,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [person.emails,emails]; arguments []; default message [emails]]; default message [不能为空] Field error in object 'person' on field 'start': rejected value [Fri Jul 26 11:12:21 CST 2019]; codes [Future.person.start,Future.start,Future.java.util.Date,Future]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [person.start,start]; arguments []; default message [start]]; default message [需要是一个将来的时间] Field error in object 'person' on field 'emails': rejected value [null]; codes [NotNull.person.emails,NotNull.emails,NotNull.java.util.List,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [person.emails,emails]; arguments []; default message [emails]]; default message [不能为null]
符合预期。
说明:因为前面说了Bean Validation内的校验类大都是线程安全的,包括校验器javax.validation.Validator也是线程安全的~
总结
从这篇文章开始,关于Bean Validation这块就切入进Spring的应用里了。本文主要描述的是一些支持类,我们了解了它可以通过手动完成对Spring Bean的校验,但是在实际应用中显然不会这么去做,毕竟一切都需要崇尚自动化嘛~
下一篇,也就是整个Bean Validation的主菜,也就是真正在企业级·Spring·应用中使用的校验方式分析,也就是大家熟悉的@Valid,@Validated以及级联属性的校验问题,欢迎点赞关注~