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 服務器使用(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)
主站蜘蛛池模板:
亚洲av无码一区二区三区网站
|
四虎永久精品免费观看
|
亚洲啪啪综合AV一区
|
羞羞视频免费观看
|
亚洲第一区精品日韩在线播放
|
亚洲精品无码日韩国产不卡av
|
国产成人免费高清激情视频
|
亚洲熟妇无码一区二区三区
|
最近高清国语中文在线观看免费
|
亚洲成年人电影在线观看
|
4hu四虎最新免费地址
|
亚洲精品亚洲人成在线观看麻豆
|
亚洲产国偷V产偷V自拍色戒
|
一级毛片大全免费播放下载
|
亚洲成av人片一区二区三区
|
成人免费夜片在线观看
|
AV在线亚洲男人的天堂
|
亚洲欧洲日产国码二区首页
|
99久久国产免费-99久久国产免费 99久久国产免费中文无字幕
|
国产一级淫片免费播放电影
|
亚洲熟妇无码一区二区三区
|
四虎永久免费地址在线观看
|
免费无码国产V片在线观看
|
国产亚洲精品久久久久秋霞
|
国产精品亚洲片在线
|
91久久青青草原线免费
|
亚洲天堂免费在线
|
亚洲Av无码乱码在线观看性色
|
二个人看的www免费视频
|
亚洲首页在线观看
|
青青草国产免费久久久下载
|
国产精品午夜免费观看网站
|
久久亚洲AV成人无码
|
免费观看a级毛片
|
中文字幕不卡免费高清视频
|
亚洲精品电影在线
|
又粗又大又硬又爽的免费视频
|
精品国产成人亚洲午夜福利
|
亚洲精品456播放
|
99xxoo视频在线永久免费观看
|
亚洲欧美一区二区三区日产
|