package com.proc;

 import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; @Aspect
@Component
public class LoggingAspect { @Before("execution(* *.*(int,int))")
public void beforeMethod(JoinPoint point){
System.out.println("正在执行方法: "+point.getSignature().getName());
} @After("execution(* *.*(int,int))")
public void afterMethod(JoinPoint point){
System.out.println("方法执行结束: "+point.getSignature().getName());
} @AfterReturning(value="execution(* *.*(int,int))",returning="retVal")
public void afterReturningMethod(JoinPoint point,Object retVal){
System.out.println("方法: "+point.getSignature().getName()+"执行结果为:"+retVal);
} @AfterThrowing(value="execution(* *.*(int,int))",throwing="ex")
public void afterThrowingMethod(JoinPoint point,Exception ex){
System.out.println("执行方法: "+point.getSignature().getName()+"出现了异常:"+ex.getMessage());
} @Around("execution(* *.*(int,int))")
public Object aroundMethod(ProceedingJoinPoint point){ System.out.println("环绕通知: "+point.getSignature().getName());
Object result=null;
//这里相当于前置通知
try {
//执行方法
result= point.proceed();
//这里相当于结果通知
} catch (Throwable e) {
//这里相当于异常通知
e.printStackTrace(); }
//这里相当于后置通知
System.out.println("环绕通知: "+point.getSignature().getName());
return result;
}
}

在对应通知的表单时总要指定execution(* *.*(int,int)),修改也必将麻烦。为了方便我们引入了切面表单时@PointCut。

下面我们来看修改该后的代码

 package com.proc;

 import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Aspect
@Component
public class LoggingAspect { /**定义一个方法,用于声明切面表达式,该方法中什么也不需要。使用是只需要引用该方法名即可*/
@Pointcut("execution(* *.*(..))")
public void declareJoinPointExpression(){} @Before("declareJoinPointExpression()")
public void beforeMethod(JoinPoint point){
System.out.println("正在执行方法: "+point.getSignature().getName());
} @After("declareJoinPointExpression()")
public void afterMethod(JoinPoint point){
System.out.println("方法执行结束: "+point.getSignature().getName());
} @AfterReturning(value="declareJoinPointExpression()",returning="retVal")
public void afterReturningMethod(JoinPoint point,Object retVal){
System.out.println("方法: "+point.getSignature().getName()+"执行结果为:"+retVal);
} @AfterThrowing(value="declareJoinPointExpression()",throwing="ex")
public void afterThrowingMethod(JoinPoint point,Exception ex){
System.out.println("执行方法: "+point.getSignature().getName()+"出现了异常:"+ex.getMessage());
} @Around("declareJoinPointExpression()")
public Object aroundMethod(ProceedingJoinPoint point){ System.out.println("环绕通知: "+point.getSignature().getName());
Object result=null;
//这里相当于前置通知
try {
//执行方法
result= point.proceed();
//这里相当于结果通知
} catch (Throwable e) {
//这里相当于异常通知
e.printStackTrace(); }
//这里相当于后置通知
System.out.println("环绕通知: "+point.getSignature().getName());
return result;
}
}

【注意】:在本类使用切面表单时,只需要引用方法名()即可

      其它本包中的类:类名.方法()

      其它非本包中的类:包名.类名.方法名()

Spring 基于Aspectj切面表达式的更多相关文章

  1. Spring 基于Aspectj切面表达式(6)

    1 package com.proc; 2 3 import org.aspectj.lang.JoinPoint; 4 import org.aspectj.lang.ProceedingJoinP ...

  2. Spring 基于 AspectJ 的 AOP 开发

    Spring 基于 AspectJ 的 AOP 开发 在 Spring 的 aop 代理方式中, AspectJ 才是主流. 1. AspectJ 简介 AspectJ 是一个基于 java 语言的 ...

  3. [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.

    前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...

  4. Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探

    由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...

  5. Spring基于AspectJ的AOP的开发——注解

    源码:https://gitee.com/kszsa/dchart 一, AspectJ的概述: AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法所以它有一个专 ...

  6. Spring AOP AspectJ Pointcut 表达式例子

    主要来源:http://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with ...

  7. Spring基于AspectJ的AOP的开发之AOP的相关术语

    1. Joinpoint(连接点) -- 所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点(任何一个方法都可以称为连接点) 2. Pointc ...

  8. 基于@AspectJ配置Spring AOP之一--转

    原文地址:http://tech.it168.com/j/2007-08-30/200708302209432.shtml 概述 在低版本Spring中定义一个切面是比较麻烦的,需要实现特定的接口,并 ...

  9. Spring 3.x企业应用开发实战(11)----基于@AspectJ配置切面

    1.@AspectJ的JDK必须是JDK 5.0+ 基于@AspectJ配置切面 @AspectJ采用注解描述切点.增强,两者只是表达式方式不同,效果相同. @AspectJ语法基础-----切点表达 ...

随机推荐

  1. Yii单元测试 codeception-Fixture的使用

    Fixture 本文主要是介绍关于Fixture,Specify的使用和遇到的坑 部分是根据源码摸索,有差错的部分望指出 Fixture主要是用来提供测试环境下的数据特定的状态 例如:测试中需要一条待 ...

  2. [转载]for循环的执行顺序

    原文地址:for循环的执行顺序作者:想飞上天的美人鱼   for循环的执行顺序用如下表达式: for(expression1;expression2;expression3) { expression ...

  3. [转]应用服务器ApacheSocketUnixthread

    HttpClient容易忽视的细节——连接关闭 博客分类: java   HttpClient client = new HttpClient(); HttpMethod method = new G ...

  4. Unity UGUI之Button

    创建Button后,会出现一个Image组件和一个Button组件,以及Button子节点Text(可以删除不影响功能) 其中Image的Image Type中有四个选项--Simple.Sliced ...

  5. leetcode76. Minimum Window Substring

    leetcode76. Minimum Window Substring 题意: 给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符. 例如, S ="AD ...

  6. python函数 divmod

    divmod(a,b)函数 中文说明: divmod(a,b)方法返回的是a//b(除法取整)以及a对b的余数 返回结果类型为tuple 参数: a,b可以为数字(包括复数) from 2. Add ...

  7. AS3使用Json 传复杂数据 -------- 用数组而不是向量

    package { public class Person { private var name:String; public function get Name():String { return ...

  8. C++虚函数、虚继承

    http://blog.csdn.net/hackbuteer1/article/details/7883531 转载请标明出处,原文地址:http://blog.csdn.net/hackbutee ...

  9. Ext文本输入框:Ext.form.TextField属性汇总(转) (

    本章介绍Ext.form.TextField组件的基本用法: <form id="form1" runat="server">    <div ...

  10. 利用AWR 查看SQL 执行计划

    在AWR中定位到问题SQL语句后想要了解该SQL statement的具体执行计划,于是就用AWR报告中得到的SQL ID去V$SQL等几个动态性能视图中查询,但发现V$SQL或V$SQL_PLAN视 ...