【问题标题】:JAXB Is there some way to unmarshal static variables?JAXB 有没有办法解组静态变量?
【发布时间】:2012-03-11 23:47:33
【问题描述】:

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

简化的设置类:

@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 的所有静态变量设置为“天生”与新创建的对象匹配,但似乎不是。
有没有办法在保留静态变量的同时实现这种行为?还是我对保存/加载应用程序设置的方法完全错误?请帮忙:)

【问题讨论】:

    标签: java xml variables static jaxb


    【解决方案1】:

    我建议制作两组变量,一组是私有的,另一组是公共的静态变量。

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

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

    【讨论】:

      【解决方案2】:

      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! :-)
          }
      
      }
      

      【讨论】:

        【解决方案3】:

        好的,我刚刚做了解决方法,请参见下面的代码:

        设置类:

        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
        

        还有没有更好的解决方案来保存/加载应用设置?无论如何解组静态变量呢?我的解决方案就是不使用它们。

        【讨论】:

        • 玩过之后,我制作了 SettingsHolder.Setting 实例 public static final,从文件中的 static {} 块 初始化它。如果文件不存在,它将创建新的设置实例,这是默认设置。
        猜你喜欢
        • 1970-01-01
        • 2016-05-31
        • 2015-12-22
        • 1970-01-01
        • 1970-01-01
        • 2014-01-17
        • 2021-10-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多