1、applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans> <bean id="hello" className="com.ifly.demo.Hello"></bean> <bean id="userService" className="com.ifly.demo.UserService"></bean> </beans>
2、Bean.java 实体类
public class Bean { private String id; private String className; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } }
3、解析applicationContext.xml 方法如下
public ClassPathXmlApplicationContext(String fileName) throws Exception { XStream xStream = new XStream(new DomDriver()); xStream.alias("beans",ArrayList.class); xStream.alias("bean",Bean.class); xStream.aliasAttribute(Bean.class,"id","id"); xStream.aliasAttribute(Bean.class,"className","className"); InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName); List<Bean> list = (List<Bean>) xStream.fromXML(in); for (Bean bean : list) { System.out.println(bean); } }原文链接:https://www.f2er.com/xml/297544.html