You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Martin Gilday <ma...@imap.cc> on 2009/03/31 11:57:32 UTC

Type converters with Spring

I'd like to have a converter which uses a Spring @service to do lookups.
 According to the docs "If a converter requires configuration you can
plug in an Injector interface to the DefaultTypeConverter which can
construct and inject converter objects via Spring or Guice."

I'm unsure what it means by working with the DefaultTypeConverter. Are
there any examples which show how to do this?

Thanks,
Martin.

Re: Type converters with Spring

Posted by Claus Ibsen <cl...@gmail.com>.
On Tue, Mar 31, 2009 at 12:37 PM, Martin Gilday <ma...@imap.cc> wrote:
> Thanks, we will give those a try.
>
> As your suggestions seem to differ from what I quoted I am still
> interested in what the docs are referring to here
> http://camel.apache.org/type-converter.html  Any ideas what it means by
> plugging in an Injector?  I'll try and update the wiki if this section
> is incorrect.
The type converters methods can be either static or non static. Most
of them are static.

And if I recall Camel uses the Injector to create the non static
classes, that esentially is Spring.
But if it does the full monty applying post construct annotations and
stuff I can be in doubt.

But we could add a test in camel-spring that has a custom type
converter with Spring @Resource annotations
and see if we can get it working out of the box.

Feel free to work with it a bit and get back with findings. Then we
can see what the gap is and maybe add/fix it and
improve the documentation.

All the camel related type converters do not use Spring @annotations
hence we havent been in need of it.



>
>
> ----- Original message -----
> From: "Claus Ibsen" <cl...@gmail.com>
> To: users@camel.apache.org
> Date: Tue, 31 Mar 2009 12:04:27 +0200
> Subject: Re: Type converters with Spring
>
> Hi
>
> Or pass in Exchange as the 2nd parameter.
> Using Exchange you can get hold on the CamelContext and thus the
> registry as well, and do lookup for Spring beans.
>
>
>
> On Tue, Mar 31, 2009 at 12:01 PM, James Strachan
> <ja...@gmail.com> wrote:
>> 2009/3/31 Martin Gilday <ma...@imap.cc>:
>>> I'd like to have a converter which uses a Spring @service to do lookups.
>>>  According to the docs "If a converter requires configuration you can
>>> plug in an Injector interface to the DefaultTypeConverter which can
>>> construct and inject converter objects via Spring or Guice."
>>>
>>> I'm unsure what it means by working with the DefaultTypeConverter. Are
>>> there any examples which show how to do this?
>>
>> You could try something like this
>>
>> public class MyServiceConverter implements ApplicationContextAware {
>>  ApplicationContext context;
>>
>>  @Converter
>>  public MyType convert(String name) {
>>   return context.getbean(name, MyType.class);
>>  }
>> }
>>
>> letting spring do whatever dependency injection you require on the
>> converter object
>>
>> --
>> James
>> -------
>> http://macstrac.blogspot.com/
>>
>> Open Source Integration
>> http://fusesource.com/
>>
>
>
>
> --
> Claus Ibsen
> Apache Camel Committer
>
> Open Source Integration: http://fusesource.com
> Blog: http://davsclaus.blogspot.com/
> Twitter: http://twitter.com/davsclaus
> Apache Camel Reference Card:
> http://refcardz.dzone.com/refcardz/enterprise-integration
>



-- 
Claus Ibsen
Apache Camel Committer

Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus
Apache Camel Reference Card:
http://refcardz.dzone.com/refcardz/enterprise-integration

Re: Type converters with Spring

Posted by James Strachan <ja...@gmail.com>.
2009/4/1 Martin Gilday <ma...@imap.cc>:
> It turns out my colleague didn't have the Spring @RunWith registered
> with his test case.  Once that was in place then the converter worked
> simply by adding @Autowired onto the required service component, and the
> Injector took care of the rest. No need for ApplicationContextAware or
> passing in the Exchange.

Cool. You should be able to use all the Spring/Guice injection stuff
inside your converter (or bean/processor/endpoint etc).

BTW I thought from your initial mail you wanted a converter who's
function was to take a String and turn it into a service - hence the
reason I used the ApplicationContext to do the lookup each time for
each message :)

-- 
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://fusesource.com/

Re: Type converters with Spring

Posted by Martin Gilday <ma...@imap.cc>.
It turns out my colleague didn't have the Spring @RunWith registered
with his test case.  Once that was in place then the converter worked
simply by adding @Autowired onto the required service component, and the
Injector took care of the rest. No need for ApplicationContextAware or
passing in the Exchange.


----- Original message -----
From: "James Strachan" <ja...@gmail.com>
To: users@camel.apache.org
Date: Tue, 31 Mar 2009 11:47:26 +0100
Subject: Re: Type converters with Spring

2009/3/31 Martin Gilday <ma...@imap.cc>:
> Thanks, we will give those a try.
>
> As your suggestions seem to differ from what I quoted I am still
> interested in what the docs are referring to here
> http://camel.apache.org/type-converter.html  Any ideas what it means by
> plugging in an Injector?  I'll try and update the wiki if this section
> is incorrect.

Its maybe a sentence that needs refactoring :)

The concept its trying to convey, I think, is that if your converter
requires some state (e.g. a JAXB provider, a database connection or
whatever) you can use dependency injection on the converter object; as
we use the registry to instantiate it...
http://camel.apache.org/registry.html

plus with Claus's point, the converter method can use the Exchange to
get the Injector/Registry explicitly.

-- 
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://fusesource.com/

Re: Type converters with Spring

Posted by James Strachan <ja...@gmail.com>.
2009/3/31 Martin Gilday <ma...@imap.cc>:
> Thanks, we will give those a try.
>
> As your suggestions seem to differ from what I quoted I am still
> interested in what the docs are referring to here
> http://camel.apache.org/type-converter.html  Any ideas what it means by
> plugging in an Injector?  I'll try and update the wiki if this section
> is incorrect.

Its maybe a sentence that needs refactoring :)

The concept its trying to convey, I think, is that if your converter
requires some state (e.g. a JAXB provider, a database connection or
whatever) you can use dependency injection on the converter object; as
we use the registry to instantiate it...
http://camel.apache.org/registry.html

plus with Claus's point, the converter method can use the Exchange to
get the Injector/Registry explicitly.

-- 
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://fusesource.com/

Re: Type converters with Spring

Posted by Martin Gilday <ma...@imap.cc>.
Thanks, we will give those a try.

As your suggestions seem to differ from what I quoted I am still
interested in what the docs are referring to here
http://camel.apache.org/type-converter.html  Any ideas what it means by
plugging in an Injector?  I'll try and update the wiki if this section
is incorrect.


----- Original message -----
From: "Claus Ibsen" <cl...@gmail.com>
To: users@camel.apache.org
Date: Tue, 31 Mar 2009 12:04:27 +0200
Subject: Re: Type converters with Spring

Hi

Or pass in Exchange as the 2nd parameter.
Using Exchange you can get hold on the CamelContext and thus the
registry as well, and do lookup for Spring beans.



On Tue, Mar 31, 2009 at 12:01 PM, James Strachan
<ja...@gmail.com> wrote:
> 2009/3/31 Martin Gilday <ma...@imap.cc>:
>> I'd like to have a converter which uses a Spring @service to do lookups.
>>  According to the docs "If a converter requires configuration you can
>> plug in an Injector interface to the DefaultTypeConverter which can
>> construct and inject converter objects via Spring or Guice."
>>
>> I'm unsure what it means by working with the DefaultTypeConverter. Are
>> there any examples which show how to do this?
>
> You could try something like this
>
> public class MyServiceConverter implements ApplicationContextAware {
>  ApplicationContext context;
>
>  @Converter
>  public MyType convert(String name) {
>   return context.getbean(name, MyType.class);
>  }
> }
>
> letting spring do whatever dependency injection you require on the
> converter object
>
> --
> James
> -------
> http://macstrac.blogspot.com/
>
> Open Source Integration
> http://fusesource.com/
>



-- 
Claus Ibsen
Apache Camel Committer

Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus
Apache Camel Reference Card:
http://refcardz.dzone.com/refcardz/enterprise-integration

Re: Type converters with Spring

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Or pass in Exchange as the 2nd parameter.
Using Exchange you can get hold on the CamelContext and thus the
registry as well, and do lookup for Spring beans.



On Tue, Mar 31, 2009 at 12:01 PM, James Strachan
<ja...@gmail.com> wrote:
> 2009/3/31 Martin Gilday <ma...@imap.cc>:
>> I'd like to have a converter which uses a Spring @service to do lookups.
>>  According to the docs "If a converter requires configuration you can
>> plug in an Injector interface to the DefaultTypeConverter which can
>> construct and inject converter objects via Spring or Guice."
>>
>> I'm unsure what it means by working with the DefaultTypeConverter. Are
>> there any examples which show how to do this?
>
> You could try something like this
>
> public class MyServiceConverter implements ApplicationContextAware {
>  ApplicationContext context;
>
>  @Converter
>  public MyType convert(String name) {
>   return context.getbean(name, MyType.class);
>  }
> }
>
> letting spring do whatever dependency injection you require on the
> converter object
>
> --
> James
> -------
> http://macstrac.blogspot.com/
>
> Open Source Integration
> http://fusesource.com/
>



-- 
Claus Ibsen
Apache Camel Committer

Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus
Apache Camel Reference Card:
http://refcardz.dzone.com/refcardz/enterprise-integration

Re: Type converters with Spring

Posted by James Strachan <ja...@gmail.com>.
2009/3/31 Martin Gilday <ma...@imap.cc>:
> I'd like to have a converter which uses a Spring @service to do lookups.
>  According to the docs "If a converter requires configuration you can
> plug in an Injector interface to the DefaultTypeConverter which can
> construct and inject converter objects via Spring or Guice."
>
> I'm unsure what it means by working with the DefaultTypeConverter. Are
> there any examples which show how to do this?

You could try something like this

public class MyServiceConverter implements ApplicationContextAware {
 ApplicationContext context;

 @Converter
 public MyType convert(String name) {
   return context.getbean(name, MyType.class);
 }
}

letting spring do whatever dependency injection you require on the
converter object

-- 
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://fusesource.com/