【问题标题】:Spring Boot could not autowire and runSpring Boot 无法自动装配和运行
【发布时间】:2015-01-29 06:05:04
【问题描述】:

我无法运行attached Spring Boot sampler application。它有一个 AMQP 启动器,需要 RabbitMQ。从根本上说,它是一个简单的应用程序,只需将消息发送到绑定了队列的 RabbitMQ Exchange。我收到以下错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.company.messaging.MessageDeliveryManager com.company.exec.Application.messageDeliveryManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.company.messaging.MessageDeliveryManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
 {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Application.java

package com.company.exec;

@SpringBootApplication
public class Application implements CommandLineRunner {

  @Autowired
  MessageDeliveryManager messageDeliveryManager;

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

  public void run(String... args) throws Exception {
    messageDeliveryManager.sendMessage(String message);
  }
}

MessageDeliveryManager.java

package com.company.messaging;

public interface MessageDeliveryManager {
  void sendMessage(String message);
}

MessageDeliveryImpl.java

package com.company.messaging;

public class MessageDeliveryManagerImpl implements MessageDeliveryManager {

  @Value("${app.exchangeName}")
  String exchangeName;

  @Value("${app.queueName}")
  String queueName;

  @Autowired
  RabbitTemplate rabbitTemplate;

  @Bean
  Queue queue() {
    return new Queue(queueName, false);
  }

  @Bean
  DirectExchange exchange() {
    return new DirectExchange(exchangeName);
  }

  @Bean
  Binding binding(Queue queue, DirectExchange exchange) {
    return BindingBuilder.bind(queue).to(exchange).with(queueName);
  }

  public void sendMessage(String message) {
    rabbitTemplate.send(queueName, message);
  }
}

如果有人可以查看并提供关于我做错了什么的建议,我将不胜感激。

【问题讨论】:

  • 这个 MessageDeliveryManager 类有什么?我认为该错误是由其他一些 bean 故障引起的。你能看到其他与 RMQ 相关的错误消息吗?
  • MessageDeliveryManager 是一个接口,由MessageDeliveryManagerImpl 实现。 Impl 类实现了合约定义的方法,注解为@Service
  • 请将接口MessageDeliveryManager以及该接口和类Application的实现添加到您的问题中。
  • MessageDeliveryManagerImpl所在的包是否被Spring Boot扫描过?如果不是,则 bean 在上下文中不可用。
  • @Jens 因为我附上了项目源代码,所以我没有将代码粘贴到问题中。我仍然会添加它。感谢您提出建议。

标签: spring rabbitmq spring-boot autowired


【解决方案1】:

因为你有这样的包树:

com.company.exec
com.company.messaging

只使用默认的@SpringBootApplication,它只是看不到您的MessageDeliveryManager 及其实现。这是因为@ComponentScan@SpringBootApplication 上的元注释)只扫描当前包及其子包。

要使其正常工作,您应该添加以下内容:

@SpringBootApplication
@ComponentScan("com.company")

或者只是将您的 Application 移动到根包 - com.company

【讨论】:

  • 只扫描当前包不准确。所有以com.company.exec 开头的包都会被扫描。但在这种情况下,只有一个。
  • @artem-bilan 成功了!非常感谢您的帮助和详细解释。
  • 使用Boot的RabbitAutoConfiguration时如何覆盖虚拟主机、主机、用户名和密码属性?
  • 请参阅引导文档:docs.spring.io/spring-boot/docs/current/reference/html/…。搜索RABBIT
猜你喜欢
  • 1970-01-01
  • 2019-01-10
  • 2018-12-27
  • 2021-04-02
  • 2016-01-06
  • 2019-06-27
  • 2020-02-09
  • 2017-10-18
  • 2017-03-28
相关资源
最近更新 更多