Springboot集成BeanValidation扩展二:加载jar中的资源文件
一、需求
今天在搭建Springboot框架的时候,又遇到一个需求:在多模块系统中,有些模块想自己管理BeanValidation的资源文件(默认是启动项目claspath下的 ValidationMessages.properties)。刚开始还天真地认为springboot会不会帮我们做了,结果并没有,于是就是撸源码了。
以下是我的实现和实现原理
二、实现
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
/**
* 当有异常时返回默认的验证器
* @return 返回的是org.springframework.validation.Validator,不是javax.validation.Validator
* 所以返回时要适配一下
*/
@Override
public Validator getValidator() {
//路径匹配
PathMatchingResourcePatternResolver resourcePatternResolver =
new PathMatchingResourcePatternResolver(
MyWebMvcConfigurer.class.getClassLoader());
try {
//匹配属性文件,有个限制,资源文件名称必须包含Validation
Resource[] resources =
resourcePatternResolver.getResources("classpath*:*Validation*.properties");
List<String> files = Arrays.stream(resources)
.filter(resource -> StringUtils.isNotBlank(resource.getFilename()))
.map(resource -> {
String fileName = resource.getFilename();
return fileName.substring(0, fileName.indexOf("."));
}).collect(Collectors.toList());
javax.validation.Validator validator = Validation.byDefaultProvider()
.configure()
//这里可以加载多个文件
.messageInterpolator(new ResourceBundleMessageInterpolator(
new AggregateResourceBundleLocator(files)))
.buildValidatorFactory()
.getValidator();
//适配
return new SpringValidatorAdapter(validator);
} catch (IOException e) {
//发生异常,返回null,springboot框架会采用默认的validator
return null;
}
}
三、实现原理
源码分析
1、定位Bean在什么地方验证的
DispatcherServlet验证Bean的主要源码路径
- RequestResponseBodyMethodProcessor#resolveArgument
- AbstractMessageConverterMethodArgumentResolver#validateIfApplicable
- DataBinder#validate(核心)
- AbstractMessageConverterMethodArgumentResolver#validateIfApplicable
源码:
/**
* 该方法定位:org.springframework.web.servlet.mvc.method.annotation.
RequestResponseBodyMethodProcessor#resolveArgument
* Throws MethodArgumentNotValidException if validation fails.
* @throws HttpMessageNotReadableException if {@link RequestBody#required()}
* is {@code true} and there is no body content or if there is no suitable
* converter to read the content with.
*/
@Override
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory)
throws Exception {
parameter = parameter.nestedIfOptional();
Object arg = readWithMessageConverters(webRequest, parameter,
parameter.getNestedGenericParameterType());
String name = Conventions.getVariableNameForParameter(parameter);
if (binderFactory != null) {
WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name);
if (arg != null) {
//*******校验方法参数是否符合要求*******
//调用链:也就是验证器被调用的地方
validateIfApplicable(binder, parameter);
if (binder.getBindingResult().hasErrors()
&& isBindExceptionRequired(binder, parameter)) {
throw
new MethodArgumentNotValidException(parameter,
binder.getBindingResult());
}
}
if (mavContainer != null) {
mavContainer.addAttribute(
BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
}
}
return adaptArgumentIfNecessary(arg, parameter);
}
/**
* 该方法定位:org.springframework.web.servlet.mvc.method.annotation.
AbstractMessageConverterMethodArgumentResolver#validateIfApplicable
*
* Validate the binding target if applicable.
* <p>The default implementation checks for {@code @javax.validation.Valid},
* Spring's {@link org.springframework.validation.annotation.Validated},
* and custom annotations whose name starts with "Valid".
* @param binder the DataBinder to be used
* @param parameter the method parameter descriptor
* @since 4.1.5
* @see #isBindExceptionRequired
*/
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
Annotation[] annotations = parameter.getParameterAnnotations();
for (Annotation ann : annotations) {
Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
if (validatedAnn != null ||
ann.annotationType().getSimpleName().startsWith("Valid")) {
Object hints = (validatedAnn != null ?
validatedAnn.value() : AnnotationUtils.getValue(ann));
Object[] validationHints = (hints instanceof Object[] ?
(Object[]) hints : new Object[] {hints});
//调用链
binder.validate(validationHints);
break;
}
}
}
/**
* 该方法定位:org.springframework.validation.DataBinder#validate(java.lang.Object...)
*
* Invoke the specified Validators, if any, with the given validation hints.
* <p>Note: Validation hints may get ignored by the actual target Validator.
* @param validationHints one or more hint objects to be passed to a {@link SmartValidator}
* @see #setValidator(Validator)
* @see SmartValidator#validate(Object, Errors, Object...)
* 核心方法
*/
public void validate(Object... validationHints) {
for (Validator validator : getValidators()) {
if (!ObjectUtils.isEmpty(validationHints) && validator instanceof SmartValidator) {
((SmartValidator) validator).validate(getTarget(),
getBindingResult(), validationHints);
}
else if (validator != null) {
validator.validate(getTarget(), getBindingResult());
}
}
}
/**
* 获取验证器(关键就在:this.validators怎么初始化的?)
*/
public List<Validator> getValidators() {
return Collections.unmodifiableList(this.validators);
}
发现:在DataBinder#validate中有验证Bean的核心代码validator.validate(...)
分析到这里关键就是validator在哪赋值的?
2、validators赋值
DataBinder属性validators赋值
private final List validators = new ArrayList<>();//断点跟踪发现:
public void setValidator(@Nullable Validator validator) {
assertValidators(validator);
this.validators.clear();
if (validator != null) {
this.validators.add(validator);
} }
DataBinder#setValidator被调用的位置
- org.springframework.web.bind.support.ConfigurableWebBindingInitializer#initBinder
源码:
@Override
public void initBinder(WebDataBinder binder) {
binder.setAutoGrowNestedPaths(this.autoGrowNestedPaths);
if (this.directFieldAccess) {
binder.initDirectFieldAccess();
}
if (this.messageCodesResolver != null) {
binder.setMessageCodesResolver(this.messageCodesResolver);
}
if (this.bindingErrorProcessor != null) {
binder.setBindingErrorProcessor(this.bindingErrorProcessor);
}
if (this.validator != null && binder.getTarget() != null &&
this.validator.supports(binder.getTarget().getClass())) {
//发现是在这里调用的,下面的问题就是ConfigurableWebBindingInitializer
//中的validator属性在哪初始化的?
//在对应的setValidator方法打断点
binder.setValidator(this.validator);
}
if (this.conversionService != null) {
binder.setConversionService(this.conversionService);
}
if (this.propertyEditorRegistrars != null) {
for (PropertyEditorRegistrar propertyEditorRegistrar :
this.propertyEditorRegistrars) {
propertyEditorRegistrar.registerCustomEditors(binder);
}
}
}
ConfigurableWebBindingInitializer#initBinder被调用的位置
研究发现:ConfigurableWebBindingInitializer#initBinder是在springboot初始化时被调用的
调用链如下:
调用链1:初始化springmvc的requestMappingHandlerAdapter
EnableWebMvcConfiguration#requestMappingHandlerAdapter- super.requestMappingHandlerAdapter();—>WebMvcConfigurationSupport
调用链2:
WebMvcConfigurationSupport#requestMappingHandlerAdapter - adapter.setWebBindingInitializer(getConfigurableWebBindingInitializer());
调用链3:
EnableWebMvcConfiguration#ConfigurableWebBindingInitializer - super.getConfigurableWebBindingInitializer();
调用链4:
WebMvcConfigurationSupport#ConfigurableWebBindingInitializer - mvcValidator(),这个是核心
源码:
/**
* @see org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.
* EnableWebMvcConfiguration#requestMappingHandlerAdapter
*/
@Bean
@Override
public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
//调用链1:调用父类WebMvcConfigurationSupport#requestMappingHandlerAdapter
RequestMappingHandlerAdapter adapter = super.requestMappingHandlerAdapter();
adapter.setIgnoreDefaultModelOnRedirect(this.mvcProperties == null
|| this.mvcProperties.isIgnoreDefaultModelOnRedirect());
return adapter;
} /**
* @seeorg.springframework.web.servlet.config.annotation.
* WebMvcConfigurationSupport#requestMappingHandlerAdapter
*
*/
public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
RequestMappingHandlerAdapter adapter = createRequestMappingHandlerAdapter();
adapter.setContentNegotiationManager(mvcContentNegotiationManager());
adapter.setMessageConverters(getMessageConverters());
//调用链2:EnableWebMvcConfiguration#getConfigurableWebBindingInitializer
adapter.setWebBindingInitializer(getConfigurableWebBindingInitializer());
adapter.setCustomArgumentResolvers(getArgumentResolvers());
adapter.setCustomReturnValueHandlers(getReturnValueHandlers());
...
} /**
* @see springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.
* EnableWebMvcConfiguration#getConfigurableWebBindingInitializer
*
*/
@Override
protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer() {
try {
//这里是不存在实例,报异常
return this.beanFactory.getBean(ConfigurableWebBindingInitializer.class);
}
catch (NoSuchBeanDefinitionException ex) {
//调用链3:WebMvcConfigurationSupport#getConfigurableWebBindingInitializer
return super.getConfigurableWebBindingInitializer();
}
} /**
* @seespringframework.web.servlet.config.annotation.WebMvcConfigurationSupport
* #getConfigurableWebBindingInitializer
*
*/
protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer() {
ConfigurableWebBindingInitializer initializer =
new ConfigurableWebBindingInitializer();
initializer.setConversionService(mvcConversionService());
//调用链4:核心方法mvcValidator()
initializer.setValidator(mvcValidator());
MessageCodesResolver messageCodesResolver = getMessageCodesResolver();
if (messageCodesResolver != null) {
initializer.setMessageCodesResolver(messageCodesResolver);
}
return initializer;
}
- super.requestMappingHandlerAdapter();—>WebMvcConfigurationSupport
3、validator是什么
通过源码分析,找到了关键点就是mvcValidator(),现在对其分析,找出其返回的validator到底是什么?
断点调试时发现mvcValidator()进入了
org.springframework.context.annotation.ConfigurationClassEnhancer.BeanMethodInterceptor#intercept
org.springframework.context.annotation.ConfigurationClassEnhancer.BeanMethodInterceptor#resolveBeanReference
resolveBeanReference方法里有个关键的代码//关键在于 beanFactory.getBean(beanName),name = "mvcValidator",创建该实例
//从而会找到EnableWebMvcConfiguration的mvcValidator方法
//(因为mvcValidator方法上有@Bean,方法名称又与beanName相同,故调用)
Object beanInstance = (useArgs ? beanFactory.getBean(beanName, beanMethodArgs) :
beanFactory.getBean(beanName));
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.EnableWebMvcConfiguration#mvcValidator
终于找到创建validator对象的点了,以下就是如何自己扩展?
继续研究创建validator的源码,寻找关键点@Bean
@Override
public Validator mvcValidator() {
if (!ClassUtils.isPresent("javax.validation.Validator",
getClass().getClassLoader())) {
return super.mvcValidator();
}
//关键在于getValidator()方法
//真正调用的是父类DelegatingWebMvcConfiguration#getValidator
return ValidatorAdapter.get(getApplicationContext(), getValidator());
}
4、关键点:分析getValidator()方法
注意:这里就是我们可以扩展的地方
/**
* springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration#getValidator
*/
@Override
@Nullable
protected Validator getValidator() {
//configurers属性是WebMvcConfigurerComposite的对象
return this.configurers.getValidator();
}
/**
*@see springframework.web.servlet.config.annotation.WebMvcConfigurerComposite#getValidator
*/
@Override
public Validator getValidator() {
Validator selected = null;
//看到WebMvcConfigurer这个东西,我是很激动呀!终于看到曙光了,激动半天
//于是我就自定义了MyWebMvcConfigurer实现WebMvcConfigurer,并重写
//其中的getValidator方法,哈哈,终于找到扩展点了
for (WebMvcConfigurer configurer : this.delegates) {
Validator validator = configurer.getValidator();
if (validator != null) {
if (selected != null) {
throw new IllegalStateException("No unique Validator found: {" +
selected + ", " + validator + "}");
}
selected = validator;
}
}
return selected;
}
通过getValidator()获取自定义的validator后
ValidatorAdapter.get(getApplicationContext(), getValidator());对其包装如下:
/**
* @see springframework.boot.autoconfigure.validation.ValidatorAdapter#get
*/
public static Validator get(ApplicationContext applicationContext,
Validator validator) {
//如果为null(自定义的validator发生异常),返回默认的
if (validator != null) {
//因为非空,会执行该行代码
return wrap(validator, false);
}
return getExistingOrCreate(applicationContext);
}
private static Validator wrap(Validator validator, boolean existingBean) {
if (validator instanceof javax.validation.Validator) {
//执行该代码
if (validator instanceof SpringValidatorAdapter) {
return new ValidatorAdapter((SpringValidatorAdapter) validator,
existingBean);
}
return new ValidatorAdapter(
new SpringValidatorAdapter((javax.validation.Validator) validator),
existingBean);
}
return validator;
}
总结:在分析源码的过程中犯了最大的错误就是:总想什么都搞明白,跟踪每个源码的实现,结果发现还是没搞懂,白白浪费了很多时间。其实在分析源码的过程中,不需要钻牛角尖,把每个都搞懂。你要搞明白你的“关注点“在哪?,不要走着走着就走偏了。很多源码“观其大意”就行,没必要深究,不然就呵呵了。
Springboot集成BeanValidation扩展二:加载jar中的资源文件的更多相关文章
- SpringBoot集成Shiro 实现动态加载权限
一.前言 本文小编将基于 SpringBoot 集成 Shiro 实现动态uri权限,由前端vue在页面配置uri,Java后端动态刷新权限,不用重启项目,以及在页面分配给用户 角色 . 按钮 .ur ...
- 从jar中读取资源文件的问题
在项目中遇到了一个问题,在IDE中读取配置文件的程序,打成架包以后,放到服务器上跑,报找不到资源文件的错误. 其中,资源文件的路径如下: 获取配置文件路径和读取的方法如下: private stati ...
- Springboot集成BeanValidation扩展一:错误提示信息加公共模板
Bean Validator扩展 1.需求 在使用validator时,有个需求就是公用错误提示信息,什么意思? 举个例子: @NotEmpty非空判断,在资源文件中我不想每个非空判断都写”不 ...
- 从webview中加载assets中的html文件
private void readHtmlFormAssets(){ WebSettings webSettings = tipsWebView.getSettings(); webSettings. ...
- Java动态加载jar及class文件
经常碰到需要动态加载jar及class文件的场景.Java类由于需要加载和编译字节码,动态加载class文件较为麻烦,但JDK仍提供了一整套方法来动态加载jar文件和class文件. 一.动态加载ja ...
- Java_Java中动态加载jar文件和class文件
转自:http://blog.csdn.net/mousebaby808/article/details/31788325 概述 诸如tomcat这样的服务器,在启动的时候会加载应用程序中lib目录下 ...
- [转载] Java中动态加载jar文件和class文件
转载自http://blog.csdn.net/mousebaby808/article/details/31788325 概述 诸如tomcat这样的服务器,在启动的时候会加载应用程序中lib目录下 ...
- [Android] Android 用于异步加载 ContentProvider 中的内容的机制 -- Loader 机制 (LoaderManager + CursorLoader + LoaderManager.LoaderCallbacks)
Android 用于异步加载 ContentProvider 中的内容的机制 -- Loader 机制 (LoaderManager + CursorLoader + LoaderManager.Lo ...
- java 从jar包中读取资源文件
在代码中读取一些资源文件(比如图片,音乐,文本等等),在集成环境(Eclipse)中运行的时候没有问题.但当打包成一个可执行的jar包(将资源文件一并打包)以后,这些资源文件找不到,如下代码: Jav ...
随机推荐
- MVC学习笔记---WebViewPage(nop等开源项目的@T)
http://www.cnblogs.com/gyche/p/5597491.html http://www.cnblogs.com/Alex80/p/5369042.html http://www. ...
- static_cast AND dynamic_cast
类型转换是一种机制,让程序员能够暂时或永久性改变编译器对对象的解释.注意,这并不意味着程序员改变了对象本身,而只是改变了对对象的解释. 在很多情况下,类型转换是合理的需求,可解决重要的兼容问题.因此, ...
- CodeForces A. Many Equal Substrings
http://codeforces.com/contest/1029/problem/A You are given a string tt consisting of nn lowercase La ...
- Vue 使用Spread.js没有层级关系(隐藏与显示)
Vue 使用Spread.js没有层级关系(隐藏与显示) 1.vue会给元素加一个监控属性.去掉 spread.js没有层级关系过半是column中值的问题
- html & email template
html & email template inline style build tools https://templates.mailchimp.com/getting-started/h ...
- 262144 (game)
262144 (game) 题目描述 Bessie likes downloading games to play on her cell phone, even though she does fi ...
- 【ZBH选讲·模数和】
[问题描述]你是能看到第二题的friends呢.——laekovHja和Yjq在玩游戏,这个游戏中Hja给了Yjq两个数,希望Yjq找到一些非负整数使得这些数的和等于n,并且所有数模maaaaaaaa ...
- eclipse增加jar包方式对比
add external jars = 增加工程外部的包add jars = 增加工程内包add library = 增加一个库add class folder = 增加一个类文件夹 add jar ...
- VMware Storage VMotion概述及功能
可以跨存储阵列实时迁移虚拟机磁盘文件.VMware Storage VMotion 使您可以在共享存储位置之间和跨共享存储位置重新分配虚拟机磁盘文件,同时保证连续的服务供应和事务处理的完整性. 1.可 ...
- mysql 逻辑架构(三层)
1.客户端(主要处理连接,授权认证,安全等). 2.MYSQL服务器层(核心服务功能都在这层,包括,查询解析,分析,优化,缓存以及所有的内置函数,所有跨存储引擎的功能都在这层实现:存储过程,触发器,视 ...