0

I use Eclipse with Maven (m2eclipse plugin) and JIBX to (un)marshall XML.

It works if I use the factory like this:

IBindingFactory bindingFactory = BindingDirectory.getFactory(mappedClass);

However I want to create a factory based on the binding file, because this is done by the automatically generated service stub. So I run the following test in TestNG:

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

and it fails with the following error message:

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

The name (filename is binding.xml) is correct, the empty "" means no package, which is also correct, but could be a problem I guess?

The factory is generated under target/classes/JiBX_bindingFactory.class in my project folder, so it should be found! (Remember everything works if I specify a concrete toplevel binded class)

Any help would be appreciated!

The build section in my pom.xml file:

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

1 回答 1

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,否则调用不明确!

旁注:方法 getFactory(String, String) 调用 BindingDirectory 中的方法 getFactory(String, String, ClassLoader)

于 2013-05-14T11:18:21.550 回答