如何在Spring(Boot)应用程序的代码中动态添加bean?

如何在Spring(Boot)应用程序的代码中动态添加bean?,spring,rabbitmq,Spring,Rabbitmq,我有一个使用SpringRabbit的Spring(boot)应用程序,我根据需要创建绑定bean,如下所示: import org.springframework.amqp.core.*; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotati

我有一个使用SpringRabbit的Spring(boot)应用程序,我根据需要创建绑定bean,如下所示:

import org.springframework.amqp.core.*; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class QueueBindings { // first binding @Bean public Queue firstQueue(@Value("${rabbitmq.first.queue}") String queueName) { return new Queue(queueName); } @Bean public FanoutExchange firstExchange(@Value("${rabbitmq.first.exchange}") String exchangeName) { return new FanoutExchange(exchangeName); } @Bean public Binding firstBinding(Queue firstQueue, FanoutExchange firstExchange) { return BindingBuilder.bind(firstQueue).to(firstExchange); } // second binding @Bean public Queue secondQueue(@Value("${rabbitmq.second.queue}") String queueName) { return new Queue(queueName); } @Bean public FanoutExchange secondExchange(@Value("${rabbitmq.second.exchange}") String exchangeName) { return new FanoutExchange(exchangeName); } @Bean public Binding secondBinding(Queue secondQueue, FanoutExchange secondExchange) { return BindingBuilder.bind(secondQueue).to(secondExchange); } } 导入org.springframework.amqp.core.*; 导入org.springframework.beans.factory.annotation.Value; 导入org.springframework.context.annotation.Bean; 导入org.springframework.context.annotation.Configuration; @配置 公共类队列绑定{ //首次装订 @豆子 公共队列firstQueue(@Value(“${rabbitmq.first.Queue}”)字符串queueName){ 返回新队列(queueName); } @豆子 public FanoutExchange firstExchange(@Value(${rabbitmq.first.exchange})字符串exchangeName){ 返回新的FanouteExchange(exchangeName); } @豆子 公共绑定firstBinding(队列firstQueue,FanoutExchange firstExchange){ 将BindingBuilder.bind(firstQueue.to)返回到(firstExchange); } //二次装订 @豆子 公共队列secondQueue(@Value(“${rabbitmq.second.Queue}”)字符串queueName){ 返回新队列(queueName); } @豆子 public FanoutExchange secondExchange(@Value(${rabbitmq.second.exchange})字符串exchangeName){ 返回新的FanouteExchange(exchangeName); } @豆子 公共绑定secondBinding(队列secondQueue,FANOTEXchange secondExchange){ 将BindingBuilder.bind(secondQueue.to)返回到(secondExchange); } } 我遇到的问题是,每3个bean只有两条信息,队列名和交换名


有没有一种方法可以将任意数量的Bean添加到上下文中,而不是复制和粘贴一堆
@Bean
方法?我想要类似“对于此列表中的每个名称,使用此连接添加这三个bean。”

要以编程方式注册任意数量的bean,您需要下拉到较低级别的API。您可以在配置类上使用
@Import
来引用
importBeanDefinitionRegistrator
实现。在注册器的
registerBeanDefinitions
方法中,您将为所有bean注册bean定义


如果您希望能够从外部配置要注册的bean,则可以使用
importBeanDefinitionRegistrator
环境感知。这允许您注入
环境
,以便您可以使用其属性自定义注册器将注册的bean。

要以编程方式注册任意数量的bean,您需要下拉到较低级别的API。您可以在配置类上使用
@Import
来引用
importBeanDefinitionRegistrator
实现。在注册器的
registerBeanDefinitions
方法中,您将为所有bean注册bean定义


如果您希望能够从外部配置要注册的bean,则可以使用
importBeanDefinitionRegistrator
环境感知。这允许您注入
环境
,以便您可以使用它的属性来定制注册器将注册的bean。

我终于开始尝试这种方法了。我遇到的问题是,
Environment
不允许您像对待地图一样对待属性;您可以要求一个已知属性,但不能要求“此属性下的所有值将其视为虚线路径”。SpringBoot能够映射到地图,但我不知道如何从注册器中访问其他bean。我终于开始尝试这种方法。我遇到的问题是,
Environment
不允许您像对待地图一样对待属性;您可以要求一个已知属性,但不能要求“此属性下的所有值将其视为虚线路径”。SpringBoot能够映射到映射,但我不知道如何从注册器中访问其他bean。