bharathi
bharathi

Reputation: 6271

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type exception

I have two class A and B in which I have autowired C interface. Now the C get implemented in D class. When I try to maven build I am getting below exception.

    java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'asynchronous-kafka-consumer': Unsatisfied dependency expressed through field 'asynchronousMessageReceiver'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mt.mtamp.message.service.AsynchronousMessageReceiver<java.lang.Object>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mt.mtamp.message.service.AsynchronousMessageReceiver<java.lang.Object>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Code:

@Service
class A {

  @Autowired
  C c;

  public void sendMessage() {
  c.sendMessage("Hello");
  } 

}


@Service
class B {

  @Autowired
  C c;

  public void sendMessage() {
  c.sendMessage("Hello");
  } 

}

public interface C {

 public void sendMessage(String message);
}

public class D implements C {

@Override
public void sendMessage(String message) {
 System.out.println("Message-->"+ message); 
}

}

Upvotes: 0

Views: 15198

Answers (1)

Przemek Sulecki
Przemek Sulecki

Reputation: 168

When you are doing:

@Autowired
C c;

You are telling the Spring Boot that should autowire the Bean 'C'. But, 'C' isn't a Bean because you didn't specify any annotation above this interface.

// Missing annotation
public interface C {

    public void sendMessage(String message);
}

Add annotations to both 'C' and 'D':

@Component
public interface C {

    public void sendMessage(String message);
}

@Component
public class D implements C {

    @Override
    public void sendMessage(String message) {
        System.out.println("Message-->" + message);
    }
}

Upvotes: 1

Related Questions