将 JAXBContext 注入 spring

发布于 2024-10-25 06:30:28 字数 773 浏览 3 评论 0原文

我试图将 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.in​​dex 我确实将 jaxb.in​​dex 文件放入包“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉城已无爱 2024-11-01 06:30:28

@Tomasz 的答案是我推荐的解决方案,但如果您想坚持使用 JAXBContext,那么您的第一个示例失败的原因是 JAXBContext 上的 static getInstance() 方法不采用单个 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>

@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>
咋地 2024-11-01 06:30:28

你尝试过 Spring OXM 吗?最后一行很重要,命名空间仅供参考:

<?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>

请参阅 8.4。基于 XML 模式的配置。您的类路径中还需要 spring-oxm

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.

静若繁花 2024-11-01 06:30:28

这将解决 spring env 中的 jaxb.in​​dex 文件或 ObjectFactory 问题。提供包的值,其中的类是生成 xml 的类

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

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

    <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="packagesToScan" >
            <value>com.adarsh.spring.integration.entities</value>
        </property>
   </bean>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文