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 服務器使用(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)
主站蜘蛛池模板:
免费污视频在线观看
|
午夜dj免费在线观看
|
亚洲日韩在线观看
|
久久亚洲精品成人无码
|
男人的好免费观看在线视频
|
91天堂素人精品系列全集亚洲
|
巨胸喷奶水视频www免费视频
|
亚洲av区一区二区三
|
欧美激情综合亚洲一二区
|
手机在线免费视频
|
亚洲综合丁香婷婷六月香
|
99热这里只有精品6免费
|
亚洲AV无码久久精品成人
|
亚洲人成网站观看在线播放
|
久久精品国产亚洲av品善
|
成人超污免费网站在线看
|
亚洲入口无毒网址你懂的
|
久久99九九国产免费看小说
|
亚洲最大黄色网址
|
精品无码免费专区毛片
|
亚洲黄色免费网址
|
曰批视频免费40分钟试看天天
|
亚洲午夜精品一区二区
|
久久免费的精品国产V∧
|
亚洲成色999久久网站
|
美女被cao网站免费看在线看
|
亚洲VA中文字幕无码毛片
|
精品视频一区二区三区免费
|
亚洲av无码一区二区乱子伦as
|
免费国产叼嘿视频大全网站
|
亚洲Av无码专区国产乱码DVD
|
日韩电影免费在线观看中文字幕
|
亚洲av无码专区在线播放
|
久久久久国产精品免费看
|
亚洲综合精品香蕉久久网97
|
97av免费视频
|
亚洲伊人精品综合在合线
|
国产1024精品视频专区免费
|
亚洲精品国产高清在线观看
|
国产成人免费a在线视频app
|
日本免费人成网ww555在线
|