我是靠谱客的博主 称心枫叶,这篇文章主要介绍java 动态添加bean_如何在Spring(Boot)应用程序的代码中动态添加bean?,现在分享给大家,希望可以做个参考。

我有一个使用spring-rabbit的Spring(启动)应用程序,我根据需要创建了绑定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);

}

}

我遇到的问题是每3个bean只有两条信息,即队列名称和交换名称 .

有没有办法在上下文中添加任意数量的bean而不是复制和粘贴一堆 @Bean 方法?我想要像"for each name in this list, add these three beans with this connection."这样的东西

最后

以上就是称心枫叶最近收集整理的关于java 动态添加bean_如何在Spring(Boot)应用程序的代码中动态添加bean?的全部内容,更多相关java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(111)

评论列表共有 0 条评论

立即
投稿
返回
顶部