Вопрос

I have a TypeConverter class which is set up in a blueprint.xml file with an injected dependency via the constructor. I can verify that the class is created when deploying the OSGI bundle to FuseESB.

I apply the type converter using the convertBodyTo element in a camel context

<convertBodyTo type="com.example.RegisterRequest" />

but this fails with an instantiation exception when constructing the converter

Caused by: java.lang.InstantiationException: com.example.RegisterRequestConverter
    at java.lang.Class.newInstance0(Class.java:359)[:1.7.0_21]
    at java.lang.Class.newInstance(Class.java:327)[:1.7.0_21]
    at org.apache.camel.util.ObjectHelper.newInstance(ObjectHelper.java:1166) 

If I add a noarg constructor then the converter is created - but obviously without it's dependency.

The Camel documentation for type converters http://camel.apache.org/type-converter.html states that

static methods are encouraged to reduce caching, but instance methods are fine, particularly if you want to allow optional dependency injection to customize the converter

Is it possible to define routes via xml and still have type converters dependency injected?

Это было полезно?

Решение

No this is not possible. A type converter should either be

  • static method
  • has a no-arg public constructor

And not use an IoC etc.

If you want IoC etc then you would need to register the instance manually into the Camel TypeConverterRegistry. This can be a bit cumbersome to do.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top