自己实现的,程序写的土了点,很多情况没去考虑,主要是复习理解怎么使用反射来实现spring 的依赖注入。

package dom4jtest;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Test3 {
    private static Object o1;
    public static void main(String[] args) {
        BeanA ba = (BeanA) getBean("beanA");
        ba.getBeanB().show();
    }
    public static Object getBean(String name){
        if(o1==null)
            o1 = setBean(name);
        return o1;
    }
    @SuppressWarnings("unchecked")
    public static Object setBean(String name){
        SAXReader sr = new SAXReader();
        Object o = null;
        Class clz = null;
        Class clz2 = null;
        String ref = null;
        String name1 = null;
        try {
            Document doc = sr.read(Test3.class.getClassLoader().getResourceAsStream("beans.xml"));
            Element e = doc.getRootElement();
            @SuppressWarnings("unchecked")
            List<Element> eles = e.elements();
            for(Element ee : eles){
                if(ee.attributeValue("id")!=null){
                    if(ee.attributeValue("id").equals(name)){
                        clz = Class.forName(ee.attributeValue("class"));
                        o = clz.newInstance();

                        List<Element> elus = ee.elements("property");
                        ref = elus.get(0).attributeValue("ref");
                        name1 = elus.get(0).attributeValue("name");
                        if(ref!=null&&name1!=null){
                            for(Element eee:eles){
                                if(eee.attributeValue("id")!=null){
                                    if(eee.attributeValue("id").equals(ref)){
                                        clz2 = Class.forName(eee.attributeValue("class"));
                                        name1 ="set"+ name1.substring(0, 1).toUpperCase() + name1.substring(1);
                                        Method m = clz.getDeclaredMethod(name1,clz2);
                                        m.invoke(o, clz2.newInstance());
                                    }

                                }
                            }
                        }

                    }
                }

            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        } catch (InstantiationException e1) {
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        } catch (IllegalArgumentException e1) {
            e1.printStackTrace();
        }
        catch (InvocationTargetException e1) {
            e1.printStackTrace();
        } catch (NoSuchMethodException e1) {
            e1.printStackTrace();
        }
        catch (SecurityException e1) {
            e1.printStackTrace();
        }
        return o;
    }
}

一道面试题,简单模拟spring ioc的更多相关文章

  1. (反射+内省机制的运用)简单模拟spring IoC容器的操作

    简单模拟spring IoC容器的操作[管理对象的创建.管理对象的依赖关系,例如属性设置] 实体类Hello package com.shan.hello; public class Hello { ...

  2. 简单模拟 Spring

            简单的理解Spring的实现过程,模拟了Spring的读取配置文件 项目结构

  3. 简单模拟Spring的注入

    主要就是读XML技术和反射技术. 在xml中读出相关配置信息,然后利用反射将其实例化为对象,并调用其构造方法,在实例化的过程中将属性注入实例. 实例化和属性注入这些操作都交给了框架,不再需要自己的去n ...

  4. 由一道面试题简单扩展 — setTimeout、闭包

    在一个前端公众号,看到这么一个号称简单的面试题: 1.以下程序输出什么? <script type="text/javascript"> function init() ...

  5. 简单模拟Spring管理Bean对象

    1: 首先我们要利用dom4j进行xml的解析,将所有的bean的配置读取出来. 2:利用java的反射机制进行对象的实例化. 3: 直接获得对象 package cn.Junit.test; imp ...

  6. Spring IOC 相关的面试题

    Spring最基础的部分就是IOC,对IOC的理解程度从某个方面代表着你对Spring  的理解程度,看了网上的一些面试题,针对Spring IOC相关的重点是下面几个: 1.Spring中Bean ...

  7. Spring容器的简单实现(IOC原理)

    引言:容器是什么?什么是容器?Spring容器又是啥东西?我给Spring容器一个对象名字,为啥能给我创建一个对象呢? 一.容器是装东西的,就像你家的水缸,你吃饭的碗等等. java中能作为容器的有很 ...

  8. 应聘阿里,字节跳动,美团必须掌握的Spring IOC与工厂模式

    Spring IOC与工厂模式 PS:本文内容较为硬核,需要对java的面向对象.反射.类加载器.泛型.properties.XML等基础知识有较深理解. (一)简单介绍 在讲Spring IOC之前 ...

  9. 【Spring系列】- 手写模拟Spring框架

    简单模拟Spring 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! 前言 上次已经学习了 ...

随机推荐

  1. SQL Server常见基础操作

    1. 常见针对表的操作(增删改查) --1. Create Table USE [MVC_000] CREATE TABLE T_TableName ( ID ,) PRIMARY KEY, Name ...

  2. php中mysql数据库异步查询实现

    问题 通常一个web应用的性能瓶颈在数据库.因为,通常情况下php中mysql查询是串行的.也就是说,如果指定两条sql语句时,第二条sql语句会等到第一条sql语句执行完毕再去执行.这个时候,如果执 ...

  3. matlab灰度变彩色+白平衡算法实现

    % matlab彩色图像分离通道并合并成处理后的彩色图像(彩色变灰度再变彩色) % 白平衡算法(灰度世界法)消除RGB受光照影响 clear all; close all; %读入原始图像 srcIm ...

  4. form表单控件

    $("select option[value='" + queryparams['vendor'] + "']").attr("selected&qu ...

  5. Spring 事务机制详解

    原文出处: 陶邦仁 Spring事务机制主要包括声明式事务和编程式事务,此处侧重讲解声明式事务,编程式事务在实际开发中得不到广泛使用,仅供学习参考. Spring声明式事务让我们从复杂的事务处理中得到 ...

  6. hdu----(3118)Arbiter(构造二分图)

    Arbiter Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total S ...

  7. HDU-------(2795)Billboard(线段树区间更新)

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. php solr 查询

    $options = array( 'hostname' => 'localhost', 'port' => 8080, 'path' => 'solr/test'); $clien ...

  9. XPATH 带命名空间数据的读取

    在XML中,很多情况下有命名空间,如果直接使用XPATH 读取是会读到空节点. 解决办法如下: InputStream is=loader.getResourceAsStream("com/ ...

  10. 如何在datagridview 的head上绘制一个全选按钮

    winform的项目中,经常要用到datagridview控件,但是为控件添加DataGridViewCheckBoxColumn来实现数据行选择这个功能的时候,经常需要提供全选反选功能,如果不重绘控 ...