org.springframework.beans.factory.BeanInitializationException: Provider com.sun.xml.bind.v2.ContextFactory could not be instantiated

Solution Unverified - Updated -

Environment

  • Fuse Message Router
  • JAXB

Issue

How to configure a jaxb marshaller in camel route?

Resolution

To resolve the error, pass in the classloader when instantiating the JAXBContext,

For Example :

context=JAXBContext.newInstance(
     "com.fusesource.myContextPath", 
     MyTransformer.class.getClassLoader());  

Root Cause

Error was caused by a camel route which un-marshaled a message using Jaxb, for example:

public class MyRoute extends SpringRouteBuilder {
private DataFormat dataFormat;
public void configure() throws Exception {
  from(direct:a)
 .unmarshal(dataFormat)
 .process(new Processor() {
  public void process(Exchange exchange) throws Exception {
  // ..... Process the exchange
  }
  })
  .marshal(dataFormat)
  .to(seda:b);
}
}

The dataFormat was instantiated as follows and subsequently injected into the Camel route:

 <bean id="dataFormat" class="org.apache.camel.converter.jaxb.JaxbDataFormat">
  <constructor-arg ref="jaxbContext" />
 </bean>
 <bean id="jaxbContext" class="com.progress.MyTransformer" factory-method="getJaxbContext" />

Inside the getJaxbContext() method the jaxbContext bean was instantiated as follows:

public static JAXBContext getJaxbContext() {

 if(context == null) {
    try {
     // Original code. No classloader specified
     //context=JAXBContext.newInstance("com.fusesource.myContextPath");

     // Updated code. We pass the transformers classloader
  context=JAXBContext.newInstance("com.fusesource.myContextPath", MyTransformer.class.getClassLoader());
    } catch (Exception e) {
   // Process exception ..
       } 
 }
} 

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.