詳解Spring 基于 Aspect 注解的增強(qiáng)實現(xiàn)
整理文檔,搜刮出一個Spring 基于 Aspect 注解的增強(qiáng)實現(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 ....................... "); } }
定義基于注解的增強(qiáng)類
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 { //定義切點(diǎn) @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 增強(qiáng)自動代理 --> <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)文章
如何在IDEA中對 hashCode()和 equals() 利用快捷鍵快速進(jìn)行方法重寫
這篇文章主要介紹了如何在IDEA中對 hashCode()和 equals() 利用快捷鍵快速進(jìn)行方法重寫,需要的朋友可以參考下2020-08-08數(shù)據(jù)庫CURD必備搭檔mybatis?plus詳解
這篇文章主要為大家介紹了數(shù)據(jù)庫CURD必備搭檔mybatis?plus詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05關(guān)于@Value注入List,Map及設(shè)置默認(rèn)值問題
這篇文章主要介紹了@Value注入List,Map及設(shè)置默認(rèn)值問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05實戰(zhàn)SpringBoot集成JWT實現(xiàn)token驗證
本文詳細(xì)講解了SpringBoot集成JWT實現(xiàn)token驗證,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12SpringBoot集成Shiro進(jìn)行權(quán)限控制和管理的示例
這篇文章主要介紹了SpringBoot集成Shiro進(jìn)行權(quán)限控制和管理的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03基于Spring MVC的文件上傳和下載實現(xiàn)方法
在Web應(yīng)用程序中,文件上傳和下載是常見的功能,Spring MVC框架提供了方便的方式來實現(xiàn)這些功能,本文將介紹如何使用Spring MVC實現(xiàn)文件上傳和下載,需要的朋友可以參考下2023-05-05