我的漫漫程序之旅
專注于JavaWeb開發(fā)
隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
數(shù)據(jù)加載中……
簡單模擬spring的容器管理bean
package
com.spring;
import
java.io.IOException;
import
java.net.URL;
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
import
org.jdom.Document;
import
org.jdom.Element;
import
org.jdom.JDOMException;
import
org.jdom.input.SAXBuilder;
import
org.jdom.xpath.XPath;
import
com.vo.BeanDefinition;
/** */
/**
* 簡單模擬spring的ClassPathXmlApplicationContext spring容器管理bean
*
*
@author
zdw
*
*/
@SuppressWarnings(
"
unchecked
"
)
public
class
MyApplicationContext
{
private
List
<
BeanDefinition
>
list
=
new
ArrayList
<
BeanDefinition
>
();
private
Map
<
String, Object
>
sigletons
=
new
HashMap
<
String, Object
>
();
public
MyApplicationContext(String fileName)
{
this
.readXML(fileName);
try
{
instanceBeans();
}
catch
(Exception e)
{
e.printStackTrace();
}
}
/** */
/**
* 實例化所有bean
*
*
@throws
InstantiationException
*
@throws
IllegalAccessException
*
@throws
ClassNotFoundException
*/
private
void
instanceBeans()
throws
InstantiationException,
IllegalAccessException, ClassNotFoundException
{
for
(BeanDefinition bean : list)
{
if
(
null
!=
bean.getClassName()
&&
bean.getClassName().length()
>
0
)
sigletons.put(bean.getId(), Class.forName(bean.getClassName())
.newInstance());
}
}
/** */
/**
* 讀取xml配置
*
*
@param
fileName
*/
public
void
readXML(String fileName)
{
SAXBuilder builder
=
new
SAXBuilder();
URL url
=
this
.getClass().getClassLoader().getResource(fileName);
try
{
Document doc
=
builder.build(url);
XPath xpath
=
XPath.newInstance(
"
//ns:beans/ns:bean
"
);
xpath.addNamespace(
"
ns
"
,
"
http://www.springframework.org/schema/beans
"
);
List
<
Element
>
beans
=
xpath.selectNodes(doc);
for
(Element bean : beans)
{
String id
=
bean.getAttributeValue(
"
id
"
);
String className
=
bean.getAttributeValue(
"
class
"
);
BeanDefinition beanDefinition
=
new
BeanDefinition(id,
className);
list.add(beanDefinition);
}
}
catch
(JDOMException e)
{
e.printStackTrace();
}
catch
(IOException e)
{
e.printStackTrace();
}
}
/** */
/**
* 得到指定id的bean
*
*
@param
id
*
@return
*/
public
Object getBean(String id)
{
return
sigletons.get(id);
}
}
測試:
@Test
public
void
testMyUserService()
{
MyApplicationContext ctx
=
new
MyApplicationContext(
"
beans.xml
"
);
UserService us
=
(UserService) ctx.getBean(
"
userService
"
);
us.print();
}
下載地址1
下載地址2
posted on 2009-01-22 11:00
々上善若水々
閱讀(1574)
評論(2)
編輯
收藏
評論
#
re: 簡單模擬spring的容器管理bean
回復(fù)
更多評論
哈哈,簡單的對象管理器,你的依賴呢! 沒啥用!
2009-01-26 10:05 |
Jack.Wang
#
re: 簡單模擬spring的容器管理bean[未登錄]
回復(fù)
更多評論
@Jack.Wang
簡單便于我們理解spring的原理。
2009-01-29 09:07 |
angel
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Powered by:
BlogJava
Copyright © 々上善若水々
導(dǎo)航
BlogJava
首頁
新隨筆
聯(lián)系
管理
<
2009年1月
>
日
一
二
三
四
五
六
28
29
30
31
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
1
2
3
4
5
6
7
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(39)
給我留言
查看公開留言
查看私人留言
隨筆檔案
(43)
2021年12月 (1)
2012年12月 (1)
2012年11月 (1)
2009年11月 (2)
2009年10月 (1)
2009年8月 (1)
2009年7月 (1)
2009年5月 (2)
2009年4月 (2)
2009年3月 (1)
2009年2月 (3)
2009年1月 (1)
2008年12月 (2)
2008年11月 (3)
2008年9月 (2)
2008年8月 (1)
2008年7月 (2)
2008年6月 (2)
2008年5月 (9)
2008年4月 (2)
2007年12月 (3)
文章分類
(304)
AJAX(7)
(rss)
flex3(1)
(rss)
Hibernate(4)
(rss)
J2EE
(rss)
J2ME(2)
(rss)
J2SE(40)
(rss)
JavaScript(59)
(rss)
JavaWeb(27)
(rss)
Java筆試與面試(52)
(rss)
JQuery(1)
(rss)
Maven(2)
(rss)
opensource(15)
(rss)
Spring(10)
(rss)
SSH整合系列(2)
(rss)
Struts1.x(3)
(rss)
Struts2(13)
(rss)
WebService(14)
(rss)
數(shù)據(jù)庫(18)
(rss)
數(shù)據(jù)結(jié)構(gòu)與算法(7)
(rss)
設(shè)計模式(17)
(rss)
軟件測試(10)
(rss)
文章檔案
(257)
2021年12月 (1)
2012年12月 (2)
2012年9月 (2)
2012年7月 (5)
2012年1月 (1)
2011年10月 (1)
2010年12月 (1)
2009年9月 (2)
2009年7月 (2)
2009年6月 (1)
2009年4月 (3)
2009年3月 (3)
2009年1月 (2)
2008年12月 (4)
2008年11月 (4)
2008年10月 (2)
2008年9月 (2)
2008年8月 (10)
2008年7月 (22)
2008年6月 (13)
2008年5月 (45)
2008年4月 (17)
2008年3月 (5)
2008年2月 (11)
2008年1月 (27)
2007年12月 (45)
2007年11月 (24)
搜索
最新評論
1.?re: 使用MyEclipse構(gòu)建MAVEN項目
評論內(nèi)容較長,點擊標(biāo)題查看
--龍平
2.?re: JDK中的URLConnection參數(shù)詳解[未登錄]
好文要頂
--wy
3.?re: Ant path 匹配原則[未登錄]
我喜歡你惠茹
--aaa
4.?re: Ant path 匹配原則[未登錄]
dfdfdfdfd
--aaa
5.?re: 利用jSMSEngine開源包進行短信的收發(fā)[未登錄]
qq郵箱
550725115@qq.com
--我是菜鳥
閱讀排行榜
1.?Linux上安裝JDK1.7與Tomcat7.0(24262)
2.?將java應(yīng)用程序打包成獨立運行的.exe方法(19828)
3.?HttpURLConnection請求數(shù)據(jù)流的寫入(write)和讀取(read)(19095)
4.?基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換(17625)
5.?javascript文件夾選擇框的兩種解決方案(16929)
評論排行榜
1.?基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換(27)
2.?Flash圖表(FusionChartsV3)的簡單應(yīng)用 (22)
3.?WEB頁面導(dǎo)出為Word文檔后分頁&橫向打印的方法 (14)
4.?javascript文件夾選擇框的兩種解決方案(10)
5.?[原創(chuàng)]J2ME/J2EE實現(xiàn)用戶登錄交互(9)
主站蜘蛛池模板:
阿图什市
|
松溪县
|
应城市
|
辽中县
|
扶余县
|
洪洞县
|
乡宁县
|
资兴市
|
安塞县
|
巩留县
|
察雅县
|
博湖县
|
巢湖市
|
葵青区
|
日喀则市
|
从江县
|
卢氏县
|
铜山县
|
邳州市
|
元谋县
|
宁城县
|
新安县
|
玛曲县
|
昂仁县
|
嘉祥县
|
宜宾县
|
彰化县
|
巴彦县
|
乐陵市
|
凭祥市
|
武山县
|
玉龙
|
灯塔市
|
宜丰县
|
吉首市
|
永丰县
|
泰州市
|
庆元县
|
东乡
|
荆州市
|
手游
|