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上一些表達都不太好。
在此我盡量把我參考的原文給大家,
與大家學習。^_^
最近在維護www.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 服務器使用(14266)
2.?mysql 權限控制筆記(11670)
3.?mysql 游標使用(7120)
4.?強大的 ant scp 和 sshexec (6388)
5.?CGLib 學習(5783)
評論排行榜
1.?hibernate 多服務器數據同步問題(支持延遲同步)(12)
2.?部分高級查詢 sql 拼寫筆記 (mysql)(9)
3.?為在平安夜還在加班的程序員祝福!(8)
4.?原創小框架: 動態面向對象數據庫操作(不要影射類哦)(6)
5.?自用小框架:DB工廠(6)
主站蜘蛛池模板:
又色又污又黄无遮挡的免费视
|
国产美女无遮挡免费网站
|
日韩电影免费在线观看网站
|
青青操在线免费观看
|
在线视频免费观看高清
|
国产伦精品一区二区三区免费迷
|
亚洲αv久久久噜噜噜噜噜
|
亚洲理论在线观看
|
亚洲AV日韩综合一区
|
国产又黄又爽胸又大免费视频
|
免费看美女裸露无档网站
|
爱情岛论坛网亚洲品质自拍
|
久久亚洲日韩看片无码
|
免费在线人人电影网
|
8x8×在线永久免费视频
|
免费永久在线观看黄网站
|
另类小说亚洲色图
|
**真实毛片免费观看
|
久久亚洲国产精品123区
|
亚洲乱码在线播放
|
aa午夜免费剧场
|
成年女人毛片免费视频
|
亚洲成AV人片在线观看WWW
|
未满十八私人高清免费影院
|
午夜视频免费成人
|
亚洲毛片免费视频
|
成人毛片18岁女人毛片免费看
|
免费的黄色的网站
|
亚洲春色在线视频
|
114一级毛片免费
|
亚洲AV中文无码乱人伦下载
|
四虎成年永久免费网站
|
亚洲国产精品无码中文字
|
美女内射毛片在线看免费人动物
|
亚洲色大成网站www永久男同
|
99re6热视频精品免费观看
|
ZZIJZZIJ亚洲日本少妇JIZJIZ
|
亚洲免费人成在线视频观看
|
国产aⅴ无码专区亚洲av麻豆
|
成年男女免费视频网站
|
亚洲国产精品成人午夜在线观看
|