Record dependency conflicts between SpringBoot2.7.3 and Minio’s okhttp package
Problem Description
When minio 8.4.6 is introduced into a project based on Spring Boot 2.7.3, the startup project reports that beans cannot be injected.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'creatMinioClient' defined in class path resource [com/xiaoyu/config/MinIOConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.xiaoyu.utils.MinIOUtils]: Factory method 'creatMinioClient' threw exception; nested exception is java.lang.NoSuchFieldError: Companion
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.22.jar:5.3.22]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486) ~[spring-beans-5.3.22.jar:5.3.22]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352) ~[spring-beans-5.3.22.jar:5.3.22]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195) ~[spring-beans-5.3.22.jar:5.3.22]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.22.jar:5.3.22]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.22.jar:5.3.22]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.22.jar:5.3.22]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.22.jar:5.3.22]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.22.jar:5.3.22]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.22.jar:5.3.22]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) ~[spring-beans-5.3.22.jar:5.3.22]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.22.jar:5.3.22]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.22.jar:5.3.22]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.3.jar:2.7.3]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-2.7.3.jar:2.7.3]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.3.jar:2.7.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.3.jar:2.7.3]
at com.xiaoyu.Application.main(Application.java:27) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.xiaoyu.utils.MinIOUtils]: Factory method 'creatMinioClient' threw exception; nested exception is java.lang.NoSuchFieldError: Companion
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.22.jar:5.3.22]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.22.jar:5.3.22]
... 17 common frames omitted
Caused by: java.lang.NoSuchFieldError: Companion
at okhttp3.internal.Util.<clinit>(Util.kt:70) ~[okhttp-4.9.3.jar:na]
at okhttp3.RequestBody$Companion.create(RequestBody.kt:145) ~[okhttp-4.9.3.jar:na]
at okhttp3.RequestBody$Companion.create$default(RequestBody.kt:143) ~[okhttp-4.9.3.jar:na]
at okhttp3.RequestBody.create(RequestBody.kt) ~[okhttp-4.9.3.jar:na]
at io.minio.S3Base.<clinit>(S3Base.java:104) ~[minio-8.4.6.jar:8.4.6]
at io.minio.MinioClient$Builder.<init>(MinioClient.java:2447) ~[minio-8.4.6.jar:8.4.6]
at io.minio.MinioClient.builder(MinioClient.java:2439) ~[minio-8.4.6.jar:8.4.6]
at com.xiaoyu.utils.MinIOUtils.createMinioClient(MinIOUtils.java:394) ~[classes/:na]
at com.xiaoyu.utils.MinIOUtils.<init>(MinIOUtils.java:45) ~[classes/:na]
at com.xiaoyu.config.MinIOConfig.creatMinioClient(MinIOConfig.java:34) ~[classes/:na]
at com.xiaoyu.config.MinIOConfig$$EnhancerBySpringCGLIB$$b19cb1ad.CGLIB$creatMinioClient$8(<generated>) ~[classes/:na]
at com.xiaoyu.config.MinIOConfig$$EnhancerBySpringCGLIB$$b19cb1ad$$FastClassBySpringCGLIB$$87925a50.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.3.22.jar:5.3.22]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.3.22.jar:5.3.22]
at com.xiaoyu.config.MinIOConfig$$EnhancerBySpringCGLIB$$b19cb1ad.creatMinioClient(<generated>) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_333]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_333]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_333]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_333]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.22.jar:5.3.22]
... 18 common frames omitted
Cause Analysis:
I saw on the Internet that it should be caused by a conflict between the jar packages of okhttp . After investigation, I found that the okhttp version used by minio dependencies is 4.10.0, and the version in spring-boot-dependencies is 4.9.3.
solution:
Directly limit the version in the parent pom to the version corresponding to Minio and return to normal.