无法根据绑定文件获取JIBX IBindingFactory

时间:2013-05-13 15:17:38

标签: binding classnotfoundexception jibx

我将Eclipse与Maven(m2eclipse插件)和JIBX用于(un)marshall XML。

如果我像这样使用工厂,它可以工作:

IBindingFactory bindingFactory = BindingDirectory.getFactory(mappedClass);

但是我想基于绑定文件创建工厂,因为这是由自动生成的服务存根完成的。所以我在TestNG中运行以下测试:

    @Test
public void testBindingFactory() {
    try
    {
        IBindingFactory factory = BindingDirectory.getFactory("binding", "");
    } catch (JiBXException e)
    {
        e.printStackTrace();
        fail(e.getMessage());
    }
}

并且失败并显示以下错误消息:

    Unable to access binding 'binding'
    Make sure classes generated by the binding compiler are available at runtime
    java.lang.ClassNotFoundException: .JiBX_bindingFactory

名称(文件名是binding.xml)是正确的,空的“”表示没有包,这也是正确的,但我觉得可能是个问题?

工厂是在我的项目文件夹中的target/classes/JiBX_bindingFactory.class下生成的,所以应该找到它! (如果我指定具体的顶级绑定类,请记住一切正常)

任何帮助将不胜感激!

我的pom.xml文件中的构建部分:

    <build>
    <plugins>
        <plugin>
            <groupId>org.jibx</groupId>
            <artifactId>jibx-maven-plugin</artifactId>
            <version>1.2.5</version>
            <configuration>
                <schemaBindingDirectory>src/main/config</schemaBindingDirectory>
                <includeSchemaBindings>
                    <includeSchemaBinding>binding.xml</includeSchemaBinding>
                </includeSchemaBindings>
                <verbose>true</verbose>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <!-- Do we require other goals? http://jibx.sourceforge.net/maven-jibx-plugin/ -->
                        <goal>bind</goal>
                        <!-- goal>test-bind</goal-->
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

1 个答案:

答案 0 :(得分:0)

找到解决方案,这是getFactory(java.lang.String bname, java.lang.String pack, java.lang.ClassLoader loader)

实施中的一个错误

考虑第一行代码:

    public static IBindingFactory  [More ...] getFactory(String bname, String pack,
    ClassLoader loader) throws JiBXException {

    String cname = (pack == null ? "" : pack + '.') +
        GENERATE_PREFIX + convertName(bname) + BINDINGFACTORY_SUFFIX;
    ...

请记住,自动生成的服务存根称为getFactory方法,如下所示:factory = org.jibx.runtime.BindingDirectory.getFactory("binding", "", ISService_1_0_deStub.class.getClassLoader());

所以空引号被替换为“。”,这会导致异常:java.lang.ClassNotFoundException: .JiBX_bindingFactory如上所述(注意类名前面的点!)。

解决方案是调用以下方法:factory = org.jibx.runtime.BindingDirectory.getFactory("binding", (String)null, ISService_1_0_deStub.class.getClassLoader()); 注意转换为String,否则调用不明确!

Sidenote:方法getFactory(String,String)在BindingDirectory中调用方法getFactory(String,String,ClassLoader)