我有一类具有以下属性的用户:
/** The username. */
private String username;
/** The password. */
private String password;
/** The list of role names the user has. */
private List<String> roleNames;
/** The list of partners with the associated database */
private DbPartner dbPartner;
/** The list of message types with the associated database */
private DbMessageType dbMessageType;
我使用XStream在文件中写入值并从中获取它们。问题是嵌套对象(dbPartner和dbMessageTypes)的值未填充,当我从xml获取用户时它们为null。我该怎么办?
这是我用来从文件中获取数据的方法:
private void lazyLoad() {
synchronized (ConfigurationDAOImpl.class) {
// Has the configuration been loaded
if (configuration == null) {
if (filename.exists()) {
try {
XStream xStream = new XStream(new DomDriver());
xStream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
xStream.alias("configuration", Configuration.class);
xStream.alias("user", User.class);
xStream.alias("dbPartner", DbPartner.class);
xStream.alias("dbMessageType", DbMessageType.class);
configuration = (Configuration) xStream
.fromXML(filename.getInputStream());
LOGGER.debug("Loaded configuration from {}.", filename);
} catch (Exception e) {
LOGGER.error("Failed to load configuration.", e);
}
} else {
LOGGER.debug("{} does not exist.", filename);
LOGGER.debug("Creating blank configuration.");
configuration = new Configuration();
configuration.setUsers(new ArrayList<User>());
// and store it
store();
}
}
}
}
private void store() {
XStream xStream = new XStream();
xStream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
xStream.alias("configuration", Configuration.class);
xStream.alias("user", User.class);
xStream.alias("dbPartner", DbPartner.class);
xStream.alias("dbMessageType", DbMessageType.class);
synchronized (ConfigurationDAOImpl.class) {
try {
xStream.toXML(configuration, new FileOutputStream(filename.getFile()));
} catch (IOException e) {
throw new RuntimeException("Failed to write to " + filename, e);
}
}
}