欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Spring 基于 Aspect 注解的增強(qiáng)實現(xiàn)

 更新時間:2017年04月27日 08:43:16   作者:多多2017  
本篇文章主要介紹了詳解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)行方法重寫

    這篇文章主要介紹了如何在IDEA中對 hashCode()和 equals() 利用快捷鍵快速進(jìn)行方法重寫,需要的朋友可以參考下
    2020-08-08
  • 數(shù)據(jù)庫CURD必備搭檔mybatis?plus詳解

    數(shù)據(jù)庫CURD必備搭檔mybatis?plus詳解

    這篇文章主要為大家介紹了數(shù)據(jù)庫CURD必備搭檔mybatis?plus詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • RabbitMQ簡單隊列實例及原理解析

    RabbitMQ簡單隊列實例及原理解析

    這篇文章主要介紹了RabbitMQ簡單隊列實例及原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • 關(guān)于@Value注入List,Map及設(shè)置默認(rèn)值問題

    關(guān)于@Value注入List,Map及設(shè)置默認(rèn)值問題

    這篇文章主要介紹了@Value注入List,Map及設(shè)置默認(rèn)值問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • 實戰(zhàn)SpringBoot集成JWT實現(xiàn)token驗證

    實戰(zhàn)SpringBoot集成JWT實現(xiàn)token驗證

    本文詳細(xì)講解了SpringBoot集成JWT實現(xiàn)token驗證,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • SpringCloud降級規(guī)則使用介紹

    SpringCloud降級規(guī)則使用介紹

    這篇文章主要介紹了SpringCloud降級規(guī)則,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • SpringBoot集成Shiro進(jìn)行權(quán)限控制和管理的示例

    SpringBoot集成Shiro進(jìn)行權(quán)限控制和管理的示例

    這篇文章主要介紹了SpringBoot集成Shiro進(jìn)行權(quán)限控制和管理的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • Java實現(xiàn)合并多個升序鏈表

    Java實現(xiàn)合并多個升序鏈表

    本文主要介紹了Java實現(xiàn)合并多個升序鏈表,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Core Java 簡單談?wù)凥ashSet(推薦)

    Core Java 簡單談?wù)凥ashSet(推薦)

    下面小編就為大家?guī)硪黄狢ore Java 簡單談?wù)凥ashSet(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • 基于Spring MVC的文件上傳和下載實現(xiàn)方法

    基于Spring MVC的文件上傳和下載實現(xiàn)方法

    在Web應(yīng)用程序中,文件上傳和下載是常見的功能,Spring MVC框架提供了方便的方式來實現(xiàn)這些功能,本文將介紹如何使用Spring MVC實現(xiàn)文件上傳和下載,需要的朋友可以參考下
    2023-05-05

最新評論