今天在用spring boot启动服务时发生了如下的异常:
[main][ERROR] (SpringApplication.java:771) Application startup failed
java.lang.AbstractMethodError: org.apache.bval.jsr303.ApacheValidatorFactory.close()V
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:216)
at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:201)
at org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.close(LocalValidatorFactoryBean.java:428)
at org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.destroy(LocalValidatorFactoryBean.java:434)
at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.freeLocalValidator(ConfigurationPropertiesBindingPostProcessor.java:233)
at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.onApplicationEvent(ConfigurationPropertiesBindingPostProcessor.java:220)
at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.onApplicationEvent(ConfigurationPropertiesBindingPostProcessor.java:77)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:393)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:347)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:883)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
…
上面的异常直接原因就是 org.apache.bval.jsr303.ApacheValidatorFactory
(org.apache.bval:bval-jsr303:0.5)没有close
方法.
进一步究期原因是 ApacheValidatorFactory 所实现的接口ValidatorFactory的早期版本是没有close方法的,而spring 4用的 org.hibernate:hibernate-validator:5.3.6.Final
用到的ValidatorFactory版本更高,定义了cloase方法
更进一步,在项目启动时阴错阳差的SPI加载ValidatorFactory实例时先找到了ApacheValidatorFactory,而非spring4需要的HibernateValidatorFactory,所以就报错了。
解决的办法么,也很直接:把org.apache.bval:bval-jsr303
排除掉
总结:
spring4.0 之后不在支持 jsr303,所以如果项目中有混入jsr303就要将之排除掉,类似如下修改pom.xml,
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>faceapi-thriftservice</artifactId>
<version>${faceapi.version}</version>
<!-- spring4.0 之后不再支持 jsr303,如果不排除会导致 AbstractMethodError 异常 -->
<exclusions>
<exclusion>
<groupId>org.apache.bval</groupId>
<artifactId>bval-jsr303</artifactId>
</exclusion>
</exclusions>
</dependency>