Spring Boot迁移JDK8时遇NoSuchFieldError: Companion错误求助
Spring Boot迁移至JDK8兼容版本后ApplicationInsights启动报错:NoSuchFieldError: Companion
错误日志
ightsTelemetryAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'azure.application-insights-com.microsoft.applicationinsights.boot.ApplicationInsightsProperties': Initialization of bean failed; nested exception is java.lang.NoSuchFieldError: Companion at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:165) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:175) at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:155) at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:97) at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:174) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5218) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:753) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:727) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:695) at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1016) at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1903) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:750) Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webRequestTrackingFilterRegistrationBean' defined in class path resource [com/microsoft/applicationinsights/boot/ApplicationInsightsWebMvcAutoConfiguration.class]: Unsatisfied dependency expressed through method 'webRequestTrackingFilterRegistrationBean' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.microsoft.applicationinsights.boot.ApplicationInsightsTelemetryAutoConfiguration': Unsatisfied dependency expressed through constructor paramet er 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'azure.application-insights-com.microsoft.applicationinsights.boot.ApplicationInsightsProperties': Initialization of bean failed; nested exception is java.lang.NoSuchFieldError: Companion at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:541) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
当前依赖配置
<dependency> <groupId>com.microsoft.azure</groupId> <artifactId>applicationinsights-spring-boot-starter</artifactId> <version>2.6.4</version> </dependency> <dependency> <groupId>com.microsoft.azure</groupId> <artifactId>applicationinsights-logging-logback</artifactId> <version>2.6.4</version> </dependency>
已更新构件版本
| artifact | previous version | updated version |
|---|---|---|
| spring-boot-starter-parent | 2.2.4.RELEASE | 2.7.12 |
| spring-cloud-dependencies | Finchley.M9 | 2021.0.3 |
| applicationinsights-spring-boot-starter | 1.0.0-BETA | 2.6.4 |
| application-insights-logging | 2.1.1 | 2.6.4 |
解决方案建议
- 排查依赖冲突:
NoSuchFieldError: Companion多由依赖版本不匹配导致,执行mvn dependency:tree分析依赖树,检查是否有旧版本的ApplicationInsights相关jar包(如applicationinsights-core)被间接引入,确保所有ApplicationInsights相关依赖统一为2.6.4版本。 - 验证兼容性匹配:Spring Boot 2.7.x与ApplicationInsights 2.6.4理论兼容,但需检查Spring Cloud组件是否引入了冲突的监控依赖,可尝试在
applicationinsights-spring-boot-starter中排除冲突的依赖模块。 - 检查Kotlin依赖版本:
Companion字段属于Kotlin伴生对象,若项目引入Kotlin相关依赖,需确保其版本与ApplicationInsights依赖的Kotlin版本一致,避免跨版本调用导致字段缺失。 - 清理缓存重建:执行
mvn clean install -U清理本地Maven缓存并强制更新依赖,防止旧缓存的jar包引发问题。 - 尝试降级版本:若2.6.4仍存在问题,可尝试降级至2.6.3或2.5.13版本,这些版本对Spring Boot 2.7的兼容性经过验证,大概率能解决冲突。
内容的提问来源于stack exchange,提问作者SoulChill19
相关产品推荐
相关产品推荐

