隨筆: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:
博客園
模板提供
:
滬江博客
久久er热视频在这里精品
|
国内精品久久久久影院薰衣草
|
久久成人影院精品777
|
91秦先生久久久久久久
|
久久人人爽人人爽人人爽
|
久久精品国产99久久无毒不卡
|
国产亚洲美女精品久久久久狼
|
久久成人小视频
|
久久99精品久久久久久齐齐
|
日本欧美久久久久免费播放网
|
国产精品午夜久久
|
欧美熟妇另类久久久久久不卡
|
久久久久久国产精品无码下载
|
久久综合偷偷噜噜噜色
|
久久国产精品99精品国产987
|
精品人妻伦九区久久AAA片69
|
国产精品狼人久久久久影院
|
久久精品国产精品亚洲艾草网美妙
|
色欲综合久久中文字幕网
|
亚洲人成无码www久久久
|
国产精品激情综合久久
|
久久中文娱乐网
|
2020久久精品国产免费
|
久久婷婷五月综合国产尤物app
|
久久妇女高潮几次MBA
|
欧美激情精品久久久久久久九九九
|
99久久精品国产麻豆
|
久久人人妻人人爽人人爽
|
国产成人久久精品一区二区三区
|
久久99这里只有精品国产
|
久久精品国产欧美日韩
|
久久精品国产亚洲5555
|
久久艹国产
|
色综合久久综精品
|
99久久精品费精品国产
|
久久国产精品波多野结衣AV
|
无码任你躁久久久久久
|
久久99国产精品久久99小说
|
国产一区二区久久久
|
少妇人妻88久久中文字幕
|
国产国产成人精品久久
|