
手写springIOC 简易框架(xml配置)
发布日期:2021-05-14 12:38:33
浏览次数:25
分类:精选文章
本文共 4007 字,大约阅读时间需要 13 分钟。
随着开发者的技术要求日益提高,学习新知识才能使我们在面试中胜任并赶上潮流,开发者往往会被问到一些框架的底层实现。与背诵面试题相比,亲自编写一遍能够更深入理解框架的工作原理。因此,我决定手写一个简易的Spring IOC框架,探索其依赖注入的实现原理,避免仅仅停留在理论层面。
项目结构
项目采用Maven进行管理,主要依赖包括DOM4J解析XML的库和Spring相关组件。项目结构设计如下:
src/main/java/com/model
:存放实体类src/main/java/com/service
:存放服务类src/main/resources
:存放Spring配置文件
pom.xml依赖管理
4.0.0 com SpringIOC 1.0-SNAPSHOT dom4j dom4j 1.6.1
UserEntity 实体类
package com.model;public class UserEntity { private String userName; private String userId; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; }}
UserService 服务类
package com.service;import java.text.SimpleDateFormat;import java.util.Date;public class UserService { private String info; public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public String getNowTime() { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); }}
SpringIOC实现
package com;import com.model.UserEntity;import com.service.UserService;import org.dom4j.Attribute;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;import java.lang.reflect.Field;import java.util.List;public class SpringApplicationContext { private String xmlPath; public SpringApplicationContext(String xmlPath) throws DocumentException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException { this.xmlPath = xmlPath; } publicT getBean(String beanId) throws DocumentException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException { SAXReader saxReader = new SAXReader(); Document xmlDom = saxReader.read(getClass().getClassLoader().getResourceAsStream(xmlPath)); Element rootElement = xmlDom.getRootElement(); List elements = rootElement.elements(); for (Element bean : elements) { String className = bean.attributeValue("class"); Class aClass = Class.forName(className); Object obj = aClass.newInstance(); List fieldList = bean.elements(); for (Element fieldItem : fieldList) { String columnName = fieldItem.attributeValue("name"); String value = fieldItem.attributeValue("value"); Field declaredField = aClass.getDeclaredField(columnName); declaredField.setAccessible(true); declaredField.set(obj, value); } String id = bean.attributeValue("id"); if (id.equals(beanId)) { return (T) obj; } } throw new RuntimeException("id不存在"); } public static void main(String[] args) throws ClassNotFoundException, InstantiationException, DocumentException, IllegalAccessException, NoSuchFieldException { SpringApplicationContext springApplicationContext = new SpringApplicationContext("application.xml"); UserEntity user = springApplicationContext.getBean("user1"); System.out.println("用户ID: " + user.getUserId() + ", 用户名: " + user.getUserName()); UserService userService = springApplicationContext.getBean("userService"); System.out.println("当前时间: " + userService.getNowTime()); }}
application.xml配置文件
测试结果
通过Main方法运行,输出结果如下:
用户ID: 0002, 用户名: 张三当前时间: 2023-10-25 00:00:00
测试成功,IOC框架能够正确注入用户实例和服务类。
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2025年04月30日 17时30分02秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
怎么解决Windows 10文件/文件夹正在使用无法删除
2019-03-11
matlab函数:fix 向0取整
2019-03-11
Allegro中如何消除器件本身Pin间距报错
2019-03-11
AD中拖动器件,无法移动在一起如何解决
2019-03-11
linux--练习001-基础类型
2019-03-11
Flask--简介
2019-03-11
Flask模板--过滤器与测试器
2019-03-11
16 python基础-恺撒密码
2019-03-11
06.1 python基础--结构控制
2019-03-11
Frame--Api框架
2019-03-11
idea 在Debug 模式中运行语句中函数的方法
2019-03-11
springboot2.1.1开启druid数据库连接池并开启监控
2019-03-11
《朝花夕拾》金句摘抄(五)
2019-03-11
Boostrap技能点整理之【网格系统】
2019-03-11
新闻发布项目——业务逻辑层(UserService)
2019-03-11
hibernate正向生成数据库表以及配置——hibernate.cfg.xml
2019-03-11
javaWeb服务详解(含源代码,测试通过,注释) ——Emp的Dao层
2019-03-11