我在尝试运行应用程序时遇到以下问题
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,但找不到该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(必需=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条答案
按热度按时间ippsafx71#
由于没有提供公共默认构造函数,并且添加了自己的非默认构造函数,因此示例化失败。
编辑:
看看类似的问题。看来你得初始化了
ITopicClient
或者有一个命名问题。考虑在配置[spring boot]中定义“package”类型的bean
https://github.com/spring-projects/spring-boot/issues/19603
添加命名覆盖可能会对此有所帮助。