自己手撕一个spring ioc容器

用那么多年spring,一直对spring容器是如何实现的感到神秘,今天自己手撕一个spring ioc容器 感觉很简单,适合入门理解。

主要用到xml的解析 和 反射

 

public class BeanUtil {

	private static Map<String, Object> beans = new HashMap<String, Object>();

	private static void parse() {
		// 获取项目根路径下的配置文件
		Resource resource = new ClassPathResource("applicationContext.xml");
		File file;
		try {
			file = resource.getFile();

			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document document = db.parse(file);
			NodeList employees = document.getChildNodes();

			for (int i = 0; i < employees.getLength(); i++) {
				Node employee = employees.item(i);
				NodeList employeeInfo = employee.getChildNodes();
				for (int j = 0; j < employeeInfo.getLength(); j++) {
					Node node = employeeInfo.item(j);
					String nodeName = node.getNodeName();

					// 解析二级标签 即 beans 下的 bean
					if ("bean".equals(nodeName)) {
						NamedNodeMap attributes = node.getAttributes();
						Node beanName = attributes.getNamedItem("id");
						Node cl = attributes.getNamedItem("class");

						String name = beanName.getNodeValue();
						String clazzName = cl.getNodeValue();

						System.out.println(name + " :: " + clazzName);
						// 利用反射 获取class对象
						Class<?> clazz = Class.forName(clazzName);
						//获取 指定class的实例
						Object instance = clazz.newInstance();
						//将去放入map 相当于一级缓存 也可以放入到redis 等 缓存工具
						beans.put(name, instance);

					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}

	}

	public static Object getBean(String beanName) {
		parse();
		return beans.get(beanName);
	}
}

这里只是简单的实现解析配置文件里的bean,对于context:componte-scan并没有实现 下期将会实现 本例有不明白的地方可以加企鹅:2624112814

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值