将 JAXBContext 注入 spring
我试图将 JAXBContext
注入到 Spring 应用程序上下文中,方法是:
<bean id="jaxbContext" class="javax.xml.bind.JAXBContext" factory-method="newInstance">
<constructor-arg type="java.lang.Class" value="com.package.MyClassName"/>
</bean>
它抛出异常:
找不到匹配的工厂方法:工厂方法“newInstance”
我也尝试:
<bean id="jaxbContext" class="javax.xml.bind.JAXBContext" factory-method="newInstance">
<constructor-arg type="java.lang.String" value="com.package"/>
</bean>
并且它抛出异常:
javax.xml.bind.JAXBException:“com.package”不包含ObjectFactory.class或jaxb.index 我确实将 jaxb.index 文件放入包“com.package”中,并在文件中包含一行“MyClassName”。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@Tomasz 的答案是我推荐的解决方案,但如果您想坚持使用 JAXBContext,那么您的第一个示例失败的原因是
JAXBContext
上的static getInstance()
方法不采用单个Class
参数,它需要它们的可变参数列表。所以你需要注入一个列表,而不是单个类:@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 thestatic getInstance()
method onJAXBContext
doesn't take a singleClass
argument, it takes a vararg list of them. So you need to inject a list, not a single class:你尝试过 Spring OXM 吗?最后一行很重要,命名空间仅供参考:
请参阅 8.4。基于 XML 模式的配置。您的类路径中还需要
spring-oxm
。Have you tried Spring OXM? The last line is important, namespaces are for reference only:
See 8.4. XML Schema-based Configuration. Yu'll also need
spring-oxm
on your classpath.这将解决 spring env 中的 jaxb.index 文件或 ObjectFactory 问题。提供包的值,其中的类是生成 xml 的类
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