You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Wuyts Diether <di...@optis.be> on 2013/02/01 16:40:56 UTC

Spring bean injection best practice

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



Re: Spring bean injection best practice

Posted by Christian Müller <ch...@gmail.com>.
And I prefer
to("bean://my bean") because it's more inline with the other endpoints and
easier to intercept if necessary.

Just my 0.02 €,
Christian

Sent from a mobile device
Am 01.02.2013 16:41 schrieb "Wuyts Diether" <di...@optis.be>:

> 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
>
>
>

Re: Spring bean injection best practice

Posted by Diether <di...@optis.be>.
Taariq Levack wrote
> 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.


Christian Mueller wrote
> And I prefer
> to("bean://my bean") because it's more inline with the other endpoints and
> easier to intercept if necessary.

Personally I don't like to put these things in "String's". 
If I refactor eg a classname I want Eclipse to do it's work and change it in
all places. I'm just lazy and don't want to do it manually :-)

Good point about the endpoints-intercepting though. But I don't have a need
to intercept at the moment.

But perhaps I should stick with  my option #1, less boilerplate and it works
as expected.



--
View this message in context: http://camel.465427.n5.nabble.com/Spring-bean-injection-best-practice-tp5726771p5726872.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Spring bean injection best practice

Posted by Taariq Levack <ta...@gmail.com>.
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 <di...@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
>
>
>