英文:
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<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();
}
}
答案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 = "autowiredTransformerFlow")
IntegrationFlow usesBeanAsTransformer(DoubleIntegerValueTransformer doubleIntegerValueTransformer) {
return IntegrationFlows.from("autowiredTransformChannel")
.filter("(payload % 2) == 1", discardReturnsCurrentMessage())
.transform(doubleIntegerValueTransformer)
.get();
}
The bean should be in a @Configuration
class, not a @Component
class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论