一道面试题,简单模拟spring ioc
自己实现的,程序写的土了点,很多情况没去考虑,主要是复习理解怎么使用反射来实现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的更多相关文章
- (反射+内省机制的运用)简单模拟spring IoC容器的操作
简单模拟spring IoC容器的操作[管理对象的创建.管理对象的依赖关系,例如属性设置] 实体类Hello package com.shan.hello; public class Hello { ...
- 简单模拟 Spring
简单的理解Spring的实现过程,模拟了Spring的读取配置文件 项目结构
- 简单模拟Spring的注入
主要就是读XML技术和反射技术. 在xml中读出相关配置信息,然后利用反射将其实例化为对象,并调用其构造方法,在实例化的过程中将属性注入实例. 实例化和属性注入这些操作都交给了框架,不再需要自己的去n ...
- 由一道面试题简单扩展 — setTimeout、闭包
在一个前端公众号,看到这么一个号称简单的面试题: 1.以下程序输出什么? <script type="text/javascript"> function init() ...
- 简单模拟Spring管理Bean对象
1: 首先我们要利用dom4j进行xml的解析,将所有的bean的配置读取出来. 2:利用java的反射机制进行对象的实例化. 3: 直接获得对象 package cn.Junit.test; imp ...
- Spring IOC 相关的面试题
Spring最基础的部分就是IOC,对IOC的理解程度从某个方面代表着你对Spring 的理解程度,看了网上的一些面试题,针对Spring IOC相关的重点是下面几个: 1.Spring中Bean ...
- Spring容器的简单实现(IOC原理)
引言:容器是什么?什么是容器?Spring容器又是啥东西?我给Spring容器一个对象名字,为啥能给我创建一个对象呢? 一.容器是装东西的,就像你家的水缸,你吃饭的碗等等. java中能作为容器的有很 ...
- 应聘阿里,字节跳动,美团必须掌握的Spring IOC与工厂模式
Spring IOC与工厂模式 PS:本文内容较为硬核,需要对java的面向对象.反射.类加载器.泛型.properties.XML等基础知识有较深理解. (一)简单介绍 在讲Spring IOC之前 ...
- 【Spring系列】- 手写模拟Spring框架
简单模拟Spring 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! 前言 上次已经学习了 ...
随机推荐
- unicode 和 utf-8 的关系和解释
首先一个字节就是8个晶体管同时发出的信号集, unicode就是一套编码,所有的字符都用2个字节表示,不像gbk和gb2312既保持了以前的ansi/ascii的字符单个字节编码,有发明了两个字节保存 ...
- virtualBox虚拟机安装与主机互访和实现上网配置
1.到官方网下载vitualBox虚拟机,目前最新版本5.0,并安装. https://www.virtualbox.org/ 2.下载安装CentOS系统安装文件 本文采用CentOS-6.5-i3 ...
- WPF: DatePicker
获取DatePicker:date_Start上显示的时间date_Start.SelectedDate 比较两个dateTime类型的时间: DateTime StartDate =Convert. ...
- jsp-------------之分页技术(一)
jsp分页技术之: 如下图:百度的喵 看上图中卡哇伊的小苗的爪子下面的数字,就是分页啦!那我们如何做出这样一个效果呢? 下面我们来逐一分解: jsp分页技术一 : (算法) /* int pageS ...
- hdu----(1950)Bridging signals(最长递增子序列 (LIS) )
Bridging signals Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Binary Tree Level Order Traversal II [LeetCode]
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- sql 语句随机时间存储过程
CREATE PROC [dbo].[Proc_GetRandomTime](@startTime datetime,@endTime datetime,@date datetime output ) ...
- 记一次web项目总结
功能需求,登录,用户管理,新闻管理. 用户管理: // 分页查询所有用户信息 public List<User> userInfo(int index, int pageSize) thr ...
- Android Bundle、Handler和Message类介绍
Bundle是一个载体,可以存放基本数据类型.对象等内容,相当于一辆汽车,可以装载很多东西,然后运到需要的地方,例如: Bundle mBundle=new Bundle(); mBundle.put ...
- MySql的导入与导出
1.导入 load data infile '/tmp/yhb/skin_info.txt' into table t_skin fields terminated by '\t' (skin_id, ...