1 package com.proc;
2
3 import org.aspectj.lang.JoinPoint;
4 import org.aspectj.lang.ProceedingJoinPoint;
5 import org.aspectj.lang.annotation.After;
6 import org.aspectj.lang.annotation.AfterReturning;
7 import org.aspectj.lang.annotation.AfterThrowing;
8 import org.aspectj.lang.annotation.Around;
9 import org.aspectj.lang.annotation.Aspect;
10 import org.aspectj.lang.annotation.Before;
11 import org.springframework.stereotype.Component;
12
13 @Aspect
14 @Component
15 public class LoggingAspect {
16
17 @Before("execution(* *.*(int,int))")
18 public void beforeMethod(JoinPoint point){
19 System.out.println("正在执行方法: "+point.getSignature().getName());
20 }
21
22 @After("execution(* *.*(int,int))")
23 public void afterMethod(JoinPoint point){
24 System.out.println("方法执行结束: "+point.getSignature().getName());
25 }
26
27 @AfterReturning(value="execution(* *.*(int,int))",returning="retVal")
28 public void afterReturningMethod(JoinPoint point,Object retVal){
29 System.out.println("方法: "+point.getSignature().getName()+"执行结果为:"+retVal);
30 }
31
32 @AfterThrowing(value="execution(* *.*(int,int))",throwing="ex")
33 public void afterThrowingMethod(JoinPoint point,Exception ex){
34 System.out.println("执行方法: "+point.getSignature().getName()+"出现了异常:"+ex.getMessage());
35 }
36
37 @Around("execution(* *.*(int,int))")
38 public Object aroundMethod(ProceedingJoinPoint point){
39
40 System.out.println("环绕通知: "+point.getSignature().getName());
41 Object result=null;
42 //这里相当于前置通知
43 try {
44 //执行方法
45 result= point.proceed();
46 //这里相当于结果通知
47 } catch (Throwable e) {
48 //这里相当于异常通知
49 e.printStackTrace();
50
51 }
52 //这里相当于后置通知
53 System.out.println("环绕通知: "+point.getSignature().getName());
54 return result;
55 }
56 }

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

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

 1 package com.proc;
2
3 import org.aspectj.lang.JoinPoint;
4 import org.aspectj.lang.ProceedingJoinPoint;
5 import org.aspectj.lang.annotation.After;
6 import org.aspectj.lang.annotation.AfterReturning;
7 import org.aspectj.lang.annotation.AfterThrowing;
8 import org.aspectj.lang.annotation.Around;
9 import org.aspectj.lang.annotation.Aspect;
10 import org.aspectj.lang.annotation.Before;
11 import org.aspectj.lang.annotation.Pointcut;
12 import org.springframework.stereotype.Component;
13
14 @Aspect
15 @Component
16 public class LoggingAspect {
17
18 /**定义一个方法,用于声明切面表达式,该方法中什么也不需要。使用是只需要引用该方法名即可*/
19 @Pointcut("execution(* *.*(..))")
20 public void declareJoinPointExpression(){}
21
22 @Before("declareJoinPointExpression()")
23 public void beforeMethod(JoinPoint point){
24 System.out.println("正在执行方法: "+point.getSignature().getName());
25 }
26
27 @After("declareJoinPointExpression()")
28 public void afterMethod(JoinPoint point){
29 System.out.println("方法执行结束: "+point.getSignature().getName());
30 }
31
32 @AfterReturning(value="declareJoinPointExpression()",returning="retVal")
33 public void afterReturningMethod(JoinPoint point,Object retVal){
34 System.out.println("方法: "+point.getSignature().getName()+"执行结果为:"+retVal);
35 }
36
37 @AfterThrowing(value="declareJoinPointExpression()",throwing="ex")
38 public void afterThrowingMethod(JoinPoint point,Exception ex){
39 System.out.println("执行方法: "+point.getSignature().getName()+"出现了异常:"+ex.getMessage());
40 }
41
42 @Around("declareJoinPointExpression()")
43 public Object aroundMethod(ProceedingJoinPoint point){
44
45 System.out.println("环绕通知: "+point.getSignature().getName());
46 Object result=null;
47 //这里相当于前置通知
48 try {
49 //执行方法
50 result= point.proceed();
51 //这里相当于结果通知
52 } catch (Throwable e) {
53 //这里相当于异常通知
54 e.printStackTrace();
55
56 }
57 //这里相当于后置通知
58 System.out.println("环绕通知: "+point.getSignature().getName());
59 return result;
60 }
61 }

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

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

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

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

  1. Spring 基于Aspectj切面表达式

    package com.proc; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; im ...

  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. nextJS使用注意事项

    项目参考 nextJs-yicha 1. 采用方案 create-next-app.antd (1)安装 npx create-next-app --example with-ant-design m ...

  2. NX二次开发-UFUN文件选择对话框UF_UI_create_filebox

    NX11+VS2013 #include <uf.h> #include <uf_ui.h> UF_initialize(); //文件选择对话框 char sPromptSt ...

  3. NX二次开发-NXOpen窗口打印NXMessageBox&ListingWindow

    NX9+VS2012 #include <NXOpen/NXObject.hxx> #include <NXOpen/Part.hxx> #include <NXOpen ...

  4. Funcation:Object

    ylbtech-Funcation:Object 1.返回顶部 1. public object GetMyRelation() { var list = new List<object> ...

  5. C#中如何实现将字符串首尾的空格去掉,如果字符串中间还有连续空格的话,仅保留一个空格

    思路:用空来替换首尾的空格,用一个空格替换中间的连续空格. 例如:string inputStr=” xx xx “; inputStr=inputStr.Trim(); inputStr=Regex ...

  6. sql 递归显示所有父节点

    1.我先建两个表 一个表示项目及级别 另一个表示项目最后一级中包含内容.两个表的数据如图 CREATE TABLE [dbo].[yq_Project]( ,) primary key, ) NOT ...

  7. 控制音量大小widget

    由于手机音量按键非常悲剧的掉了.无法控制手机音量大小.使用起来非常不方便.所以决定写一个小widget放在桌面能够随时控制音量吧.也算是解决一点便利问题. 1.一个简单的widget 由于我的需求非常 ...

  8. ionic js 加载动画 ionSpinner 提供了许多种旋转加载的动画图标。当你的界面加载时,你就可以呈现给用户相应的加载图标。 该图标采用的是SVG

    ionic 加载动画 ion-spinner ionSpinner 提供了许多种旋转加载的动画图标.当你的界面加载时,你就可以呈现给用户相应的加载图标. 该图标采用的是SVG. 用法 <ion- ...

  9. SQL 顺序

    查询语句中select from where group by having order by的执行顺序 查询语句中select from where group by having order by ...

  10. 2000w数据,redis中只存20w的数据,如何保证redis中的数据都是热点数据

    redis 内存数据集大小上升到一定大小的时候,就会施行数据淘汰策略.redis 提供 6种数据淘汰策略: voltile-lru:从已设置过期时间的数据集(server.db[i].expires) ...