手写spring ioc框架
来源:互联网 发布:云管理平台软件 编辑:程序博客网 时间:2024/06/09 01:23
user.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="user1" class="com.huishao.entity.UserEntity">
<property name="userId" value="0001"></property>
<property name="userName" value="张三"></property>
</bean>
<bean id="user2" class="com.huishao.entity.UserEntity">
<property name="userId" value="0002"></property>
<property name="userName" value="李四"></property>
</bean>
</beans>
userEntity.java
package com.huishao.entity;
public class UserEntity {
private String userId;
private String userName;
public UserEntity(){
System.out.println("使用反射技术 ,执行无参数构造 函数");
}
public UserEntity(String userId) {
this.userId=userId;
System.out.println("使用反射技术 执行 有参构造函数 userId:"+userId);
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
ClassPathXmlApplicationContext.java
package com.huishao.spring;
import java.lang.reflect.Field;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.huishao.entity.UserEntity;
public class ClassPathXmlApplicationContext {
private String xmlPath;
public ClassPathXmlApplicationContext(String xmlPath) {
this.xmlPath = xmlPath;
}
public Object getBean(String beanId) throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException {
//1、读取xml配置文件
// 获取xml解析器
SAXReader saxReader = new SAXReader();
// 获得document对象
Document read = saxReader.read(this.getClass().getClassLoader().getResourceAsStream(xmlPath));
// 获得根节点
Element rootElement = read.getRootElement();
System.out.println("根节点的名称: " +rootElement.getName());
//获得元素对象
List<Element> elements = rootElement.elements();
Object obj = null;
for (Element sonEle : elements) {
//2、获取到每个bean配置,获得class地址
//获得每个bean配置 获取class地址
String sonBeanId = sonEle.attributeValue("id");
if(!beanId.equals(sonBeanId)){
continue;
}
String beanClassPath = sonEle.attributeValue("class");
//3、拿到class地址,进行反射技术实例化对象,使用反射api为私有属性赋值
Class<?> forName = Class.forName(beanClassPath);
obj = forName.newInstance();
//拿到成员属性
List<Element> sonSoneleme = sonEle.elements();
for (Element element : sonSoneleme) {
String name = element.attributeValue("name");
String value = element.attributeValue("value");
//使用反射技术为私有属性赋值
Field declaredField = forName.getDeclaredField(name);
//运行往私有属性赋值
declaredField.setAccessible(true);
declaredField.set(obj, value);
}
}
return obj;
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, DocumentException {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("user.xml");
UserEntity user = (UserEntity) classPathXmlApplicationContext.getBean("user1");
System.out.println(user.getUserId() + "---" +user.getUserName());
}
}
- 手写spring ioc框架
- 手写精简版spring 框架后对ioc的体会
- 实现一个简易的IoC框架(上)(此篇与Spring.NET无关,为自己手写IoC框架)
- 手写spring ioc文件配置版
- Spring 框架 IOC本质
- Spring框架之IOC
- Spring框架IoC模块
- spring框架--IOC
- spring框架之ioC
- spring框架之IOC
- Spring框架--IOC
- spring框架IOC
- Spring框架IOC容器
- Spring框架之IOC
- Spring框架之IOC
- Spring框架IOC理解
- Spring框架中的Ioc
- spring框架Ioc学习
- linux部署docker(注意:要64位,不然下载docker-ce时出错)
- [51nod1244]莫比乌斯函数之和
- 算法分析与设计——LeetCode刷题之TwoSum(easy)
- C++ 11标准及知识点的记录(2)
- 爬虫
- 手写spring ioc框架
- 欢迎使用CSDN-markdown编辑器
- Codeforces559C/51nod1486【DP组合数】
- 试验
- 51nod 1675 序列变换 (简单莫比乌斯)
- 深入理解Java虚拟机——JVM类加载机制(类加载过程和类加载器)
- 电阻 电容 并联电路
- 国是怎么落后的---封建思想的存在,一个家为单位的,也是一个私立的
- UVA.12230 Crossing Rivers (期望)