I don't know about best practices, often depends on use cases, but Camel
will also cache the result of the registry lookup, if the bean is a
singleton.[1]
For instance you may not want to autowire all these services into your
router, or even know which ones to autowire, then Camel can look it up in
the registry and cache the bean for the next invocation.

Nothing wrong with your option 2, slight variation is beanRef("myService"),
a bit less boilerplate.

[1] http://camel.apache.org/bean.html


On Fri, Feb 1, 2013 at 5:40 PM, Wuyts Diether <diether.wu...@optis.be>wrote:

> There are a few possibilities when using spring beans in your camel
> routes. But I was wondering what the common best practice is.
>
> 1) Use the class in the .bean method:
> from("direct://start")
> .bean(MyService.class, "process")
> .end();
>
> Normally Camel will then create a new instance of MyService class.
> But from what I've tested this won't happen if you do annotate the service
> with @Service (or @Component).
> So no new Myservice instance is created and the Spring managed bean is
> used.
>
>
> 2) The other possibility is to use autowiring
>
> @Autowired
> Private MyService myService;
> ...
> from("direct://start")
> .bean(myservice, "process")
> .end();
>
>
> At the moment I'm using #2.
> Because it's easier to see that it is in fact a spring managed bean. And
> not a newly created object.
>
> Any thoughts?
>
> Regards,
> Diether
>
>
>

Reply via email to