Inject JAXBContext into spring Inject JAXBContext into spring spring spring

Inject JAXBContext into spring


@Tomasz's answer is the solution I'd recommend, but if you want to stick with JAXBContext, then the reason your first example failed is that the static getInstance() method on JAXBContext doesn't take a single Class argument, it takes a vararg list of them. So you need to inject a list, not a single class:

<bean id="jaxbContext" class="javax.xml.bind.JAXBContext" factory-method="newInstance">  <constructor-arg value-type="java.lang.Class">    <list>       <value>com.package.MyClassName</value>    </list>  </constructor-arg></bean>


Have you tried Spring OXM? The last line is important, namespaces are for reference only:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:oxm="http://www.springframework.org/schema/oxm"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-1.5.xsd">    <oxm:jaxb2-marshaller id="marshaller" contextPath="com.package"/></beans>

See 8.4. XML Schema-based Configuration. Yu'll also need spring-oxm on your classpath.


this will resolve the problem for jaxb.index file or ObjectFactory problem in spring env. provide the value of the package where the classes are their which generate the xml

enter code here`

    <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">        <property name="packagesToScan" >            <value>com.adarsh.spring.integration.entities</value>        </property>   </bean>`