JMSTemplate Bean找不到:Spring Boot自动注入失败求助
问题:Spring Boot整合Azure ServiceBus JMS时找不到JmsTemplate Bean
昨天运行正常的Spring Boot整合Azure ServiceBus的JMS消息收发程序突然报错,未做任何代码改动,完全按照微软官方教程实现,报错信息如下:
错误信息
Field jmsTemplate in com.example.messagesender.MessageSenderApplication required a bean of type 'org.springframework.jms.core.JmsTemplate' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)
建议提示:
Consider defining a bean of type 'org.springframework.jms.core.JmsTemplate' in your configuration.
相关配置与代码
Pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-starter-servicebus-jms</artifactId> <version>6.0.0-beta.4</version> </dependency> <dependency> <groupId>com.azure</groupId> <artifactId>azure-messaging-servicebus</artifactId> <version>7.14.1</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jms --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.jms/javax.jms-api --> <dependency> <groupId>javax.jms</groupId> <artifactId>javax.jms-api</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>org.messaginghub</groupId> <artifactId>pooled-jms</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-dependencies</artifactId> <version>5.3.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
Java代码
@SpringBootApplication @EnableJms public class MessageSenderApplication implements CommandLineRunner { private static final Logger LOGGER = LoggerFactory.getLogger(MessageSenderApplication.class); private static final String QUEUE_NAME = "queue"; @Autowired private JmsTemplate jmsTemplate; public static void main(String[] args) { SpringApplication.run(MessageSenderApplication.class, args); } @Override public void run (String... args){ LOGGER.info("Sending message"); jmsTemplate.convertAndSend(QUEUE_NAME, "Hello World"); } @JmsListener(destination = QUEUE_NAME, containerFactory = "jmsListenerContainerFactory") public void receiveMessage (String message){ LOGGER.info("Message received: {}", message); } }
解决方案
问题根源在于依赖版本冲突和多余依赖导致Spring Boot自动配置失效,无法创建JmsTemplate Bean,具体修复步骤如下:
对齐Azure依赖版本
spring-cloud-azure-starter-servicebus-jms使用了6.0.0-beta.4,但dependencyManagement中的spring-cloud-azure-dependencies是5.3.0,版本不匹配导致自动配置逻辑无法正常执行。将starter版本改为和dependencyManagement一致的5.3.0。移除冗余依赖
spring-jms、javax.jms-api已经被spring-cloud-azure-starter-servicebus-jms包含,手动指定旧版本会引发依赖冲突,直接删除这两个依赖。azure-messaging-servicebus也已被starter包含,可选择移除该依赖,让starter管理对应版本。
修改后的Pom.xml示例
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-starter-servicebus-jms</artifactId> <version>5.3.0</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.messaginghub</groupId> <artifactId>pooled-jms</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-dependencies</artifactId> <version>5.3.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
完成以上修改后,重新构建项目并运行,Spring Boot的自动配置会正确创建JmsTemplate和jmsListenerContainerFactory Bean,程序即可恢复正常。
内容的提问来源于stack exchange,提问作者Theo
相关产品推荐
相关产品推荐

