You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用spring-cloud-azure-starter-servicebus-jms连接Azure Service Bus遇JmsTemplate缺失错误

Spring Cloud Azure Service Bus JMS 连接失败问题处理

我按照官方文档尝试使用spring-cloud-azure-starter-servicebus-jms连接Azure Service Bus,但启动时提示找不到JmsTemplate类型的Bean。

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.0.4</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.3-SNAPSHOT</version>
    <name>springcloud-servicebus-1</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.azure.spring</groupId>
            <artifactId>spring-cloud-azure-starter-servicebus-jms</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.azure.spring</groupId>
                <artifactId>spring-cloud-azure-dependencies</artifactId>
                <version>4.6.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Java 代码

package com.example.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;

@SpringBootApplication
@EnableJms
public class Sample implements CommandLineRunner {

    private static final Logger LOGGER = LoggerFactory.getLogger(Sample.class);
    private static final String TOPIC_NAME = "loan-app";
    private static final String SUBSCRIPTION_NAME = "loan-app-sub";

    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args) {
        SpringApplication.run(Sample.class, args);
    }

    @Override
    public void run(String... args) {
        LOGGER.info("Sending message");
        jmsTemplate.convertAndSend(TOPIC_NAME, "Hello Word");
    }

    @JmsListener(destination = TOPIC_NAME, containerFactory = "topicJmsListenerContainerFactory", subscription = SUBSCRIPTION_NAME)
    public void receiveMessage(String message) {
        LOGGER.info("Message received: {}", message);
    }

}

启动错误信息

APPLICATION FAILED TO START

描述:
com.example.demo.Sample中的jmsTemplate字段需要一个类型为'org.springframework.jms.core.JmsTemplate'的bean,但无法找到。
注入点带有以下注解:

  • @org.springframework.beans.factory.annotation.Autowired(required=true)

解决步骤

  1. 添加Azure Service Bus配置信息
    在application.properties或application.yml中配置Service Bus的连接信息:

    spring.cloud.azure.servicebus.connection-string=<你的Service Bus连接字符串>
    spring.cloud.azure.servicebus.jms.enabled=true
    

    或YAML格式:

    spring:
      cloud:
        azure:
          servicebus:
            connection-string: <你的Service Bus连接字符串>
            jms:
              enabled: true
    

    连接字符串可在Azure Portal的Service Bus实例中获取。

  2. 确认依赖版本兼容性
    Spring Boot 3.0.x对应Spring Cloud Azure 4.x版本,当前pom中spring-cloud-azure-dependencies版本4.6.0符合兼容性要求,若问题仍存在可尝试升级至同大版本的最新子版本。

  3. 检查自动配置生效状态
    spring-cloud-azure-starter-servicebus-jms会自动创建JmsTemplate Bean,但缺少正确的连接配置时自动配置会跳过。确保配置信息无误后,Spring会自动完成Bean初始化。

  4. 可选:手动配置JmsTemplate(自动配置失效时)
    若自动配置仍未生效,可手动编写配置类创建Bean:

    @Configuration
    public class JmsConfig {
        @Autowired
        private ConnectionFactory connectionFactory;
    
        @Bean
        public JmsTemplate jmsTemplate() {
            JmsTemplate template = new JmsTemplate();
            template.setConnectionFactory(connectionFactory);
            return template;
        }
    }
    

内容的提问来源于stack exchange,提问作者Martin Joseph

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 04:18:18