简单模拟 Spring
项目结构
主要代码如下
1
2
3
4
5
6
|
public class User { private String username; private String password; // getter and setter .. } |
1
2
3
4
5
6
|
<beans> <bean id= "u" class = "com.demo.dao.impl.UserDAOImpl" /> <bean id= "userService" class = "com.demo.service.UserService" > <property name= "userDAO" bean= "u" /> </bean> </beans> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
/** * 管理器 * * @author jerome_s@qq.com */ public class ClassPathXmlApplicationContext implements BeanFactory { private Map<String, Object> beans = new HashMap<String, Object>(); /** * 初始化配置文件 * 模拟spring配置文件。模拟大的工厂,把东西都写到大的配置文件,通过代码读取xml文件模拟spring。 * * @throws Exception */ @SuppressWarnings ( "unchecked" ) public ClassPathXmlApplicationContext() throws Exception { System.out.println( "ClassPathXmlApplicationContext init." ); SAXBuilder sb = new SAXBuilder(); Document doc = sb.build( this .getClass().getClassLoader().getResourceAsStream( "beans.xml" )); // 构造文档对象 Element root = doc.getRootElement(); // 获取根元素HD List<Element> list = root.getChildren( "bean" ); // 取名字为bean的所有元素 // 循环每个bean for ( int i = 0 ; i < list.size(); i++) { Element element = list.get(i); String id = element.getAttributeValue( "id" ); String clazz = element.getAttributeValue( "class" ); Object o = Class.forName(clazz).newInstance(); System.out.println( "ClassPathXmlApplicationContext bean's id = " + id); System.out.println( "ClassPathXmlApplicationContext bean's class = " + clazz); beans.put(id, o); // 循环bean下的property for (Element propertyElement : (List<Element>) element.getChildren( "property" )) { // 调用 setUserDAO 设置 userDao的值 String name = propertyElement.getAttributeValue( "name" ); // userDAO String bean = propertyElement.getAttributeValue( "bean" ); // u System.out.println( "ClassPathXmlApplicationContext bean's property name = " + name); System.out.println( "ClassPathXmlApplicationContext bean's property bean = " + bean); Object beanObject = beans.get(bean); // UserDAOImpl instance // userDAO -> setUserDao String methodName = "set" + name.substring( 0 , 1 ).toUpperCase() + name.substring( 1 ); Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[ 0 ]); m.invoke(o, beanObject); System.out.println( "ClassPathXmlApplicationContext bean's property invoke method " + methodName); } System.out.println( "-------------------------" ); } } public Object getBean(String id) { return beans.get(id); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class UserService { private UserDAO userDAO; public void add(User user) { userDAO.save(user); } public UserDAO getUserDAO() { return userDAO; } public void setUserDAO(UserDAO userDAO) { this .userDAO = userDAO; } } |
1
2
3
4
5
6
7
8
|
public class UserDAOImpl implements UserDAO { public void save(User user) { System.out.println( "UserDAOImpl saved()" ); System.out.println( "user.toString() = " + user.toString()); } } |
源码
简单模拟 Spring的更多相关文章
- (反射+内省机制的运用)简单模拟spring IoC容器的操作
简单模拟spring IoC容器的操作[管理对象的创建.管理对象的依赖关系,例如属性设置] 实体类Hello package com.shan.hello; public class Hello { ...
- 一道面试题,简单模拟spring ioc
自己实现的,程序写的土了点,很多情况没去考虑,主要是复习理解怎么使用反射来实现spring 的依赖注入. package dom4jtest; import java.lang.reflect.Inv ...
- 简单模拟Spring的注入
主要就是读XML技术和反射技术. 在xml中读出相关配置信息,然后利用反射将其实例化为对象,并调用其构造方法,在实例化的过程中将属性注入实例. 实例化和属性注入这些操作都交给了框架,不再需要自己的去n ...
- 简单模拟Spring管理Bean对象
1: 首先我们要利用dom4j进行xml的解析,将所有的bean的配置读取出来. 2:利用java的反射机制进行对象的实例化. 3: 直接获得对象 package cn.Junit.test; imp ...
- 【Spring系列】- 手写模拟Spring框架
简单模拟Spring 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! 前言 上次已经学习了 ...
- spring之mvc原理分析及简单模拟实现
在之前的一篇博客中已经简单的实现了spring的IOC和DI功能,本文将在之前的基础上实现mvc功能. 一 什么是MVC MVC简单的说就是一种软件实现的设计模式,将整个系统进行分层,M(model ...
- java web学习总结(二十二) -------------------简单模拟SpringMVC
在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: ...
- 工厂模式模拟Spring的bean加载过程
一.前言 在日常的开发过程,经常使用或碰到的设计模式有代理.工厂.单例.反射模式等等.下面就对工厂模式模拟spring的bean加载过程进行解析,如果对工厂模式不熟悉的,具体可以先去学习一下工厂 ...
- 尚学堂Spring视频教程(一):模拟Spring
Spring简单的说就是作为控制反转的容器,看这篇文章前需要先搞懂“控制反转和依赖注入“这个设计模式 我们先来模拟Spring,实现用户添加的功能,新建WEB项目”Spring_0100_Abstra ...
随机推荐
- html取地址栏参数的方法
现在的人写的博客真是日常挖坑 闲的蛋疼 想把所有东西都转成jstl格式 有个界面是取地址栏的信息的 之前用的是 <%--不用jstl的方法直接传递--%> <%--String ro ...
- Zookeeper 快速入门(上)
来源:holynull, blog.leanote.com/post/holynull/Zookeeper 如有好文章投稿,请点击 → 这里了解详情 Zookeeper是Hadoop分布式调度服务,用 ...
- 从Object.definedProperty中看vue的双向数据的绑定
前言 Object.defineProperty是ES5中的方法,它可以直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象.vue.js正式利用这种方法实现数据的双向绑定,以 ...
- WIN2008虚拟机安装ORACLE11g记录
---恢复内容开始--- 1.ORACLE11g的安装包下载与解压 官网下载地址:(http://www.oracle.com/technetwork/database/enterprise-edit ...
- Hibernate异常之Integer转float(自动类型转换错误)
错误代码: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Float at org.hiber ...
- ACM KMP 格式输入导致TLE
在写 Oulipo POJ - 3461 时候遇上的奇怪的问题 在格式输入上不一样,提交的时候返回TLE,两段代码如下: A#include<iostream> #include< ...
- ACM 最小公倍数
给定两个正整数,计算这两个数的最小公倍数. Input 输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数.Output对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行. ...
- XML Condition And
<Target Name="CustomBuildStep" Condition="'@(CustomBuildStep)' != '' and '$(Select ...
- Android新特性Instant Run详解
关于 Instant Run Android Studio 2.0 中引入的 Instant Run 是 Run 和 Debug 命令的行为,可以大幅缩短应用更新的时间.尽管首次构建可能需要花费较长的 ...
- 理解性能的奥秘——应用程序中慢,SSMS中快(2)——SQL Server如何编译存储过程
本文属于<理解性能的奥秘--应用程序中慢,SSMS中快>系列 接上文:理解性能的奥秘--应用程序中慢,SSMS中快(1)--简介 本文介绍SQL Server如何编译存储过程并使用计划缓存 ...