0

I need to declare multiple Fanout exchanges

@SpringBootApplication
public class Application {
  @Bean
  FanoutExchange exchange1() {
    return new FanoutExchange(exchangeName1, true, false);
  }

  @Bean
  FanoutExchange exchange2() {
    return new FanoutExchange(exchangeName2, true, false);
  }

  ....
  ....
}

As soon as I add the code for the exchange2 I get the error:


APPLICATION FAILED TO START


Description:

Parameter 1 of method binding in com.Application required a single bean, but 2 were found: - exchange1: defined by method 'exchange1' in com.Application - exchange2: defined by method 'exchange2' in com.Application

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

Fabry
  • 1,498
  • 4
  • 23
  • 47

1 Answers1

1

The exception is telling you the solution:

  1. user Qualifier on your bean
  2. define which one from the 2 beans is the primary one using @Primary

Your code should look like

@SpringBootApplication
public class Application {

    @Bean
    @Qualifier("exchange1")
    @Primary
    FanoutExchange exchange1() {
       return new FanoutExchange(exchangeName1, true, false);
    }

    @Bean
    @Qualifier("exchange2")
    FanoutExchange exchange2() {
       return new FanoutExchange(exchangeName2, true, false);
    }
 }
Adina Rolea
  • 2,031
  • 1
  • 7
  • 16