Inject JAXBContext into spring
I am trying to inject a JAXBContext
into spring application context, by:
<bean开发者_高级运维 id="jaxbContext" class="javax.xml.bind.JAXBContext" factory-method="newInstance">
<constructor-arg type="java.lang.Class" value="com.package.MyClassName"/>
</bean>
It throws an exception:
No matching factory method found: factory method 'newInstance'
And I also try :
<bean id="jaxbContext" class="javax.xml.bind.JAXBContext" factory-method="newInstance">
<constructor-arg type="java.lang.String" value="com.package"/>
</bean>
And It throws an an exception:
javax.xml.bind.JAXBException: "com.package" doesnt contain ObjectFactory.class or jaxb.index I did put a jaxb.index file inside the package "com.package" and has a single line "MyClassName" in the file.
@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>`
精彩评论