1

我有以下情况:

这些项目有一个通用配置文件和一个特定配置文件,我需要使用 jaxb 解组创建特定配置文件的实例。

超类 ConfigA.java:

package net.test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "config")
@XmlAccessorType(XmlAccessType.FIELD)
public class ConfigA {

private String attribute1;

private String attribute2;

public String getAttribute1() {
    return attribute1;
}

public void setAttribute1(String attribute1) {
    this.attribute1 = attribute1;
}

public String getAttribute2() {
    return attribute2;
}

public void setAttribute2(String attribute2) {
    this.attribute2 = attribute2;
}

}

和子类 ConfigB.java:

package net.test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "config")
@XmlAccessorType(XmlAccessType.FIELD)
public class ConfigB extends ConfigA {

private String attribute3;

public String getAttribute3() {
    return attribute3;
}

public void setAttribute3(String attribute3) {
    this.attribute3 = attribute3;
}
}

对应的xml文件:

<config>
    <attribute1>a</attribute1>
    <attribute2>b</attribute2>
    <attribute3>c</attribute3>
</config>

工厂类是:

package net.test;

import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class ConfigFactory {

    public static <T extends ConfigA> T createConfig(String file, Class<T> clazz) {
        T config = null;

        JAXBContext context = null;
        Unmarshaller unmarshaller = null;

        try {
            file = "/" + file;
            InputStream stream = ConfigFactory.class.getResourceAsStream(file);
            if (stream == null) {
                // Error handling
            }
            if (clazz != null) {
                context = JAXBContext.newInstance(ConfigA.class, clazz);
            } else {
                context = JAXBContext.newInstance(ConfigA.class);
            }
            unmarshaller = context.createUnmarshaller();

            unmarshaller.setSchema(null); // No Schema
            config = (T)unmarshaller.unmarshal(stream);

        } catch (JAXBException e) {
            // exception handling
        } catch (Exception e) {
            // exception handling
        }
        return config;
    }
}

现在junit测试是:

@Test
public void testConfig() throws Exception {
    ConfigB config = ConfigFactory.createConfig("config.xml", ConfigB.class);
    assertNotNull(config);
    assertEquals(config.getClass(), ConfigB.class);
}

这适用于 Sun 的 jaxb 参考实现:jaxb-impl-2.1.9.jar,但在 Webshpere 下,ConfigFactory.createConfig() 方法始终只返回 ConfigA 的实例,而不是 ConfigB。

在 createConfig() 方法中尝试了以下操作:

JAXBContext.newInstance(clazz, ConfigA.class);

JAXBContext.newInstance(clazz);

使用这两种代码,即使使用 jaxb 参考实现,也只会创建 ConfigA 实例。

谁能帮我吗?

多谢!

4

0 回答 0