将 JAXBContext 注入 spring

问题描述 投票:0回答:3
spring jaxb code-injection
3个回答
15
投票

@Tomasz 的答案是我推荐的解决方案,但如果您想坚持使用

JAXBContext
,那么您的第一个示例失败的原因是 static getInstance()
 上的 
JAXBContext
方法不接受单个
 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>

11
投票

你尝试过 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


2
投票

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

    <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="packagesToScan" >
            <value>com.adarsh.spring.integration.entities</value>
        </property>
   </bean>
© www.soinside.com 2019 - 2024. All rights reserved.