缘起
最近公司项目要求JDK从8升到17,SpringBoot版本从2.x升级到3.x,期间遇到了一个诡异的FileNotFoundException异常,日志如下(敏感信息使用xxx脱敏)
1org.springframework.beans.factory.BeanDefinitionStoreException: 2 at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:179) 3 at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:397) 4 at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:283) 5 at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:344) 6 at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:115) 7 at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:745) 8 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:565) 9 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) 10 at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) 11 at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:432) 12 at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) 13 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) 14 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) 15 at com.xxx.xxx.OperationAnalysisApplication.main(OperationAnalysisApplication.java:34) 16Caused by: java.io.FileNotFoundException: 17 at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:211) 18 at org.springframework.core.type.classreading.SimpleMetadataReader.getClassReader(SimpleMetadataReader.java:54) 19 at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:48) 20 at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:103) 21 at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.createMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:86) 22 at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.getMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:73) 23 at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:81) 24 at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:611) 25 at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getSuperClass(ConfigurationClassParser.java:924) 26 at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:335) 27 at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:244) 28 at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:189) 29 at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:298) 30 at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:244) 31 at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:197) 32 at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:165) 33 ... 13 common frames omitted 34
SpringBoot3.x已经删除了类WebMvcConfigurerAdapter,建议实现接口WebMvcConfigurer
过程
1. 查找当前项目中使用WebMvcConfigurerAdapter的地方
这个报错日志明显没有提示出哪个类有问题,先排除当前项目自己写的代码有没有这个类,于是在IDEA中使用全局查找来查找使用WebMvcConfigurerAdapter的地方,发现都被WebMvcConfigurer接口替代了,看来并不是自己写的代码里面的,二是某个Jar中的,这大概就是没有显示因为哪个类继承了WebMvcConfigurerAdapter导致异常的原因吧
2. 查找哪个Jar中引用了这个类
下个断点看看
这TM和大海捞针没区别啊,这项目光依赖自己的包就几十个,大部分都是公司内部包,我都没有源码,这可怎么找啊,只能去报异常的地方碰碰运气了。我找到了报异常的地方: 日志显示在这
1at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:211)
源码如下:
1public InputStream getInputStream() throws IOException { 2 InputStream is; 3 if (this.clazz != null) { 4 is = this.clazz.getResourceAsStream(this.path); 5 } else if (this.classLoader != null) { 6 is = this.classLoader.getResourceAsStream(this.absolutePath); 7 } else { 8 is = ClassLoader.getSystemResourceAsStream(this.absolutePath); 9 } 10 11 if (is == null) { 12 throw new FileNotFoundException(this.getDescription() + " cannot be opened because it does not exist"); 13 } else { 14 return is; 15 } 16}
报异常的地方很明显了,就是这
1 if (is == null) { 2 throw new FileNotFoundException(this.getDescription() + " cannot be opened because it does not exist"); 3 }
在这里下断点,启动项目,发现这个类里this.clazz是null,this.classLoader不是null,感觉这个classloader应该就是加载某个类的时候发现它的父类是WebMvcConfigurerAdapter,去加载WebMvcConfigurerAdapter找不到才报错的。就去这个classloader里面翻找了一下,感觉最有价值的就是里面的classes属性了,如下:

当前实例中的classes中找找
里面居然已经加载了5285个类,一个一个的看显然不行,于是在classes上右键,然后选择评估表达式,想着这个classes是个ArrayList,使用classes.stream().filter()筛选一下加载的类里面是否有父类是WebMvcConfigurerAdapter的,找到了不就知道是哪个有问题了么。写好筛选条件后执行,报错如下:
这.....大概是JDK里面很多类还没加载导致的吧,看来这条行不通了。于是考虑这个classes是ArrayList类型,有序列表啊,其中最后一个元素有没有可能是当前继承了WebMvcConfigurerAdapter的类么?于是使用评估表达式获取了最后一个元素,很遗憾,是个idea的类:
梦想再一次破灭了。
是不是当前类加载器能提供点什么信息
想着是不是当前类加载器能看到当前在加载哪个类或者哪个类的加载引起了当前类的加载啊?很遗憾,classloader根本没有这信息,只有当前类加载器正在加载的这个类WebMvcConfigurerAdapter。
看看谁触发了当前类加载
很遗憾,上面的方法都没找到很有用的信息,于是想着,看看谁调用了当前方法,也就是说谁触发了加载WebMvcConfigurerAdapter这个类的动作,于是顺着调用栈:
一直往回找,终于,在下面这个地方找到了:
这里点开resource字段,直接定位到类:
1import com.xxx.data.analytics.interceptor.RequestDataInterceptor; 2import org.springframework.context.annotation.Configuration; 3import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 4import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 5 6@Configuration 7public class InterceptorConfig extends WebMvcConfigurerAdapter { 8 public InterceptorConfig() { 9 } 10 11 public void addInterceptors(InterceptorRegistry registry) { 12 registry.addInterceptor(new RequestDataInterceptor()).addPathPatterns(new String[]{"/**"}); 13 super.addInterceptors(registry); 14 } 15}
就是这了。
Jar里面没法排掉某个特定的类,只能在当前项目中创建一个和这个类一模一样的类,代码改成自己想要的。因为Java中,某个类(同一个classloader+全限定名)已经被加载了,就不会再次被加载,项目中的类加载优先于Jar包中的,又都是同一个类加载器加载,所以会优先加载到项目中类,Jar中的则不会被加载了,曲线救国 我试了下似乎不行,待考证,暂时先加删除线,后续会回来看看怎么解决这个问题。
-------------------------------------------------------->分割线20230729<------------------------------------------------------------------------------
最后在项目里创建了一个package :org.springframework.web.servlet.config.annotation然后把WebMvcConfigurerAdapter拷贝进来,把InterceptorConfig 在SpringBoot启动类上设置为不扫描,把问题解决了。不创建包org.springframework.web.servlet.config.annotation并拷贝WebMvcConfigurerAdapter直接在SpringBoot启动类上设置为不扫描是不行的,因为你设置InterceptorConfig 不扫描时JVM会首先加载这个类,这个类有父类就先尝试加载父类WebMvcConfigurerAdapter,父类不存在,直接报错。