隨筆:15 文章:206 評(píng)論:35 引用:0
fenglin
創(chuàng)新、創(chuàng)意、挑戰(zhàn)
C++博客
首頁(yè)
發(fā)新隨筆
發(fā)新文章
聯(lián)系
聚合
管理
Spring3_動(dòng)態(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
//
此實(shí)現(xiàn)方法對(duì)象必須由Spring容器來初始化才能實(shí)現(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)所有方法進(jìn)行代理
//
其它注解請(qǐng)查Spring文檔
@Before(
"
myMethod()
"
)
public
void
before()
{
System.out.println(
"
Log start
"
);
}
@Pointcut(
"
within(com.bebig.service..*)
"
)
//
代理沒有實(shí)現(xiàn)接口的對(duì)象時(shí),是由CGLib直接生成二進(jìn)制文件的,因此需要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"
/>
<!--
動(dòng)態(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對(duì)象來創(chuàng)建代理對(duì)象
UserService service
=
(UserService) cxt.getBean(
"
userService
"
);
service.add(
new
User());
service.deleteById(
1
);
}
}
發(fā)表于 2010-10-19 17:13
風(fēng)林
閱讀(623)
評(píng)論(0)
編輯
收藏
引用
所屬分類:
JAVA
、
Spring
只有注冊(cè)用戶
登錄
后才能發(fā)表評(píng)論。
【推薦】100%開源!大型工業(yè)跨平臺(tái)軟件C++源碼提供,建模,組態(tài)!
相關(guān)文章:
[轉(zhuǎn)]java編程中'為了性能'一些盡量做到的地方
JAVAC命令行錯(cuò)誤
Java基礎(chǔ)_Collection接口下的子類存儲(chǔ)特性
Android_常用控件使用
Android_Activity&Intent&Layout
Integer自動(dòng)裝箱、拆箱問題
自定義比較器、排序
JDBC操作實(shí)例
常用數(shù)據(jù)庫(kù)的JDBC連接代碼
[轉(zhuǎn)]J2SE_內(nèi)部類
網(wǎng)站導(dǎo)航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
CALENDER
<
2025年7月
>
日
一
二
三
四
五
六
29
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
常用鏈接
我的隨筆
我的評(píng)論
我參與的隨筆
留言簿
給我留言
查看公開留言
查看私人留言
隨筆分類
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)
相冊(cè)
CSS
Hibernate
搜索
最新評(píng)論
1.?re: Struts2_三種傳參數(shù)方法
方式的發(fā)生
--阿飛史蒂夫
2.?re: 在Win7上搭建JSP開發(fā)環(huán)境
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--鄒
3.?re: ckeditor&ckfinder&s2sh集成
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--庸幾何
4.?re: 在Win7上搭建JSP開發(fā)環(huán)境
下個(gè) myeclipse@lou
--孫毅
5.?re: 在Win7上搭建JSP開發(fā)環(huán)境
@lou
運(yùn)行 -cmd 找到startup.bat 在java 環(huán)境中運(yùn)行
--孫毅
閱讀排行榜
1.?開始找Java開發(fā)類工作了(510)
2.?給一家公司的軟件做加密方案(444)
3.?一流、二流、三流(422)
4.?周一到周五都得出差,周末才能回(420)
5.?從WIN32開發(fā)轉(zhuǎn)型到WEB開發(fā)(381)
評(píng)論排行榜
1.?周一到周五都得出差,周末才能回(2)
2.?給力2011(2)
3.?最近工作有點(diǎn)忙(0)
4.?生活(0)
5.?在博客園開博了(0)
Powered By:
博客園
模板提供
:
滬江博客
久久一区二区三区99
|
国产精品丝袜久久久久久不卡
|
久久久久国产精品三级网
|
99久久综合国产精品二区
|
久久久久亚洲AV无码专区桃色
|
久久午夜无码鲁丝片秋霞
|
久久精品亚洲中文字幕无码麻豆
|
很黄很污的网站久久mimi色
|
青青久久精品国产免费看
|
久久精品国产久精国产果冻传媒
|
国产精品免费久久久久电影网
|
久久露脸国产精品
|
亚洲精品NV久久久久久久久久
|
亚洲中文字幕无码久久2020
|
国内精品久久久久久99
|
久久久艹
|
久久99精品国产自在现线小黄鸭
|
久久久人妻精品无码一区
|
国产成人久久精品一区二区三区
|
伊人久久综在合线亚洲2019
|
亚洲国产精品18久久久久久
|
久久99精品国产麻豆蜜芽
|
国产精品美女久久久久
|
久久久久久精品久久久久
|
亚洲国产精品久久久久
|
99久久香蕉国产线看观香
|
香港aa三级久久三级
|
久久久久亚洲AV片无码下载蜜桃
|
久久综合九色综合欧美就去吻
|
99久久免费国产精精品
|
亚洲av伊人久久综合密臀性色
|
久久精品亚洲福利
|
久久综合视频网站
|
久久久久久毛片免费看
|
久久精品国产精品国产精品污
|
精品久久久噜噜噜久久久
|
一个色综合久久
|
一本色道久久综合狠狠躁篇
|
三级韩国一区久久二区综合
|
日本欧美国产精品第一页久久
|
久久精品一本到99热免费
|