Spring Boot could not autowire and run

Web User

I am unable to run the attached Spring Boot sampler application. It has an AMQP starter, requiring RabbitMQ. Fundamentally, it is a simple application that just sends a message to a RabbitMQ Exchange with a queue bound to it. I get the following error:

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);
  }
}

I would really appreciate if someone can review and provide a suggestion on what I am doing wrong.

Artem Bilan

Since you have a package tree like this:

com.company.exec
com.company.messaging

and just use a default @SpringBootApplication, it just doesn't see your MessageDeliveryManager and its implementation. That's because @ComponentScan (the meta-annotation on the @SpringBootApplication) does a scan only for the current package and its subpackages.

To make it worked you should add this:

@SpringBootApplication
@ComponentScan("com.company")

Or just move your Application to the root package - com.company.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Could not autowire SessionRegistry in spring boot

How to Autowire conditionally in spring boot?

Spring Boot can't autowire @ConfigurationProperties

Could not autowire authentication manager in Spring Boot 2.0.0

Could not autowire field in spring. why?

Mybatis ResultHandler with spring boot could not run

Can't Autowire List of objects in Spring boot

Spring boot test unable to autowire service class

Run thread in background in spring boot and being able to autowire

How to Autowire a OkHttpClient bean in Spring Boot?

How to Autowire repository interfaces in Components in Spring Boot

Could not autowire field:RestTemplate in Spring boot application

Spring Boot: autowire beans from library project

Expression based Autowire in Spring Boot (with Kotlin)

How to autowire Hibernate SessionFactory in Spring boot

Can't @Autowire repository interface Spring Boot

How to autowire default XmlMapper in Spring Boot application

How autowire a repository in an integration test with Spring Boot?

Could not autowire hibernate 5 sessionfactory with spring 5

Spring boot 2+ Could not Autowire. There is more than one bean of 'UserDetailsService'

Could not autowire field (Spring mvc - annotations)

Spring Data JPA (CrudRepository) - BeanCreationException: Could not autowire field

JUnit Spring Boot Application - Could not autowire fields

Spring Boot autowire dependency in TestExecutionListeners

How to autowire dependency within a Spring Boot Converter?

Autowire not working in Spring boot Forms

Failed to Autowire beans in a Java thread in Spring Boot

Can't Autowire @Repository interface in Spring Boot

WebSocket in Spring Boot app Could not autowire error