JAXB有没有办法解组静态变量?

时间:2012-03-11 23:47:33

标签: java xml variables static jaxb

我有“设置”类,它将我的应用程序的设置存储在静态变量中(从应用程序的任何位置“可见”),我希望能够保存/加载它。

简化设置类:

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Settings {
    @XmlElement
    private static int option = 0;

    private Settings() {
    }

    public static int getOption() {
        return option;
    }
    public static void setOption(int option) {
        Settings.option = option;
    }
}

用于编组的代码:

public static void main(String[] args) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(Settings.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(new Settings(), new File("c:\\test\\test.xml"));
}

输出xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<settings>
    <option>**0**</option>
</settings>

现在问题:当我通过调用Settings.setOption(5)更改 static int option 的值时;如下所示,并且对以前封送选项(为0)进行解组,生成的Settings对象具有与当前Settings.option 相同的Settings.option的值,即5。

Settings.setOption(5);
JAXBContext context = JAXBContext.newInstance(Settings.class);
Settings s2 = (Settings)context.createUnmarshaller().unmarshal(new File("c:\\test\\test.xml"));
// Settings.option is 5, but should be 0!

我只是希望在解组之后它实际上会将Setting的所有静态变量设置为“按性质”匹配新创建的对象,但似乎没有。
有没有办法在保留静态变量的同时实现这种行为?或者我对保存/加载应用程序设置的方法完全错误?请帮助:)

3 个答案:

答案 0 :(得分:1)

我建议制作两组变量,一组是另一组私有。

使用方法将private设置为静态变量。

在我看来,这更容易理解,并且在调用该方法之外以及在该类本身之外的代码中不需要进行任何更改。

答案 1 :(得分:1)

User3862024指出了我正确的方向。

毕竟解决方案非常简单:

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Settings
{

    public static int option = 0;

    private int getOption()
    {
        return option;
    }

    @XmlElement(name = "option")
    private void setOption(int option)
    {
        Settings.option = option;
    }

    public static void main(String[] args) throws JAXBException
    {
        JAXBContext context = JAXBContext.newInstance(Settings.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(new Settings(), new File("c:\\test\\test.xml"));

        Settings.option = 5;
        System.out.println(Settings.option); // prints 5
        context.createUnmarshaller().unmarshal(new File("c:\\test\\test.xml"));
        System.out.println(Settings.option); // prints 0! :-)
    }

}

答案 2 :(得分:0)

好的,我刚解决了,请参阅下面的代码:

设置类:

public final class SettingsHolder
{

    private SettingsHolder() {
        throw new AssertionError();
    }

    public static Settings settings = new Settings();

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.NONE)
    public final static class Settings
    {

        @XmlElement
        private int option = 0;

        public int getOption() {
            return option;
        }

        public void setOption(int option) {
            this.option = option;
        }

    }

}

元帅代码:

JAXBContext context = JAXBContext.newInstance(SettingsHolder.Settings.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(SettingsHolder.settings, new File("c:\\test\\test.xml"));

的test.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<settings>
    <option>0</option>
</settings>

现在解组测试:

SettingsHolder.settings.setOption(5);
JAXBContext context = JAXBContext.newInstance(SettingsHolder.Settings.class);

System.out.println("Old Settings: " + SettingsHolder.settings.getOption());
// prints Old Settings: 5

SettingsHolder.settings = (SettingsHolder.Settings)context.createUnmarshaller().unmarshal(new File("c:\\test\\test.xml"));

System.out.println("New Settings: " + SettingsHolder.settings.getOption());
// prints New Settings: 0

但是,有没有更好的解决方案来保存/加载应用设置?那么解组静态变量呢?我的解决方案就是不使用它们。