XStream模拟解析applicationContext.xml

前端之家收集整理的这篇文章主要介绍了XStream模拟解析applicationContext.xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


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

猜你在找的XML相关文章