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

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

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?

  1. @Component
  2. public class DoubleIntegerValueTransformer implements GenericTransformer<Integer, Integer> {
  3. @Override
  4. public Integer transform(Integer source) {
  5. return source * 2;
  6. }
  7. }
  1. @Component
  2. public class AutowiredTransformerFlows {
  3. // Would like to eliminate the constructor and local instance variable
  4. private final DoubleIntegerValueTransformer doubleIntegerValueTransformer;
  5. public AutowiredTransformerFlows(DoubleIntegerValueTransformer doubleIntegerValueTransformer) {
  6. this.doubleIntegerValueTransformer = doubleIntegerValueTransformer;
  7. }
  8. @Bean(name = "autowiredTransformerFlow")
  9. IntegrationFlow usesBeanAsTransformer() {
  10. return IntegrationFlows.from("autowiredTransformChannel")
  11. .filter("(payload % 2) == 1", discardReturnsCurrentMessage())
  12. .transform(doubleIntegerValueTransformer)
  13. .get();
  14. }
  15. }
英文:

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?

  1. @Component
  2. public class DoubleIntegerValueTransformer implements GenericTransformer&lt;Integer, Integer&gt; {
  3. @Override
  4. public Integer transform(Integer source) {
  5. return source * 2;
  6. }
  7. }
  1. @Component
  2. public class AutowiredTransformerFlows {
  3. // Would like to eliminate the constructor and local instance variable
  4. private final DoubleIntegerValueTransformer doubleIntegerValueTransformer;
  5. public AutowiredTransformerFlows(DoubleIntegerValueTransformer doubleIntegerValueTransformer) {
  6. this.doubleIntegerValueTransformer = doubleIntegerValueTransformer;
  7. }
  8. @Bean(name = &quot;autowiredTransformerFlow&quot;)
  9. IntegrationFlow usesBeanAsTransformer() {
  10. return IntegrationFlows.from(&quot;autowiredTransformChannel&quot;)
  11. .filter(&quot;(payload % 2) == 1&quot;, discardReturnsCurrentMessage())
  12. .transform(doubleIntegerValueTransformer)
  13. .get();
  14. }
  15. }

答案1

得分: 2

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

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

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

英文:

Just add a parameter to the bean factory method:

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

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:

确定