Validator结构

在SpringBoot中使用javax.validation.Validator做校验时,因为涉及到国际化,想要改变语言环境,但没有发现改变的地方。所以就对整个校验的过程大概做了分析,并记录下学习心得,仅供参考。
下图是大概的结构图。

(1)入口类一般LocaleValidatorFactoryBean类,这个类中封装了 MessageInterpolator、ValidatorFactory等对象
(2)MessageInterpolator 类的实例是LocaleContextMessageInterpolator和AbstractMessageInterpolator,这个地方使用了代理模式,其中LocaleContextMessageInterpolator是代理类, AbstractMessageInterpolator是被代理类。
语言环境的设置, 就是在LocaleContextMessageInterpolator中。通过LocaleContextHolder.getLocale来获取Locale对象。 所以,改变LocaleContextHolder中的Locale,就可以改变语言环境。可以设置一个拦截器,每次从request中读取language参数,并更新LocaleContextHolder。
(3)根据是否有插件表达式,AbstractMessageInterpolator又可以分为两类:ParameterMessageInterpolator和ResourceBundleMessageInterpolator。 ResourceBundleMessageInterpolator会绑定指定的MessageSource, 实现多语言处理。

你可能感兴趣的:(★JAVA,java,Validator)