gpt4 book ai didi

java - Spring Boot定义多个交换

转载 作者:行者123 更新时间:2023-12-02 10:42:40 24 4
gpt4 key购买 nike

我需要声明多个扇出交换

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

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

....
....
}

一旦我添加 exchange2 的代码我收到错误:


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

最佳答案

异常告诉你解决办法:

  1. bean 上的用户限定符
  2. 使用@Primary定义2个bean中哪一个是主要的

您的代码应该如下所示

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

关于java - Spring Boot定义多个交换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52816716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com