Java 具有Spring集成的动态配置
从RabbitMQ连接规范列表到发布-订阅通道上的一系列使用者的正确方式是什么 也就是说,假设我有如下兔子配置:Java 具有Spring集成的动态配置,java,spring,rabbitmq,spring-integration,spring-java-config,Java,Spring,Rabbitmq,Spring Integration,Spring Java Config,从RabbitMQ连接规范列表到发布-订阅通道上的一系列使用者的正确方式是什么 也就是说,假设我有如下兔子配置: rabbits: - hostname: blahblah vhost: blahlbah username: blahlbah password: blahlbalh exchange: blahblah - hostname: blahblah1 vhost: blahlbah1 userna
rabbits:
- hostname: blahblah
vhost: blahlbah
username: blahlbah
password: blahlbalh
exchange: blahblah
- hostname: blahblah1
vhost: blahlbah1
username: blahlbah1
password: blahlbalh1
exchange: blahblah1
...
我将它们封送到@NestedConfigurationProperty列表中。我可以编写一个@Bean
方法,从其中一个列表中获取amqpmetplate
,如下所示:
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public AmqpTemplate amqpTemplate(RabbitConfiguration rabbitConfiguration) {
...
}
然后,我可以将其映射到一个集成流
:
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public IntegrationFlow integrationFlow(AmqpTemplate amqpTemplate) {
return IntegrationFlows.from(inboundPubSubChannel()).handle(Amqp.outboundAdapter(amqpTemplate)).get();
}
但是,对列表
执行此操作的正确方法是什么,并由Spring处理生成的列表
?当然不仅仅是:
@Bean
public List<IntegrationFlow> integrationFlows(List<RabbitConfiguration> rabbitConfigurations) {
return rabbitConfigurations.stream()
.map(this::amqpTemplate)
.map(this::integrationFlow)
.collect(toList())
}
@Bean
公共列表集成流(列表位配置){
返回rabbitConfigurations.stream()
.map(此::amqpmodel)
.map(此::integrationFlow)
.collect(toList())
}
我认为您试图做的是根据您的配置属性动态创建IntegrationFlow类型的Springbean。根据你想要的“魔法”或“透明”程度,你有很多选择。如果您想要完全变魔术,您必须实现BeanFactoryPostProcessor并在Spring上下文中注册。
像这样的方法应该会奏效:
public class IntegrationFlowPostProcessor implements BeanFactoryPostProcessor{
List<RabbitConfiguration> rabbitConfigurations;
public IntegrationFlowPostProcessor(List<RabbitConfiguration> rabbitConfigurations) {
this.rabbitConfigurations = rabbitConfigurations;
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
rabbitConfigurations.stream()
.forEach(rabbitConfig -> {
IntegrationFlow intFlow = integrationFlow(amqpTemplate(rabbitConfig));
beanFactory.registerSingleton(rabbitConfig.getHostanme(), intFlow);
});
}
private AmqpTemplate amqpTemplate(RabbitConfiguration rabbitConfiguration) {
//Implement here
return null;
}
private IntegrationFlow integrationFlow(AmqpTemplate amqpTemplate) {
//Implement here
return null;
}
}
公共类集成FlowPostProcessor实现BeanFactoryPostProcessor{
列出rabbitConfigurations;
公共集成流后处理器(列表位配置){
this.rabbitConfigurations=rabbitConfigurations;
}
@凌驾
public void后处理beanFactory(ConfigurableListableBeanFactory beanFactory)引发BeanException{
rabbitConfigurations.stream()
.forEach(rabbitConfig->{
IntegrationFlow intFlow=IntegrationFlow(amqpmetplate(rabbitConfig));
registerSingleton(rabbitConfig.getHostanme(),intFlow);
});
}
专用AmqpTemplate AmqpTemplate(RabbitConfiguration RabbitConfiguration){
//在这里实施
返回null;
}
专用集成流集成流(AmqpTemplate AmqpTemplate){
//在这里实施
返回null;
}
}
然后,您必须在配置类中注册后处理器:
@Bean public IntegrationFlowPostProcessor ifpp(List<RabbitConfiguration> config {
return new IntegrationFlowPostProcessor(config);
}
@Bean公共集成流后处理器ifpp(列表配置{
返回新的IntegrationFlowPostProcessor(配置);
}
然后,您就可以使用@Qualifier
,或者作为一个集合(比如List
),通过主机名将每个集成流注入到其他bean中,所有集成流都存在于Spring上下文中