手写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;
}
public
T 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框架能够正确注入用户实例和服务类。

上一篇:禁止反射创建实例、 禁止修改破坏单例模式
下一篇:java 锁的分类

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2025年04月30日 17时30分02秒