0

I am facing the following issue while trying to run the application

2021-02-17T18:16:38.159+05:30 [APP/PROC/WEB/0] [OUT] Parameter 0 of constructor in com.example.demo.service.ServiceBusProducer required a bean of type 'com.microsoft.azure.servicebus.ITopicClient' that could not be found. 2021-02-17T18:16:38.159+05:30 [APP/PROC/WEB/0] [OUT] The injection point has the following annotations:

2021-02-17T18:16:38.159+05:30 [APP/PROC/WEB/0] [OUT] - @org.springframework.beans.factory.annotation.Autowired(required=true)

2021-02-17T18:16:38.159+05:30 [APP/PROC/WEB/0] [OUT] Action:

2021-02-17T18:16:38.159+05:30 [APP/PROC/WEB/0] [OUT] Consider defining a bean of type 'com.microsoft.azure.servicebus.ITopicClient' in your configuration.

I have the dependency defined as highlighted below

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-servicebus-spring-boot-starter</artifactId>
    <version>2.2.4</version>
</dependency>

and below is the ServiceBusProducer.java class

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

How do I fix this issue? com.example.demo.service.ServiceBusProducer required a bean of type 'com.microsoft.azure.servicebus.ITopicClient' that could not be found.

Updated the code however still facing the same problem

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;
   }
}
One Developer
  • 99
  • 5
  • 43
  • 103

1 Answers1

1

Since you did not provide the public default constructor and you added your own non-default constructor the instantiation failed.

Edit:


Check out similar issues. It seems you have to initialize ITopicClient bean or there has been a naming problem.

Adding naming override might help with this.

Harshita Singh
  • 4,590
  • 1
  • 10
  • 13