Azure ServiceBus:找不到“com.microsoft.azure.servicebus.ITopicClient”

Posted

技术标签:

【中文标题】Azure ServiceBus:找不到“com.microsoft.azure.servicebus.ITopicClient”【英文标题】:Azure ServiceBus: 'com.microsoft.azure.servicebus.ITopicClient' that could not be found 【发布时间】:2021-05-20 09:21:14 【问题描述】:

我在尝试运行应用程序时遇到以下问题

2021-02-17T18:16:38.159+05:30 [APP/PROC/WEB/0] [OUT] 参数 0 的 com.example.demo.service.ServiceBusProducer 中的构造函数需要一个 'com.microsoft.azure.servicebus.ITopicClient' 类型的 bean 可以 找不到。 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 配置。

我定义了如下突出显示的依赖项

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

下面是ServiceBusProducer.java类

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;
    

我该如何解决这个问题? com.example.demo.service.ServiceBusProducer 需要一个“com.microsoft.azure.servicebus.ITopicClient”类型的 bean,但无法找到。

更新了代码,但仍然面临同样的问题

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;
   

【问题讨论】:

【参考方案1】:

由于您没有提供公共默认构造函数并且您添加了自己的非默认构造函数,因此实例化失败。

编辑:


查看类似问题。看来您必须初始化 ITopicClient bean 或出现命名问题。

Consider defining a bean of type 'package' in your configuration [Spring-Boot] https://github.com/spring-projects/spring-boot/issues/19603

添加命名覆盖可能对此有所帮助。

【讨论】:

你能更新代码并帮助我完成课程吗? 这个答案有帮助吗? 更新了代码但是还是遇到同样的问题,能否请您参考一下问题,我已经发布了最近更新的代码 即使我尝试将构造函数设为公开,结果也是一样的。 请添加一个实例而不是参数:new TopicClient(.......);.

以上是关于Azure ServiceBus:找不到“com.microsoft.azure.servicebus.ITopicClient”的主要内容,如果未能解决你的问题,请参考以下文章

Azure ServiceBus的消息中带有@strin3http//schemas.microsoft.com/2003/10/Serialization/?

Azure Messaging-ServiceBus Messaging消息队列技术系列8-服务总线配额

使用订阅的 Azure 服务总线队列

Azure Messaging-ServiceBus Messaging消息队列技术系列3-消息顺序保证

Azure 函数无法将参数“messageReceiver”绑定到类型“Microsoft.Azure.ServiceBus.Core.MessageReceiver”

Azure 数据工厂中的 *.servicebus.windows.net 用于啥?