从xml中初始化bean实例
需要从配置文件中初始化一个bean实例,并且里面需要包含一个Map类型的属性,相关代码如下:
xml
<?xml version="1.0" encoding="UTF-8"?> <java version="1.6.0_23" name="code">InputStream inputStream = new FileInputStream("c:/outxml.xml"); XMLDecoder d = new XMLDecoder(inputStream, null, null); Object obj = d.readObject(); if (obj instanceof UserInfo) { UserInfo u = (UserInfo) obj; Map m = u.getTest(); System.out.println(m.get("test1")); System.out.println(m.get("test3")); }
public static class UserInfo implements Serializable { private String name; private String password; private Map<String, String> test; public Map<String, String> getTest() { return test; } public void setTest(Map<String, String> test) { this.test = test; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { final StringBuilder sb = new StringBuilder(); sb.append("UserInfo"); sb.append("{name='").append(name).append('\''); sb.append(", password='").append(password).append('\''); sb.append(", test=").append(test); sb.append('}'); return sb.toString(); } }