在尝试运行应用程序时,我面临以下问题
2021-02-17T18:16:38.159+05:30 com.example.demo.service.ServiceBusProducer中构造函数的APP/PROC/WEB/0参数0需要一个无法找到的'com.microsoft.azure.servicebus.ITopicClient‘类型的bean。2021-02-17T18:16:38.159+05:30 APP/PROC/WEB/0注入点有以下注释:
2021-02-17T18:16:38.159+05:30 APP/PROC/WEB/0 - @org.springframework.beans.factory.annotation.Autowired(required=true)
2021-02-17T18:16:38.159+05:30 APP/PROC/WEB/0行动:
2021-02-17T18:16:38.159+05:30 APP/PROC/WEB/0考虑在配置中定义'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;
}
}发布于 2021-02-17 14:21:12
由于您没有提供公共默认构造函数,并且添加了您自己的非默认构造函数,实例化失败。
编辑:
看看类似的问题。您似乎必须初始化ITopicClient bean,否则就会出现命名问题。
添加命名覆盖可能会对此有所帮助。
https://stackoverflow.com/questions/66242758
复制相似问题