詳解Spring 基于 Aspect 注解的增強實現(xiàn)
整理文檔,搜刮出一個Spring 基于 Aspect 注解的增強實現(xiàn)的代碼,稍微整理精簡一下做下分享
定義基本實體類
package com.advice;
/**
* @author Duoduo
* @version 1.0
* @date 2017/4/25 23:41
*/
public class Performer {
public void doPerform() {
System.out.println("Performer do perform ....................... ");
}
}
定義基于注解的增強類
package com.advice;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
/**
* @author Duoduo
* @version 1.0
* @date 2017/4/25 23:42
*/
@Aspect//定義切面
public class Audience {
//定義切點
@Pointcut("execution(* com.advice.Performer.doPerform(..))")
public void doPerform(){}
@Before("doPerform()")
public void takeSeas() {
System.out.println("The audience is taking their seats.");
}
@Before("doPerform()")
public void turnOffPhone() {
System.out.println("The audience is turn off their cellphone.");
}
@AfterReturning("doPerform()")
public void applaund() {
System.out.println("CLAP CLAP CLAP CLAP ...");
}
@AfterThrowing("doPerform()")
public void demandRefund() {
System.out.println("Boo! we want our money back!");
}
@Around("doPerform()")
public void watchPerfomance(ProceedingJoinPoint joinPoint) {
try {
Long start = System.currentTimeMillis();
joinPoint.proceed();
long end = System.currentTimeMillis();
System.out.println("The performance took "+(end-start)+" milliseconds");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
Spring 自動代理配置
<!-- aop 增強自動代理 --> <aop:aspectj-autoproxy/> <bean id="audience" class="com.advice.Audience"/> <bean id="performer" class="com.advice.Performer"/>
Junit測試
@Test
public void testDoPerform() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:smart-context.xml");
//代理為指向Interface的代理
Performer performer = (Performer) context.getBean("performer");
System.out.println("+++++++++++++++++++++++++++++++++");
performer.doPerform();
}
測試結(jié)果
+++++++++++++++++++++++++++++++++ 2017-04-26 20:51:16,980 DEBUG [main] (AbstractBeanFactory.java:251) - Returning cached instance of singleton bean 'audience' The audience is taking their seats. The audience is turn off their cellphone. Performer do perform ....................... The performance took 91 milliseconds CLAP CLAP CLAP CLAP ...
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot整合企業(yè)微信機器人助手推送消息的實現(xiàn)
本文主要介紹了Springboot整合企業(yè)微信機器人助手推送消息的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
解決myBatis中openSession()自動提交的問題
在學(xué)習(xí)MySQL過程中,發(fā)現(xiàn)插入操作自動提交,問題原因可能是myBatis中的openSession()方法設(shè)置了自動提交,或者是MySQL的默認(rèn)引擎設(shè)置為不支持事務(wù)的MyISAM,解決辦法包括更改myBatis的提交設(shè)置或?qū)ySQL表的引擎改為InnoDB2024-09-09
SpringBoot+Vue靜態(tài)資源刷新后無法訪問的問題解決方案
這篇文章主要介紹了SpringBoot+Vue靜態(tài)資源刷新后無法訪問的問題解決方案,文中通過代碼示例和圖文講解的非常詳細(xì),對大家解決問題有一定的幫助,需要的朋友可以參考下2024-05-05
如何在Java中使用標(biāo)準(zhǔn)庫創(chuàng)建臨時文件
有時候我們程序運行時需要產(chǎn)生中間文件,但是這些文件只是臨時用途,并不做長久保存,我們可以使用臨時文件,不需要長久保存,這篇文章主要給大家介紹了關(guān)于如何在Java中使用標(biāo)準(zhǔn)庫創(chuàng)建臨時文件的相關(guān)資料,需要的朋友可以參考下2023-10-10
Spring整合TimerTask實現(xiàn)定時任務(wù)調(diào)度
這篇文章主要介紹了Spring整合TimerTask實現(xiàn)定時任務(wù)調(diào)度的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
java wait()/notify() 實現(xiàn)生產(chǎn)者消費者模式詳解
這篇文章主要介紹了java wait()/notify() 實現(xiàn)生產(chǎn)者消費者模式詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
Java使用釘釘創(chuàng)建企業(yè)內(nèi)部機器人的實現(xiàn)
釘釘?shù)某霈F(xiàn)為企業(yè)節(jié)約了大量人力成本,其中也開放了大量的外部api接口方便企業(yè)使用。本文主要介紹了Java使用釘釘創(chuàng)建企業(yè)內(nèi)部機器人的實現(xiàn),感興趣的可以了解一下2021-11-11

