利用java反射读取xml配置文件
2014-04-05 16:13
696 查看
原文链接:http://www.cnblogs.com/flyoung2008/archive/2011/10/05/2199613.html
一、利用java反射机制创建类的实例分为两种情况:
1、不带参数
2、带参数
二、使用dom4j解析xml
需要:dom4j-2.0.0-ALPHA1.jar
下载地址:http://sourceforge.net/projects/dom4j/files/
测试用xml文件
使用Dom4j解析xml
输出结果
参考:http://xhy0422.iteye.com/blog/50235
三、使用java发射机制创建类的实例
业务接口
业务接口实现
读取xml配置文件创建类的实例
输出结果
此文是为利用动态代理实现简单的aop作铺垫。
作者:像少年啦飞驰
出处:http://www.cnblogs.com/flyoung2008/
Blog:http://www.flyoung.me
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
一、利用java反射机制创建类的实例分为两种情况:
1、不带参数
Class c = Class.forName("className");//返回与带有给定字符串名的类 或接口相关联的 Class 对象。 Object object = c.newInstance();//创建此 Class 对象所表示的类的一个新实例。 System.out.println(object);
2、带参数
Class c=Class.forName("className"); Class[] ptype=new Class[]{double.class,int.class}; Constructor constructor=c.getConstructor(ptype);//返回一个 Constructor 对象,它反映此 Class 对象所表示的类的指定公共构造方法 Object[] obj=new Object[]{new Double(3.1415),new Integer(123)}; Object object=construct.newInstance(obt);//使用此 Constructor 对象表示的构造方法来创建该构造方法的声明类的新实例,并用指定的初始化参数初始化该实例。 System.out.println(object);
二、使用dom4j解析xml
需要:dom4j-2.0.0-ALPHA1.jar
下载地址:http://sourceforge.net/projects/dom4j/files/
测试用xml文件
<?xml version="1.0" encoding="UTF-8"?> <package> <action name="hello" class="com.flyoung.HelloWorldIml"></action> </package>
使用Dom4j解析xml
package com.flyoung; import org.dom4j.io.SAXReader; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.Attribute; import java.util.Iterator; import java.util.List; import java.io.File; public class TestDom4j { /** * 获取指定xml文档的Document对象,xml文件必须在classpath中可以找到 * * @param xmlFilePath xml文件路径 * @return Document对象 */ public static Document parse2Document(String xmlFilePath){ SAXReader reader = new SAXReader(); Document doc = null; try { doc = reader.read(new File(xmlFilePath)); } catch (DocumentException e) { e.printStackTrace(); } return doc; } public static void testParseXmlData(String xmlFilePath){ //获取xml解析器对象 //SAXReader reader = new SAXReader(); //将xml解析为Document对象 Document doc = TestDom4j.parse2Document(xmlFilePath); //获取文档的根元素 Element root = doc.getRootElement(); //定义保存xml数据的缓冲字符串 StringBuffer sb = new StringBuffer(); for(Iterator i_action=root.elementIterator();i_action.hasNext();){ Element e_action = (Element)i_action.next(); for(Iterator a_action=e_action.attributeIterator();a_action.hasNext();){ Attribute attribute = (Attribute)a_action.next(); sb.append(attribute.getName()+":"+attribute.getValue()); sb.append("\n"); } } System.out.println(sb); } public static void main(String[] args) { TestDom4j.testParseXmlData("E:/workspace/Dom4j/test.xml"); } }
输出结果
name:hello class:com.flyoung.HelloWorldIml
参考:http://xhy0422.iteye.com/blog/50235
三、使用java发射机制创建类的实例
业务接口
package com.flyoung; public interface HelloWorld { public void sayHelloWorld(); }
业务接口实现
package com.flyoung; public class HelloWorldIml implements HelloWorld { public void sayHelloWorld() { System.out.println("Hello World!!!"); } }
读取xml配置文件创建类的实例
package com.flyoung; import org.dom4j.io.SAXReader; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.Attribute; import java.util.Iterator; import java.util.List; import java.io.File; import java.util.Map; import java.util.HashMap; public class TestDom4j { /** * 获取指定xml文档的Document对象,xml文件必须在classpath中可以找到 * * @param xmlFilePath xml文件路径 * @return Document对象 */ public static Document parse2Document(String xmlFilePath){ SAXReader reader = new SAXReader(); Document doc = null; try { doc = reader.read(new File(xmlFilePath)); } catch (DocumentException e) { e.printStackTrace(); } return doc; } public static Map testParseXmlData(String xmlFilePath){ //获取xml解析器对象 //SAXReader reader = new SAXReader(); //将xml解析为Document对象 Document doc = TestDom4j.parse2Document(xmlFilePath); //获取文档的根元素 Element root = doc.getRootElement(); //定义保存xml数据的缓冲字符串 //StringBuffer sb = new StringBuffer(); //定义保存属性、值的map Map<String,String> map = new HashMap<String,String>(); for(Iterator i_action=root.elementIterator();i_action.hasNext();){ Element e_action = (Element)i_action.next(); for(Iterator a_action=e_action.attributeIterator();a_action.hasNext();){ Attribute attribute = (Attribute)a_action.next(); //sb.append(attribute.getName()+":"+attribute.getValue()); //sb.append("\n"); map.put(attribute.getName(), attribute.getValue()); } } //System.out.println(sb); return map; } public static void main(String[] args) { Map map = TestDom4j.testParseXmlData("E:/workspace/Dom4j/test.xml"); String className =(String)map.get("class"); try { Class c = Class.forName(className); HelloWorld hw =(HelloWorld) c.newInstance(); hw.sayHelloWorld(); } catch (ClassNotFoundException e) { e.printStackTrace(); }catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } }
输出结果
Hello World!!!
此文是为利用动态代理实现简单的aop作铺垫。
作者:像少年啦飞驰
出处:http://www.cnblogs.com/flyoung2008/
Blog:http://www.flyoung.me
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
相关文章推荐
- 利用java反射读取xml配置文件
- 利用java反射读取xml配置文件
- 利用反射与dom4j读取javabean生成对应XML和读取XML得到对应的javabean对象集合
- JAVA技术:XML配置文件的读取处理
- Java开发-读取XML与properties配置文件
- java xml配合实现读取配置文件登陆
- java读取xml配置文件(sax)
- Java 利用 ByteArrayOutputStream 和 ByteArrayInputStream 避免重复读取配置文件
- JAVA解析XML配置文件的读取操作
- java:利用Dom读取xml文件
- JAVA读取XML文件并利用该文件对数据库进行配置操作
- JAVA技术:XML配置文件的读取处理
- JAVA中读取XML文件中的配置信息
- java读取xml配置文件
- 利用反射与dom4j读取javabean生成对应XML和读取XML得到对应的javabean对象集合
- 利用Java的Properties 类读取配置文件信息
- java读取xml数据库配置文件--利用dom4j.jar
- java读取xml配置文件(小结)
- java读取系统Properties配置文件利用线程实时监控配置文件变化