为spring Jaxb2Marshaller指定一个包而不是"classesToBeBound"

San*_*ore 13 java spring jaxb2

我试图使用Jaxb2Marshaller使用spring编组一组java类.我知道这可以使用以下代码完成

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            <value>com.example.test1</value>
            <value>com.example.test2</value>
        </list>
    </property>
</bean>  
Run Code Online (Sandbox Code Playgroud)

我想要做的是不是指定类列表,而是仅指定包含所有类的包名(在上面的例子中是com.example).

有没有人知道这样做的方法,或任何其他方式不要求我列出所有类.任何帮助,将不胜感激 !

谢谢.

tom*_*asb 11

从Spring 3.1(我认为)你也可以使用packagesToScan属性,它接受通配符.它只适用于没有@XmlRootElement注释的元素,就像contextPath属性一样.这些需要生成对象工厂.

看起来像:

<property name="packagesToScan">
    <list>
        <value>com.test.*</value>
        <value>com.*.test</value>
    </list>
</property>
Run Code Online (Sandbox Code Playgroud)


Lai*_*Lai 5

您可以使用以下语法设置contextPath:

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="contextPath" value="com.example"/>
</bean>  
Run Code Online (Sandbox Code Playgroud)