Spring Cloud 2023.0.0环境下Spring Cloud Gateway启动失败求助
错误信息
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'routerFunctionMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Error creating bean with name 'gatewayCompositeRouterFunction': Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'gatewayRouterFunctionHolder' defined in BeanDefinition defined in null: Could not generate CGLIB subclass of class org.springframework.cloud.gateway.server.mvc.config.GatewayMvcPropertiesBeanDefinitionRegistrar$RouterFunctionHolder: Common causes of this problem include using a final class or a non-visible class
环境信息
- Spring Boot 3.2.0
- Spring Cloud 2023.0.0
- Java 17
- 配置文件(application.properties):
spring.application.name=gateway-server server.port=9090 # Eureka config eureka.client.service-url.defaultZone=${EUREKA_URI:http://localhost:8761/eureka} spring.cloud.gateway.discovery.locator.lower-case-service-id=true spring.cloud.gateway.discovery.locator.enabled=true
解决方案
1. 确认依赖类型,使用WebFlux版本的Gateway
确保项目引入的是spring-cloud-gateway依赖(基于WebFlux),而非spring-cloud-gateway-server-mvc(MVC版本)。MVC版本的Gateway在Spring Boot 3.2.x下存在兼容性问题,会触发上述CGLIB子类生成失败的错误。
Maven依赖示例:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
2. 排除WebMvc自动配置
Spring Cloud Gateway基于WebFlux运行,无需WebMvc的自动配置,可在启动类上排除相关配置:
@SpringBootApplication(exclude = WebMvcAutoConfiguration.class) public class GatewayServerApplication { public static void main(String[] args) { SpringApplication.run(GatewayServerApplication.class, args); } }
3. 验证依赖版本一致性
Spring Cloud 2023.0.0(Leyton)官方兼容Spring Boot 3.2.x,若仍存在冲突,可通过mvn dependency:tree(Maven)或./gradlew dependencies(Gradle)命令排查依赖树,调整冲突依赖的版本。
内容的提问来源于stack exchange,提问作者Tushar Girase

