隨筆:15 文章:206 評論:35 引用:0
fenglin
創(chuàng)新、創(chuàng)意、挑戰(zhàn)
C++博客
首頁
發(fā)新隨筆
發(fā)新文章
聯(lián)系
聚合
管理
Spring3_動態(tài)代理_AOP_Annotation配置方式
LogInterception.java
package
com.bebig.aop;
import
org.aspectj.lang.annotation.Aspect;
import
org.aspectj.lang.annotation.Before;
import
org.aspectj.lang.annotation.Pointcut;
import
org.springframework.stereotype.Component;
@Aspect
//
需要加入Aspectj包文件才能使用,而Aspectj包又需要aopalliance包支持
@Component
//
此實現(xiàn)方法對象必須由Spring容器來初始化才能實現(xiàn)代理
public
class
LogInterception
{
//
@Before("execution(public void com.bebig.dao.impl.UserDAOImpl.save(com.bebig.model.User))")
//
@Before("execution(public * com.bebig.dao.impl.UserDAOImpl.*(..))")
//
@Before("execution(* save(..))")
//
@Before("within(com.bebig.dao.impl..*)")
//
將此包及其子包內(nèi)所有方法進行代理
//
其它注解請查Spring文檔
@Before(
"
myMethod()
"
)
public
void
before()
{
System.out.println(
"
Log start
"
);
}
@Pointcut(
"
within(com.bebig.service..*)
"
)
//
代理沒有實現(xiàn)接口的對象時,是由CGLib直接生成二進制文件的,因此需要cglib包文件與objectweb.asm包文件支持
public
void
myMethod()
{
}
;
}
UserDAO.java
package
com.bebig.dao;
import
com.bebig.model.User;
public
interface
UserDAO
{
public
void
save(User u);
public
void
deleteById(
int
id);
}
UserDAOImpl.java
package
com.bebig.dao.impl;
import
org.springframework.stereotype.Repository;
import
com.bebig.dao.UserDAO;
import
com.bebig.model.User;
@Repository
public
class
UserDAOImpl
implements
UserDAO
{
@Override
public
void
save(User u)
{
System.out.println(
"
a user saved!
"
);
}
@Override
public
void
deleteById(
int
id)
{
System.out.println(
"
delete a user by id!
"
);
}
}
User.java
package
com.bebig.model;
public
class
User
{
private
String username;
private
String password;
public
String getPassword()
{
return
password;
}
public
String getUsername()
{
return
username;
}
public
void
setPassword(String password)
{
this
.password
=
password;
}
public
void
setUsername(String username)
{
this
.username
=
username;
}
}
UserService.java
package
com.bebig.service;
import
javax.annotation.Resource;
import
org.springframework.stereotype.Service;
import
com.bebig.dao.UserDAO;
import
com.bebig.model.User;
@Service(
"
userService
"
)
public
class
UserService
{
private
UserDAO userDAO;
public
void
add(User u)
{
userDAO.save(u);
}
public
void
deleteById(
int
id)
{
userDAO.deleteById(id);
}
@Resource
public
void
setUserDAO(UserDAO userDAO)
{
this
.userDAO
=
userDAO;
}
public
UserDAO getUserDAO()
{
return
userDAO;
}
public
void
init()
{
System.out.println(
"
init.
"
);
}
public
void
destory()
{
System.out.println(
"
destory.
"
);
}
}
beans.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<
beans
xmlns
="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context
="http://www.springframework.org/schema/context"
xmlns:aop
="http://www.springframework.org/schema/aop"
xsi:schemaLocation
="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
>
<!--
a service object; we will be profiling its methods
-->
<
context:annotation-config
/>
<
context:component-scan
base-package
="com.bebig"
/>
<!--
動態(tài)代理Annotation編寫方法
-->
<
aop:aspectj-autoproxy
/>
</
beans
>
UserServiceTest.java
package
com.bebig.service;
import
org.junit.Test;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import
com.bebig.model.User;
public
class
UserServiceTest
{
@Test
public
void
testAdd()
throws
Exception
{
ClassPathXmlApplicationContext cxt
=
new
ClassPathXmlApplicationContext(
"
beans.xml
"
);
//
不再需要使用Proxy對象來創(chuàng)建代理對象
UserService service
=
(UserService) cxt.getBean(
"
userService
"
);
service.add(
new
User());
service.deleteById(
1
);
}
}
發(fā)表于 2010-10-19 17:13
風(fēng)林
閱讀(627)
評論(0)
編輯
收藏
引用
所屬分類:
JAVA
、
Spring
只有注冊用戶
登錄
后才能發(fā)表評論。
【推薦】100%開源!大型工業(yè)跨平臺軟件C++源碼提供,建模,組態(tài)!
相關(guān)文章:
[轉(zhuǎn)]java編程中'為了性能'一些盡量做到的地方
JAVAC命令行錯誤
Java基礎(chǔ)_Collection接口下的子類存儲特性
Android_常用控件使用
Android_Activity&Intent&Layout
Integer自動裝箱、拆箱問題
自定義比較器、排序
JDBC操作實例
常用數(shù)據(jù)庫的JDBC連接代碼
[轉(zhuǎn)]J2SE_內(nèi)部類
網(wǎng)站導(dǎo)航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
CALENDER
<
2025年8月
>
日
一
二
三
四
五
六
27
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
常用鏈接
我的隨筆
我的評論
我參與的隨筆
留言簿
給我留言
查看公開留言
查看私人留言
隨筆分類
jBPM
(rss)
隨筆檔案
2011年7月 (1)
2011年4月 (1)
2011年2月 (1)
2010年11月 (1)
2010年10月 (2)
2010年9月 (3)
2010年8月 (4)
2010年7月 (2)
文章分類
AJAX(2)
(rss)
Android(2)
(rss)
C#(20)
(rss)
C++(6)
(rss)
ckeditor&ckfinder(1)
(rss)
CSS
(rss)
Delphi(2)
(rss)
Hibernate(39)
(rss)
JAVA(95)
(rss)
jQuery(1)
(rss)
JSP(9)
(rss)
Maven(1)
(rss)
MySQL(4)
(rss)
OOP(1)
(rss)
Python(42)
(rss)
Spring(31)
(rss)
SQL Server(4)
(rss)
Struts2(35)
(rss)
SVN(1)
(rss)
Tomcat(1)
(rss)
Ubuntu(1)
(rss)
軟件加解密技術(shù)
(rss)
雜文(1)
(rss)
文章檔案
2011年8月 (1)
2011年7月 (3)
2011年6月 (19)
2011年5月 (2)
2011年4月 (1)
2011年2月 (1)
2010年12月 (2)
2010年11月 (21)
2010年10月 (67)
2010年9月 (48)
2010年8月 (37)
2010年7月 (4)
新聞檔案
2010年7月 (1)
相冊
CSS
Hibernate
搜索
最新評論
1.?re: Struts2_三種傳參數(shù)方法
方式的發(fā)生
--阿飛史蒂夫
2.?re: 在Win7上搭建JSP開發(fā)環(huán)境
評論內(nèi)容較長,點擊標題查看
--鄒
3.?re: ckeditor&ckfinder&s2sh集成
評論內(nèi)容較長,點擊標題查看
--庸幾何
4.?re: 在Win7上搭建JSP開發(fā)環(huán)境
下個 myeclipse@lou
--孫毅
5.?re: 在Win7上搭建JSP開發(fā)環(huán)境
@lou
運行 -cmd 找到startup.bat 在java 環(huán)境中運行
--孫毅
閱讀排行榜
1.?開始找Java開發(fā)類工作了(515)
2.?給一家公司的軟件做加密方案(450)
3.?一流、二流、三流(436)
4.?周一到周五都得出差,周末才能回(431)
5.?從WIN32開發(fā)轉(zhuǎn)型到WEB開發(fā)(388)
評論排行榜
1.?周一到周五都得出差,周末才能回(2)
2.?給力2011(2)
3.?最近工作有點忙(0)
4.?生活(0)
5.?在博客園開博了(0)
Powered By:
博客園
模板提供
:
滬江博客
91精品国产综合久久精品
|
久久人人爽人人爽人人av东京热
|
久久久久久av无码免费看大片
|
亚洲AV无码久久精品蜜桃
|
亚洲第一极品精品无码久久
|
久久久久亚洲AV无码永不
|
99久久精品国产高清一区二区
|
精品久久久久久久
|
久久久久se色偷偷亚洲精品av
|
久久精品www人人爽人人
|
欧美无乱码久久久免费午夜一区二区三区中文字幕
|
国产偷久久久精品专区
|
久久99精品国产99久久6
|
伊人色综合久久天天人手人婷
|
国产日产久久高清欧美一区
|
久久天天婷婷五月俺也去
|
国产成人精品久久免费动漫
|
囯产极品美女高潮无套久久久
|
久久99精品久久久久久噜噜
|
欧美一区二区精品久久
|
国产91久久精品一区二区
|
亚洲国产精品无码久久98
|
国产精品久久久久久久app
|
久久综合九色综合久99
|
日批日出水久久亚洲精品tv
|
99久久精品这里只有精品
|
久久午夜无码鲁丝片午夜精品
|
AAA级久久久精品无码区
|
久久毛片免费看一区二区三区
|
久久国产免费直播
|
无码伊人66久久大杳蕉网站谷歌
|
久久久久久久波多野结衣高潮
|
久久精品国产亚洲av日韩
|
久久WWW免费人成一看片
|
91精品国产高清久久久久久国产嫩草
|
伊人久久大香线蕉影院95
|
久久亚洲AV无码精品色午夜
|
一本一本久久a久久综合精品蜜桃 一本一道久久综合狠狠老
|
人妻无码精品久久亚瑟影视
|
日韩久久久久久中文人妻
|
久久亚洲日韩看片无码
|