Spring Boot集成SQS启动报错:SimpleMessageListenerContainer实例化失败
PayloadArgumentResolver类找不到问题 问题场景
在Spring Boot 3.3.4项目中配置SQS相关的SqsConfig类,定义了QueueMessagingTemplate和SimpleMessageListenerContainerFactory的Bean,项目构建成功,但启动时出现报错:无法实例化org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer,根源为java.lang.ClassNotFoundException: org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver。
相关代码与配置
SQS配置类
@Configuration @EnableSqs public class SqsConfig { @Bean public QueueMessagingTemplate queueMessagingTemplate(AmazonSQSAsync amazonSqs) { return new QueueMessagingTemplate(amazonSqs); } @Bean public SimpleMessageListenerContainerFactory simpleMessageListenerContainerAddAnaplanListQueue(AmazonSQSAsync amazonSQSAsync) { SimpleMessageListenerContainerFactory simpleMessageListenerContainer = new SimpleMessageListenerContainerFactory(); simpleMessageListenerContainer.setAmazonSqs(amazonSQSAsync); simpleMessageListenerContainer.setAutoStartup(true); simpleMessageListenerContainer.setMaxNumberOfMessages(10); simpleMessageListenerContainer.setWaitTimeOut(1); simpleMessageListenerContainer.setBackOffTime(Long.valueOf(20000)); return simpleMessageListenerContainer; } }
pom.xml依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.gtt</groupId> <artifactId>converter</artifactId> <version>0.0.1-SNAPSHOT</version> <name>converter</name> <description>converter</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-aws-messaging</artifactId> <version>2.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-aws</artifactId> <version>2.2.6.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
报错日志
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'simpleMessageListenerContainer' defined in class path resource [org/springframework/cloud/aws/messaging/config/annotation/SqsConfiguration.class]: Failed to instantiate [org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer]: Factory method 'simpleMessageListenerContainer' threw exception with message: Error creating bean with name 'queueMessageHandler' defined in class path resource [org/springframework/cloud/aws/messaging/config/annotation/SqsConfiguration.class]: org.springframework/messaging/handler/annotation/support/PayloadArgumentResolver at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:648) ~[spring-beans-6.1.13.jar:6.1.13] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50) ~[spring-boot-devtools-3.3.3.jar:3.3.3] Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer]: Factory method 'simpleMessageListenerContainer' threw exception with message: Error creating bean with name 'queueMessageHandler' defined in class path resource [org/springframework/cloud/aws/messaging/config/annotation/SqsConfiguration.class]: org.springframework/messaging/handler/annotation/support/PayloadArgumentResolver Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'queueMessageHandler' defined in class path resource [org/springframework/cloud/aws/messaging/config/annotation/SqsConfiguration.class]: org.springframework/messaging/handler/annotation/support/PayloadArgumentResolver at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:146) ~[spring-beans-6.1.13.jar:6.1.13] ... 25 common frames omitted Caused by: java.lang.NoClassDefFoundError: org/springframework/messaging/handler/annotation/support/PayloadArgumentResolver Caused by: java.lang.ClassNotFoundException: org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver
原因分析
核心问题是版本不兼容:
- 你使用的
spring-cloud-aws-messaging和spring-cloud-starter-aws2.2.6.RELEASE是基于Spring Boot 2.x的版本,依赖Spring Framework 5.x。 - Spring Boot 3.3.4依赖Spring Framework 6.x,而
PayloadArgumentResolver类在Spring Framework 6.x中已被移除(重构为PayloadMethodArgumentResolver),老版本Spring Cloud AWS无法找到该类,导致启动失败。 - 另外,Spring Cloud AWS从3.x版本开始,groupId已从
org.springframework.cloud变更为io.awspring.cloud,并适配了Spring Boot 3.x的API调整。
解决方案
1. 替换Spring Cloud AWS依赖为适配Spring Boot 3.x的版本
移除原有的spring-cloud-aws-messaging和spring-cloud-starter-aws依赖,替换为适配Spring Boot 3.x的SQS专用starter:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 替换为适配Spring Boot 3.x的SQS依赖 --> <dependency> <groupId>io.awspring.cloud</groupId> <artifactId>spring-cloud-starter-aws-sqs</artifactId> <version>3.2.1</version> <!-- 选择与Spring Boot 3.3.x兼容的版本 --> </dependency> </dependencies>
版本参考:Spring Cloud AWS 3.2.x适配Spring Boot 3.3.x,可根据官方版本映射选择最新兼容版本。
2. 调整SQS配置类
Spring Cloud AWS 3.x中,SimpleMessageListenerContainerFactory已被SqsListenerContainerFactory替代,且API使用构建器模式,同时客户端类从AmazonSQSAsync变更为SqsAsyncClient:
@Configuration @EnableSqs public class SqsConfig { @Bean public QueueMessagingTemplate queueMessagingTemplate(SqsAsyncClient sqsAsyncClient) { return new QueueMessagingTemplate(sqsAsyncClient); } @Bean public SqsListenerContainerFactory<?> simpleMessageListenerContainerFactory(SqsAsyncClient sqsAsyncClient) { return SqsListenerContainerFactory.builder() .sqsAsyncClient(sqsAsyncClient) .configure(options -> { options.setMaxNumberOfMessages(10); options.setWaitTime(Duration.ofSeconds(1)); options.setBackOffTime(Duration.ofSeconds(20)); options.setAutoStartup(true); }) .build(); } }
3. 验证启动
执行mvn clean install刷新依赖后启动项目,即可解决类找不到的问题。
内容的提问来源于stack exchange,提问作者suneel meena

