Skynet
posts - 165, comments - 198, trackbacks - 0, articles - 1
BlogJava
::
首頁
::
新隨筆
::
聯系
::
聚合
::
管理
為我學習 Spring AOP 準備(Proxy)
Posted on 2007-10-09 11:13
G_G
閱讀(1116)
評論(2)
編輯
收藏
所屬分類:
Spring
為我準備學習和理解spring
特留下代碼筆記:
參考:
http://dev.csdn.net/author/labile/e70c97cb7f504d35b7b5350e7810cc5a.html
代碼感想:
???
沒個方法都或多或少需要環境參數(如: jdbc的conn ,hbn的session...等等 ),方法結束后又要關閉。
何不用proxy代理并用配置文件的方法來 關,開session 等
如:
以下是我的想法并不是實際可用
???
配置:
<
class?
name
="
HelloWorldImpl
"
>
?
<
function?
name
="set*"
/>
? <
function?
name
="getName"
>
???
<
proxyBegin artt="name"
value
="liukaiyi"
/>
??? ???
//使用Proxy來賦值 name那在實現代碼中就可以不用去關注象 session 等屬性的開關了
??? ??? //proxy中配置下 , 在實現類中 就使用 就可以了
??? <
proxyBegin?
ref
="HelloWorldHandler.doBefter"
?args
="null"
/>
??? <
proxyEnd?
ref
="HelloWorldHandler.doAfter"
?args
="null"
/>
? </
function
>
? <
function?
=?name="sayHelloWorld">
??? <proxyEnd?ref
="HelloWorldHandler.doAfter"
?args
="null"
/>
? </
function
>
</
class
>
代碼:
HelloWorld hw =
(
HelloWorld
)Factory.getBean(
"HelloWorldImpl"
);
hw.getName();
結果是:
before method invoke!
劉凱毅
after method invoke!
在此 我只是想象,spring 還沒有看,但我認為spring 這個著名的框架應該在這方面有很好的實現。
實際代碼:
希望spring可以向我上面的方法配置好用來取代下面的實際代碼 ^_^
package
?test.proxy;
import
?java.lang.reflect.InvocationHandler;
import
?java.lang.reflect.Method;
import
?java.lang.reflect.Proxy;
import
?junit.framework.TestCase;
public
?
class
?TestProxy?
extends
?TestCase?{
????
????
protected
?
void
?setUp()?
throws
?Exception?{
????????
super
.setUp();
????}
????
????
public
?
void
?testProxy(){
?????????????HelloWorld?hw?
=
?
new
?HelloWorldImpl();???????????
???????????????????????InvocationHandler?handler?
=
?
new
?HelloWorldHandler(hw);??????????
???????????????????????HelloWorld?proxy?
=
?(HelloWorld)?Proxy.newProxyInstance(???
?????????????????????????????????????hw.getClass().getClassLoader(),???
?????????????????????????????????????hw.getClass().getInterfaces(),???
?????????????????????????????????????handler);???
???????????????????????proxy.sayHelloWorld();
???????????????????????
???????????????????????System.out.println();
???????????????????????proxy.setName(
"
liukaiyi
"
);
???????????????????????proxy.getName();
????}
????
}
interface
?HelloWorld?{???
????
void
?sayHelloWorld()?;???
????
void
?getName();
????
void
?setName(String?name);
}
class
?HelloWorldImpl?
implements
?HelloWorld?{???
????
private
?String?name?
=
?
""
;
????
public
?
void
?setName(String?name)?{
????????
this
.name?
=
?name;
????}
????
public
?
void
?sayHelloWorld()?{???
????????System.out.println(
"
Hello?World!
"
);??????????????
????}
????
public
?
void
?getName()?{
????????System.out.println(
this
.name);
????}???
}
class
?HelloWorldHandler?
implements
?InvocationHandler?{???
????????????
//
要代理的原始對象???
????????????
private
?Object?objOriginal;???
????????????
/**
??
?????????????*?構造函數。??
?????????????*?
@param
?obj?要代理的原始對象。??
?????????????
*/
??
????????????
public
?HelloWorldHandler(Object?obj)?{???
???????????????????
this
.objOriginal?
=
?obj?;???
????????????}???
????????????
public
?Object?invoke(Object?proxy,?Method?method,?Object[]?args)???
??????????????????????????
throws
?Throwable?{???????????????
???????????????????Object?result?
=
?
null
?;??????????????
???????????????????String?meName?
=
?method.getName();
???????????????????
if
(meName.indexOf(
"
set
"
)
>-
1
?){
???????????????????????
return
?method.invoke(
this
.objOriginal?,args);???
???????????????????}
???????????????????
if
(?meName.equals(
"
getName
"
)?){
????????????????????
//
方法調用之前???
???????????????????????????doBefore();?
//
仿佛是AOP的影子,呵呵???
???????????????????}
?????????????????????
//
調用原始對象的方法???
???????????????????????????result?
=
?method.invoke(
this
.objOriginal?,args);???
?????????????????????
//
方法調用之后???
???????????????????????????doAfter();???????
???????????????????
???????????????????
return
?result?;???
????????????}???
????????????
private
?
void
?doBefore()?{???
???????????????????System.out.println(
"
before?method?invoke!
"
);???
????????????}???
????????????
private
?
void
?doAfter()?{???
???????????????????System.out.println(
"
after?method?invoke!
"
);???
????????????}???
?????}???
結果:
Hello World!
after method invoke!
before method invoke!
劉凱毅
after method invoke!
評論
#
re: 為我學習 Spring AOP 準備(Proxy)
回復
更多評論
2007-10-09 12:05 by
千里冰封
呵呵,用代理還是比較好玩的
#
re: 為我學習 Spring AOP 準備(Proxy)
回復
更多評論
2008-03-13 18:14 by
bless
要是很多不同的被代理類的函數返回值、里面的判斷條件等等的好多種,很復雜,那invoke里豈不是要寫很多判斷條件,那時候怎么辦啊。
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關文章:
spring 與 hibernate 整合(事務)
spring AspectJ 基本使用
spring 學習筆記
為我學習 Spring AOP 準備(Proxy)
Powered by:
BlogJava
Copyright © G_G
日歷
<
2007年10月
>
日
一
二
三
四
五
六
30
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
8
9
10
公告
由于時間問題,
blog上一些表達都不太好。
在此我盡量把我參考的原文給大家,
與大家學習。^_^
最近在維護m.tkk7.com\Skynet 腳本中
有什么技術問題不會,
我很愿意和大家討論!
多交流快成長
liukaiyi@gmail.com
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(13)
給我留言
查看公開留言
查看私人留言
隨筆分類
ant(6)
AOP(5)
Database(9)
E_Study(3)
EJB(4)
hibernate(25)
HTML(4)
Inspiration(11)
Jakarta Commons(3)
javaGeneral(28)
javascript(28)
javascript Framework(7)
JDBC(5)
json(3)
JspTag(12)
JUnit(8)
Other(5)
Reportform(3)
Spring(4)
struct(5)
Swing(1)
xml Related(7)
隨筆檔案
2009年4月 (1)
2009年2月 (2)
2009年1月 (1)
2008年12月 (4)
2008年11月 (1)
2008年9月 (7)
2008年8月 (8)
2008年7月 (12)
2008年6月 (9)
2008年5月 (5)
2008年4月 (11)
2008年3月 (6)
2008年1月 (8)
2007年12月 (13)
2007年11月 (13)
2007年10月 (15)
2007年9月 (11)
2007年8月 (9)
2007年7月 (7)
2007年6月 (6)
2007年5月 (3)
2007年4月 (9)
文章檔案
2007年4月 (1)
相冊
EJB
hbn
ss
wz
hibernate
cjdbc
hibernate.cache
Hibernate中outer-join、lazy 、fetch join關鍵字的使用
My連接
一個讀取Gmail郵件的簡單程序
bat 1%
eXtremeTable limit
java+web
jsp java javascrip 交互
JS函數集合大全
strut c:
Struts Menu
tree jsp
上手JFreeChart
關鍵筆記
根據name調用getName方法
用JAVA實現一個分頁類
搜索
最新評論
1.?re: 序列化和反序列化對象到 數據庫
qwe
--erwqe
2.?re: 部分高級查詢 sql 拼寫筆記 (mysql)
說實話,寫的的的確是差了那么一點點
--老衲用飄柔
3.?re: html 簡單 div 拖動
好。
--火星華人
4.?re: hibernate 多服務器數據同步問題(支持延遲同步)
評論內容較長,點擊標題查看
--張久強
5.?re: 數據庫表地址數據(中國地區)
謝謝分享,收藏了!
--久久快樂鮮花
閱讀排行榜
1.?google svn 服務器使用(14267)
2.?mysql 權限控制筆記(11672)
3.?mysql 游標使用(7121)
4.?強大的 ant scp 和 sshexec (6391)
5.?CGLib 學習(5784)
評論排行榜
1.?hibernate 多服務器數據同步問題(支持延遲同步)(12)
2.?部分高級查詢 sql 拼寫筆記 (mysql)(9)
3.?為在平安夜還在加班的程序員祝福!(8)
4.?原創小框架: 動態面向對象數據庫操作(不要影射類哦)(6)
5.?自用小框架:DB工廠(6)
主站蜘蛛池模板:
麻豆国产VA免费精品高清在线
|
a级毛片免费观看视频
|
国产免费av片在线看
|
91亚洲国产成人久久精品网址
|
久久久国产精品福利免费
|
久久精品亚洲一区二区
|
最近2019免费中文字幕6
|
亚洲精品国产手机
|
18国产精品白浆在线观看免费
|
亚洲狠狠ady亚洲精品大秀
|
91视频免费网址
|
亚洲精品天堂在线观看
|
免费无码又爽又高潮视频
|
国产综合激情在线亚洲第一页
|
亚洲福利视频一区二区
|
久久久久久av无码免费看大片
|
我想看一级毛片免费的
|
日韩亚洲翔田千里在线
|
国产亚洲精品自在线观看
|
日本高清不卡aⅴ免费网站
|
久久亚洲国产成人精品性色
|
在线看免费观看AV深夜影院
|
亚洲色中文字幕在线播放
|
国产成人免费片在线观看
|
ssswww日本免费网站片
|
97se亚洲综合在线
|
成人a视频片在线观看免费
|
青青免费在线视频
|
亚洲国产精品热久久
|
成人无遮挡裸免费视频在线观看
|
18禁亚洲深夜福利人口
|
久久久亚洲精品视频
|
毛片免费全部免费观看
|
国产免费A∨在线播放
|
久久精品国产亚洲AV大全
|
国产免费黄色大片
|
久久精品无码专区免费青青
|
特级aa**毛片免费观看
|
一区二区三区亚洲
|
亚洲成a人片在线观看国产
|
久9久9精品免费观看
|