路过一个Spring bean到骆驼组成部分 [英] Passing a Spring bean to a Camel component
问题描述
我有类型的自定义组件 FooComponent
这是由以下行添加到路由:
I have a custom component of type FooComponent
which is added to the route by the following lines:
from("foo://bar?args=values&etc")
.bean(DownstreamComponent.class)
...
FooComponent
创建一个端点和消费者(类型为 FooConsumer
),其进而发射其得到的<信息code> DownstreamComponent 和管道的其余部分。
FooComponent
creates an endpoint and consumer (of type FooConsumer
) which in turn emits messages which get to the DownstreamComponent
and the rest of the pipeline.
有关的监测,我需要的 FooComponent
消费者打电话到非骆驼对象,我创建作为一个Spring bean上的一个方法。骆驼管道是非常敏感的表现,所以我无法划分 FooComponent
成两半,然后插入监控呼叫作为它们之间的骆驼组成部分(我的preferred解决方案,因为 FooComponent
应该不会真的要了解显示器)。而我不愿意把方法调用成将由监控组件后来在管道上拿起一个骆驼消息,如管道过滤之后变成复杂,我不想用它染指超过必要的。
For monitoring, I need the FooComponent
consumer to call a method on a non-Camel object, which I'm creating as a Spring bean. The Camel pipeline is very performance sensitive so I'm unable to divide the FooComponent
into two halves and insert the monitor call as a Camel component between them (my preferred solution, since FooComponent
shouldn't really have to know about the monitor). And I'm reluctant to turn the method call into a Camel Message that will be picked up by the monitoring component later in the pipeline, as the pipeline filtering becomes complicated later and I don't want to meddle with it more than necessary.
里面的某处 FooConsumer
,我有:
// in the class
@Autowired
Monitor monitor;
// inside the consumer's run method
monitor.noticeSomething();
问题是,监测
将永远不会被设置为这是在应用程序的其他部分产生的监视器豆。据我了解,这是因为 FooConsumer
本身不是春可见 - 这类型的对象是在 FooComponent $ C $正常创建C>。
The problem is that monitor
will never be set to the Monitor bean which is created in the rest of the application. As I understand it, it's because FooConsumer
itself is not visible to Spring -- an object of that type is created normally inside FooComponent
.
所以,我怎样才能得到 FooComponent
找到显示器
实例,它需要使用?
So, how can I get FooComponent
to find the Monitor
instance that it needs to use?
- 我可以通过它创建路由时吗?这似乎棘手,因为定义是一个虚假的URL
富://酒吧ARGS =值&放大器;等等
;我还没有找到如何通过Java对象的方式。 - 我可以让Spring找到
@Autowired
在FooConsumer
标注并以某种方式注入监视器对象?
- Can I pass it in when the route is created? This seems tricky because the definition is a faux URL
"foo://bar?args=values&etc"
; I haven't found how to pass Java objects that way. - Can I get Spring to find that
@Autowired
annotation insideFooConsumer
and inject the monitor object somehow?
推荐答案
如果您有监视器的单一实例你应该能够@Autowire它在FooComponent类。由于骆驼会让创建FooComponent春天依赖注入。
If you have a singleton instance of Monitor you ought to be able to @Autowire it in the FooComponent class. As Camel will let Spring dependency inject when creating the FooComponent.
然后,你可以当你从你的组件创建端点/消费者传递显示器实例。
Then you can pass on the monitor instance when you create the endpoint / consumer from your component.
这篇关于路过一个Spring bean到骆驼组成部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!