Spring Boot从2.4.6升级至2.5.4出现IllegalAccessError启动失败如何解决
你遇到的启动报错核心异常如下:
启动ApplicationContext出错,如需展示条件评估报告,请开启'debug'模式重新运行应用。2021-09-28 14:10:37 [main] ERROR o.s.boot.SpringApplication - 应用运行失败
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'supplierInitializer' defined in class path resource [org/springframework/cloud/stream/function/FunctionConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'inMemorySwaggerResourcesProvider_integrationflow.org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean#0': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'integrationGlobalProperties' defined in class path resource [org/springframework/boot/autoconfigure/integration/IntegrationAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.integration.context.IntegrationProperties]: Factory method 'integrationGlobalProperties' threw exception; nested exception is java.lang.IllegalAccessError: class org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration tried to access private method 'void org.springframework.integration.context.IntegrationProperties.()' (org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration and org.springframework.integration.context.IntegrationProperties are in unnamed module of loader 'app')
该问题本质是依赖版本不兼容,具体原因:
- Spring Boot 2.4.x默认适配Spring Integration 5.4.x版本,该版本中
IntegrationProperties类的无参构造方法为私有 - Spring Boot 2.5.x默认适配Spring Integration 5.5.x版本,自带的
IntegrationAutoConfiguration自动配置类需要调用IntegrationProperties的公有构造方法,该方法仅在5.5.x及以上版本提供 - 同时项目中使用的Spring Cloud Stream、Swagger集成组件都和Spring Integration存在依赖关系,Spring Cloud与Spring Boot有严格的版本绑定规则,未同步升级会进一步放大冲突
- 方案1:全量适配对应版本(推荐)
按Spring Boot 2.5.4的官方适配要求升级所有关联依赖:- Spring Integration升级到5.5.x系列的最新稳定版
- Spring Cloud版本升级到2020.0.x(Ilford)系列的SR4及以上版本,该版本完全兼容Spring Boot 2.5.x
- 同步升级Swagger/OpenAPI相关的集成组件到适配Spring Boot 2.5.x的版本,避免Swagger和Spring Integration的集成冲突
- 方案2:临时关闭冲突自动配置
如果你项目没有直接使用Spring Integration原生能力,仅通过Spring Cloud Stream间接依赖,可以在配置文件中添加如下配置关闭自动配置规避冲突:
该方案需要自测验证所有消息流转、接口文档功能是否正常,避免影响业务。spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration
- Maven项目执行
mvn clean install -U,Gradle项目执行gradlew clean build --refresh-dependencies,清理本地依赖缓存,强制拉取新版本依赖 - 启动应用确认无Bean创建异常
- 验证核心业务功能(消息生产消费、接口文档访问等)正常运行
内容的提问来源于stack exchange,提问作者Prashant Raghav

