我的漫漫程序之旅
專注于JavaWeb開發
隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
數據加載中……
簡單模擬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
回復
更多評論
哈哈,簡單的對象管理器,你的依賴呢! 沒啥用!
2009-01-26 10:05 |
Jack.Wang
#
re: 簡單模擬spring的容器管理bean[未登錄]
回復
更多評論
@Jack.Wang
簡單便于我們理解spring的原理。
2009-01-29 09:07 |
angel
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Powered by:
BlogJava
Copyright © 々上善若水々
導航
BlogJava
首頁
新隨筆
聯系
管理
<
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)
數據庫(18)
(rss)
數據結構與算法(7)
(rss)
設計模式(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構建MAVEN項目
評論內容較長,點擊標題查看
--龍平
2.?re: JDK中的URLConnection參數詳解[未登錄]
好文要頂
--wy
3.?re: Ant path 匹配原則[未登錄]
我喜歡你惠茹
--aaa
4.?re: Ant path 匹配原則[未登錄]
dfdfdfdfd
--aaa
5.?re: 利用jSMSEngine開源包進行短信的收發[未登錄]
qq郵箱
550725115@qq.com
--我是菜鳥
閱讀排行榜
1.?Linux上安裝JDK1.7與Tomcat7.0(24262)
2.?將java應用程序打包成獨立運行的.exe方法(19828)
3.?HttpURLConnection請求數據流的寫入(write)和讀取(read)(19095)
4.?基于jsTree的無限級樹JSON數據的轉換(17625)
5.?javascript文件夾選擇框的兩種解決方案(16929)
評論排行榜
1.?基于jsTree的無限級樹JSON數據的轉換(27)
2.?Flash圖表(FusionChartsV3)的簡單應用 (22)
3.?WEB頁面導出為Word文檔后分頁&橫向打印的方法 (14)
4.?javascript文件夾選擇框的兩種解決方案(10)
5.?[原創]J2ME/J2EE實現用戶登錄交互(9)
主站蜘蛛池模板:
桦川县
|
霞浦县
|
万宁市
|
岢岚县
|
凌海市
|
康平县
|
启东市
|
济源市
|
汤原县
|
榆树市
|
七台河市
|
大田县
|
沾益县
|
贵港市
|
灵山县
|
喀喇沁旗
|
离岛区
|
岳池县
|
东丽区
|
雷州市
|
涿州市
|
凌海市
|
喜德县
|
文水县
|
浦城县
|
大邑县
|
阿拉善左旗
|
和静县
|
乳源
|
鄂托克旗
|
济阳县
|
高唐县
|
柘城县
|
郑州市
|
胶州市
|
龙江县
|
镇沅
|
仁寿县
|
九江市
|
上栗县
|
宁蒗
|