zoukankan      html  css  js  c++  java
  • springIOC实现原理模拟(springIOC底层使用xml解析+反射实现)

    springIOC底层使用xml解析+反射实现。

    模拟ClassPathXmlApplicationContext:

    import java.io.File;
    import java.lang.reflect.Field;
    import java.util.Iterator;
    
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    
    public class ClassPathXmlApplicationContextCopy {
        private Document document;
        
        public ClassPathXmlApplicationContextCopy(String location){
            // 解析xml
            parseXML(location);
        }
        
        private void parseXML(String location) {
            try {
                SAXReader saxReader = new SAXReader();
                Document document = saxReader.read(new File(location));
                this.document = document;
            } catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        public Object getBean(String name){
            // 1.根据name匹配到bean。2.根据节点中class的值反射实例化。3.遍历子节点set值。
            // 匹配对应bean
            Element root = document.getRootElement();
            Iterator beans = root.elementIterator();
            Object instance = null;
            while(beans.hasNext()){
                Element ele = (Element) beans.next();
                if(name.equals(ele.attributeValue("id"))){
                    String classPath = ele.attributeValue("class");
                    try {
                        Class<?> clazz = Class.forName(classPath);
                        // 反射实例化
                        instance = clazz.newInstance();
                        // set值
                        Iterator nodes = ele.elementIterator();
                        while(nodes.hasNext()){
                            Element node = (Element) nodes.next();
                            String fieldName = node.attributeValue("name");
                            String fieldValue = node.attributeValue("value");
                            try {
                                Field f = clazz.getDeclaredField(fieldName);
                                f.setAccessible(true);
                                f.set(instance, fieldValue);
                            } catch (NoSuchFieldException e) {
                                e.printStackTrace();
                            } catch (SecurityException e) {
                                e.printStackTrace();
                            } catch (IllegalArgumentException e) {
                                e.printStackTrace();
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                            // 有get*(String *)方法
                            /*for(Field f : clazz.getDeclaredFields()){
                                if(f.getName().equals(fieldName)){
                                    f.setAccessible(true);
                                    f.set(instance, fieldValue);
                                }
                            }*/
                        }
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (SecurityException e) {
                        e.printStackTrace();
                    }
                }
            }
            return instance;
        }
    }

    测试:

    public class Test {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            ClassPathXmlApplicationContextCopy ctx = new ClassPathXmlApplicationContextCopy("D:/eclipse-jee-juno-win32/eclipse/workspace/SpringTest/resources/applicationContext.xml");
            UserEntity userEntity = (UserEntity) ctx.getBean("user2");
            System.out.println(userEntity.getUserId() + ":" + userEntity.getUserName());
            
            /*SpringHelper springHelper = (SpringHelper) ctx.getBean("springHelper");
            ProcessEngine processEngine = springHelper.createProcessEngine();
            List<HistoryTask> hisTaskList = processEngine.getHistoryService().createHistoryTaskQuery().list();
            for(HistoryTask hisTask : hisTaskList){
                System.out.println(hisTask.getId() + ":" + hisTask.getAssignee());
            }*/
            
        }
    
    }

    结果:

    无参构造函数执行...
    0002:张三

    spring的applicationContext.xml配置:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
           xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
           xmlns:cache="http://www.springframework.org/schema/cache"
           xsi:schemaLocation="
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/jdbc
        http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd">
          <bean id="user1" class="com.test.UserEntity">
            <property name="userId" value="0001"></property>
            <property name="userName" value="李四"></property>
        </bean>
        <bean id="user2" class="com.test.UserEntity">
            <property name="userId" value="0002"></property>
            <property name="userName" value="张三"></property>
        </bean>
    </beans>
  • 相关阅读:
    Java多线程系列 JUC锁03 公平锁(一)
    Java多线程系列 JUC锁02 互斥锁ReentrantLock
    JDBC课程3--通过ResultSet执行查询操作
    JDBC课程2--实现Statement(用于执行SQL语句)--使用自定义的JDBCTools的工具类静态方法,包括insert/update/delete三合一
    JDBC_通过DriverManager获得数据库连接
    JDBC课程1-实现Driver接口连接mysql数据库、通用的数据库连接方法(使用文件jdbc.properties)
    [终章]进阶20-流程控制结构--if/case/while结构
    MySQL进阶19--函数的创建(举例)/设置mysql的创建函数的权限/查看(show)/删除(drop) / 举4个栗子
    MySQL进阶18- 存储过程- 创建语句-参数模式(in/out/inout-对应三个例子) -调用语法-delimiter 结束标记'$'- 删除/查看/修改-三个练习
    SQL进阶17-变量的声明/使用(输出)--全局变量/会话变量--用户变量/局部变量
  • 原文地址:https://www.cnblogs.com/super-chao/p/14926492.html
Copyright © 2011-2022 走看看