如何使用 IntegrationFlowContext 作为流程配置的一部分?

问题描述 投票:1回答:1

作为我的spring-boot流程配置的一部分,我创建了多个非常相似的集成流。我目前的配置代码看起来像下面。

@Configuration
public class MyConfig {

    @Component
    public static class FlowFactory {
        public IntegrationFLow createFlow(String someValue) {
            return IntegrationFLows.from
            ...
            .get();
        }
    }

    @Bean
    public IntegrationFlow flow1(FlowFactory flowFactory) {
        return flowFactory.createFlow("1");
    } 
    @Bean
    public IntegrationFlow flow2(FlowFactory flowFactory) {
        return flowFactory.createFlow("2");
    } 
    @Bean
    public IntegrationFlow flow3(FlowFactory flowFactory) {
        return flowFactory.createFlow("3");
    } 

}

我怎样才能用一个利用IntegrationFlowContext的注册循环来替换(在我的配置类中)那些硬编码的Bean?也许像下面这样?

@Configuration
public class MyConfig {

    @Component
    public static class FlowFactory {

        FlowFactory(IntegrationFlowContext flowContext) {
            for (String someValue : ImmutableList.of("1","2","3") {
                flowContext.registration(createFLow(someValue)).register();    
            }
        }

        private IntegrationFLow createFlow(String someValue) {
            return IntegrationFLows.from
            ...
            .get();
        }
    }

}

非常感谢您的时间和您的专业知识。最诚挚的问候

spring-integration
1个回答
1
投票

是的,你可以使用一个 IntegrationFlowContext 这样,但最好是这样做。register()@PostConstructor注意:有很多因素可能会影响到我们如何注册豆子,所以最好是尽可能地推迟流程注册。

因此,在我看来,情况是这样的。

@Component
public static class FlowFactory {
    private final IntegrationFlowContext flowContext;

    FlowFactory(IntegrationFlowContext flowContext) {
        this.flowContext = flowContext;
    }

    @PostConstruct
    public void init() {
        for (String someValue : ImmutableList.of("1","2","3") {
            this.flowContext.registration(createFLow(someValue)).register();    
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.