Spring 简单模拟
来源:互联网 发布:手机壳设计软件 编辑:程序博客网 时间:2024/05/09 04:50
简单的理解Spring的实现过程,模拟了Spring的读取配置文件
项目结构
主要代码如下
User.java
1
2
3
4
5
6
public
class
User {
private
String username;
private
String password;
// getter and setter ..
}
beans.xml 配置文件
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>
ClassPathXmlApplicationContext.java
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);
}
}
UserService.java
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;
}
}
UserDAOImpl.java
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());
}
}
运行 UserServiceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public
class
UserServiceTest {
@Test
public
void
testAdd()
throws
Exception {
BeanFactory applicationContext =
new
ClassPathXmlApplicationContext();
UserService service = (UserService) applicationContext.getBean(
"userService"
);
User u =
new
User();
u.setUsername(
"jerome"
);
u.setPassword(
"jeromepwd"
);
service.add(u);
}
}
运行结果如下
ClassPathXmlApplicationContext init.
ClassPathXmlApplicationContext bean's id = u
ClassPathXmlApplicationContext bean's class = com.demo.dao.impl.UserDAOImpl
-------------------------
ClassPathXmlApplicationContext bean's id = userService
ClassPathXmlApplicationContext bean's class = com.demo.service.UserService
ClassPathXmlApplicationContext bean's property name = userDAO
ClassPathXmlApplicationContext bean's property bean = u
ClassPathXmlApplicationContext bean's property invoke method name = setUserDAO
-------------------------
UserDAOImpl saved()
user.toString() = User [username=jerome, password=jeromepwd]
源码
https://github.com/JeromeSuz/simulation_spring
运行 UserServiceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public
class
UserServiceTest {
@Test
public
void
testAdd()
throws
Exception {
BeanFactory applicationContext =
new
ClassPathXmlApplicationContext();
UserService service = (UserService) applicationContext.getBean(
"userService"
);
User u =
new
User();
u.setUsername(
"jerome"
);
u.setPassword(
"jeromepwd"
);
service.add(u);
}
}
运行结果如下
0 0
- Spring 简单模拟
- spring 注入简单模拟
- spring 简单模拟 ioc
- 简单模拟Spring IOC功能
- Java简单模拟实现Spring
- 简单模拟 Spring 的 ioc
- 01spring spring的简单模拟
- 模拟spring - 简单实现spring IOC
- 操作 spring 简单模拟-aop简单实现
- Spring 架构的简单模拟实现
- Spring学习笔记--- BeanFactory简单模拟
- java简单模拟Spring的依赖注入
- 简单代码 模拟 spring的IOC机制
- 简单模拟spring装载注入bean原理
- spring依赖注入原理的简单模拟
- 最简单的模拟spring容器例子
- 简单模拟Spring容器(一)
- spring简单环境搭建 和spring容器解析模拟
- Find Minimum in Rotated Sorted Array II
- Python Show-Me-the-Code 第 0009 题 提取网页中的超链接
- vmware虚拟机怎样让linux(centos6.4)桥接上网|本机用putty连接centos通信
- 怎么成为java大神?
- 实现xxxAware接口直接访问Servlet API
- Spring 简单模拟
- 背包问题I--最大字段和
- 2014年省赛组队练习赛4
- 输入字符串时输入空格
- 清华尹成老师主办的智锋互联
- github 命令提交工程到网站中(一)
- 调试技巧
- 广播机制小结
- C++ STACK 入门