Spring-Integration DSL transform() 方法使用 bean 的名称作为转换器。

huangapple go评论65阅读模式
英文:

Spring-Integration DSL transform() method using name of a bean as the transformer

问题

When creating an IntegrationFlow that uses from(), channel(), or gateway(), I can refer to a channel by simply giving the String name of the channel. However, with a transform() operation using transformers that are beans, I have to include those Autowired beans in the constructor. This can be fastidious when there are a large number of transformations in the flow.

Is there a simple way of referring to a Bean used by a transform() without autowiring the bean in the constructor?

@Component
public class DoubleIntegerValueTransformer implements GenericTransformer<Integer, Integer> {
    @Override
    public Integer transform(Integer source) {
        return source * 2;
    }
}
@Component
public class AutowiredTransformerFlows {

    // Would like to eliminate the constructor and local instance variable
    private final DoubleIntegerValueTransformer doubleIntegerValueTransformer;

    public AutowiredTransformerFlows(DoubleIntegerValueTransformer doubleIntegerValueTransformer) {
        this.doubleIntegerValueTransformer = doubleIntegerValueTransformer;
    }

    @Bean(name = "autowiredTransformerFlow")
    IntegrationFlow usesBeanAsTransformer() {
        return IntegrationFlows.from("autowiredTransformChannel")
                .filter("(payload % 2) == 1", discardReturnsCurrentMessage())
                .transform(doubleIntegerValueTransformer)
                .get();
    }
}
英文:

When creating an IntegrationFlow that uses from() or channel() or gateway(), I can refer to a channel by simply giving the String name of the channel. However, with a transform() operation using transformers that are beans, I have to include those Autowired beans in the constructor. This can be fastidious when then are a large number of transformations in the flow.

Is there a simple way of referring to a Bean used by a transform() without autowiring the bean in the constructor?

@Component
public class DoubleIntegerValueTransformer implements GenericTransformer&lt;Integer, Integer&gt; {
    @Override
    public Integer transform(Integer source) {
        return source * 2;
    }
}
@Component
public class AutowiredTransformerFlows {

    // Would like to eliminate the constructor and local instance variable
    private final DoubleIntegerValueTransformer doubleIntegerValueTransformer;

    public AutowiredTransformerFlows(DoubleIntegerValueTransformer doubleIntegerValueTransformer) {
        this.doubleIntegerValueTransformer = doubleIntegerValueTransformer;
    }

    @Bean(name = &quot;autowiredTransformerFlow&quot;)
    IntegrationFlow usesBeanAsTransformer() {
        return IntegrationFlows.from(&quot;autowiredTransformChannel&quot;)
                .filter(&quot;(payload % 2) == 1&quot;, discardReturnsCurrentMessage())
                .transform(doubleIntegerValueTransformer)
                .get();
    }
}

答案1

得分: 2

只需在bean工厂方法中添加一个参数:

@Bean(name = "autowiredTransformerFlow")
IntegrationFlow usesBeanAsTransformer(DoubleIntegerValueTransformer doubleIntegerValueTransformer) {
    return IntegrationFlows.from("autowiredTransformChannel")
            .filter("(payload % 2) == 1", discardReturnsCurrentMessage())
            .transform(doubleIntegerValueTransformer)
            .get();
}

该bean应该在@Configuration类中,而不是在@Component类中。

英文:

Just add a parameter to the bean factory method:

@Bean(name = &quot;autowiredTransformerFlow&quot;)
IntegrationFlow usesBeanAsTransformer(DoubleIntegerValueTransformer doubleIntegerValueTransformer) {
    return IntegrationFlows.from(&quot;autowiredTransformChannel&quot;)
            .filter(&quot;(payload % 2) == 1&quot;, discardReturnsCurrentMessage())
            .transform(doubleIntegerValueTransformer)
            .get();
}

The bean should be in a @Configuration class, not a @Component class.

huangapple
  • 本文由 发表于 2023年5月7日 18:13:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定