You need to enable JavaScript to run this app. Spring Boot集成Azure Service Bus时找不到ITopicClient Bean的问题排查
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot集成Azure Service Bus时找不到ITopicClient Bean的问题排查

Spring Boot 集成 Azure Service Bus 启动异常说明

异常现象

应用启动阶段抛出Bean注入失败错误,完整日志如下:

2021-02-17T18:16:38.159+05:30 [APP/PROC/WEB/0] [OUT] com.example.demo.service.ServiceBusProducer 构造方法的第0个入参需要类型为 com.microsoft.azure.servicebus.ITopicClient 的Bean,当前Spring容器中无法找到符合要求的实例
2021-02-17T18:16:38.159+05:30 [APP/PROC/WEB/0] [OUT] 注入点的标注信息:
2021-02-17T18:16:38.159+05:30 [APP/PROC/WEB/0] [OUT] - @org.springframework.beans.factory.annotation.Autowired(required=true)
2021-02-17T18:16:38.159+05:30 [APP/PROC/WEB/0] [OUT] 异常给出的操作指引:
2021-02-17T18:16:38.159+05:30 [APP/PROC/WEB/0] [OUT] 请在项目配置中定义类型为 'com.microsoft.azure.servicebus.ITopicClient' 的Bean。

项目现有配置

已引入Azure Service Bus对应的Spring Boot Starter依赖,Maven配置如下:

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-servicebus-spring-boot-starter</artifactId>
    <version>2.2.4</version>
</dependency>

初始业务代码

消息生产者类的初始实现如下:

package com.example.demo.service;

import com.microsoft.azure.servicebus.ITopicClient;
import com.microsoft.azure.servicebus.Message;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.time.Instant;

@Log4j2
@Component
@Service
public class ServiceBusProducer implements Ordered {
    @Autowired
    private ITopicClient iTopicClient;

    ServiceBusProducer(ITopicClient iTopicClient) {
        this.iTopicClient = iTopicClient;
    }

    @EventListener(ApplicationReadyEvent.class)
    public void produce() throws Exception {
        this.iTopicClient.send(new Message("Hello @ " + Instant.now().toString()));
    }

    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}

修复尝试

针对报错提示,调整代码新增@PostConstruct方法对客户端属性重新赋值,修改后的代码如下:

package com.example.demo.service;

import com.microsoft.azure.servicebus.ITopicClient;
import com.microsoft.azure.servicebus.Message;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.PostConstruct;
import java.time.Instant;

@Log4j2
@Component
@Service
public class ServiceBusProducer implements Ordered {
    @Autowired
    private ITopicClient iTopicClient;

    ServiceBusProducer(ITopicClient iTopicClient) {
        this.iTopicClient = iTopicClient;
    }

    @PostConstruct
    private void postConstruct() {
        this.iTopicClient = iTopicClient;
    }

    @EventListener(ApplicationReadyEvent.class)
    public void produce() throws Exception {
        this.iTopicClient.send(new Message("Hello @ " + Instant.now().toString()));
    }

    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}

调整后启动应用,仍抛出相同的Bean无法找到的异常,问题未解决。

根因与修复方法

  • 根本原因:azure-servicebus-spring-boot-starter 2.2.4版本仅完成Azure Service Bus连接配置的加载与自动装配,不会自动创建ITopicClient实例并注册到Spring容器,因此直接按类型注入会触发Bean不存在的报错。
  • 可落地的修复步骤:
    1. 在配置类中手动定义ITopicClient类型的Bean,从配置文件读取Service Bus连接字符串、目标Topic名称完成客户端实例的初始化
    2. 移除业务类上同时标注的@Component、@Service重复注解
    3. 删除无实际逻辑的@PostConstruct方法,该方法仅对成员变量做自我赋值,无实际作用

内容的提问来源于stack exchange,提问作者One Developer

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.03 20:06:02