Java实现动态配置-教学案例

本文介绍了一种利用XML配置文件动态加载类并通过反射创建实例的方法。通过解析XML配置文件,将类名映射到一个哈希表中,然后使用反射机制根据配置信息创建指定的类实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package dynamicConfiguration;

/*
 *主类(author by qiangliu)
 */
public class DynamicClassTest {

	public static void main(String[] args) {
		Configutaion conf =  new Configutaion(System.getProperty("user.dir")+"\\src\\class-config.xml") ;
		Printer pr = conf.newPrintInstance() ;
		pr.print();
	}

}

/*
 *
 */
package dynamicConfiguration;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Configutaion {
	private static Hashtable<String, String> classMap = new Hashtable<String, String>();

	private static final Class<?>[] EMPTY_ARRAY = new Class[] {};
	private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = new HashMap<Class<?>, Constructor<?>>(); // 类构造器缓存

	static {
		ClassLoader cL = Thread.currentThread().getContextClassLoader();
		if (cL.getResource("config.xml") != null) {
			System.out.println("config.xml find! ");
		}
	}

	private static ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

	public Configutaion(String filePath) {
		getClassNameFromXml(filePath);
	}
	
	/**
	 * 生成指定类的对象
	 * 
	 * @param <T>
	 * @param theClass
	 * @param conf
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T newPrintInstance() {
		T result;
		try {
			String printClassName = classMap.get("printer") ;
			Class<?> printClass = Class.forName(printClassName, true, classLoader);
			Constructor<T> meth = (Constructor<T>) CONSTRUCTOR_CACHE.get(printClass);
			if (meth == null) {
				meth = (Constructor<T>) printClass.getConstructor(EMPTY_ARRAY);//通过类对象的getConstructor()或getDeclaredConstructor()方法获得构造器
			}
			result = meth.newInstance();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

		return result;
	}

	/**
	 * 解析并保存xml文件的配置参数
	 * @param path
	 */
	@SuppressWarnings("unused")
	private void getClassNameFromXml(String path) {
		try {
			InputStream xmlIns = new FileInputStream(path);
			if (xmlIns == null) {
	            return  ;
	        }
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
			Document doc = builder.parse(xmlIns);
			Element root = doc.getDocumentElement();
			
			NodeList props = root.getChildNodes();
			for (int i = 0; i < props.getLength(); i++) {
				Node propNode = props.item(i);
				if (!(propNode instanceof Element))
					continue;
				Element prop = (Element) propNode;
				if ("class".equals(prop.getTagName())) {
					String classname = prop.getAttribute("name");
					String value = prop.getAttribute("value");
					if (classname != null && value != null) {
						classMap.put(classname, value);
					}
				}
			}
			
			if (xmlIns != null) { //关闭流
				xmlIns.close()  ;
	        }
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

package dynamicConfiguration;

public interface Printer {

	public void print() ;
}

package dynamicConfiguration;

public class Printer1 implements Printer {
	public Printer1() {
	}
	
	public void print() {
		System.out.println("I'm Printer1! ");
	}
}

package dynamicConfiguration;

public class Printer2 implements Printer {
	public Printer2() {
	}
	
	public void print() {
		System.out.println("I'm Printer2! ");
	}
}


<?xml version="1.0" encoding="UTF-8"?>

<configuration>
<!-- 
<class name="printer" value="dynamicConfiguration.Printer1">
</class>
 -->

<class name="printer" value="dynamicConfiguration.Printer2">
</class>

</configuration>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值