详解Spring 基于 Aspect 注解的增强实现

 更新时间:2017年04月27日 08:43:16   作者:多多2017  
本篇文章主要介绍了详解Spring 基于 Aspect 注解的增强实现,非常具有实用价值,需要的朋友可以参考下

整理文档,搜刮出一个Spring 基于 Aspect 注解的增强实现的代码,稍微整理精简一下做下分享

定义基本实体类

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();
  }

测试结果

+++++++++++++++++++++++++++++++++
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 ...

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Springboot中集成Swagger2框架的方法

    Springboot中集成Swagger2框架的方法

    这篇文章主要介绍了Springboot中集成Swagger2框架的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-12-12
  • Java数据结构之对象比较详解

    Java数据结构之对象比较详解

    这篇文章主要为大家详细介绍了Java中对象的比较、集合框架中PriorityQueue的比较方式以及PriorityQueue的模拟实现,感兴趣的可以了解一下
    2022-07-07
  • springmvc如何使用POJO作为参数

    springmvc如何使用POJO作为参数

    这篇文章主要介绍了springmvc如何使用POJO作为参数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01
  • 代码分析Java中线程的等待与唤醒

    代码分析Java中线程的等待与唤醒

    本篇文章给大家分享了关于Java中线程的等待与唤醒的知识点内容,有需要的朋友们可以学习下。
    2018-10-10
  • SpringMVC结合ajaxfileupload.js实现文件无刷新上传

    SpringMVC结合ajaxfileupload.js实现文件无刷新上传

    这篇文章主要介绍了SpringMVC结合ajaxfileupload.js实现文件无刷新上传,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • 深入了解Java中的反射机制(reflect)

    深入了解Java中的反射机制(reflect)

    Java的反射机制允许我们对一个类的加载、实例化、调用方法、操作属性的时期改为在运行期进行,这大大提高了代码的灵活度,本文就来简单讲讲反射机制的具体使用方法吧
    2023-05-05
  • Java中操作数组的Arrays类

    Java中操作数组的Arrays类

    大家好,本篇文章主要讲的是Java中操作数组的Arrays类,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-02-02
  • SpringBoot+MyBatis-Plus实现分页功能

    SpringBoot+MyBatis-Plus实现分页功能

    在SpringBoot项目中,结合MyBatis-Plus(简称MP)可以非常方便地实现分页功能,MP为开发者提供了分页插件PaginationInterceptor,只需简单配置即可使用,本文给大家介绍了SpringBoot+MyBatis-Plus实现分页功能,文中通过代码示例给大家介绍的非常详细,需要的朋友可以参考下
    2024-01-01
  • 解析阿里一面CyclicBarrier和CountDownLatch的区别

    解析阿里一面CyclicBarrier和CountDownLatch的区别

    这篇文章主要介绍了阿里一面CyclicBarrier和CountDownLatch的区别是啥,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • Java中Arrays.sort自定义一维数组、二维数组的排序方式

    Java中Arrays.sort自定义一维数组、二维数组的排序方式

    这篇文章主要介绍了Java中Arrays.sort自定义一维数组、二维数组的排序方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08

最新评论