手写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());
    }
}



原创粉丝点击