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. JS分支结构与循环结构

    1.分支结构 ①if语句 语法结构 if (/* 条件表达式 */) { // 执行语句 } ​ if (/* 条件表达式 */){ // 成立执行语句 } else { // 否则执行语句 } ​ ...

  2. 【Codeforces Round #589 (Div. 2) D】Complete Tripartite

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 其实这道题感觉有点狗. 思路大概是这样 先让所有的点都在1集合中. 然后随便选一个点x,访问它的出度y 显然tag[y]=2 因为和他相连了嘛 ...

  3. NX二次开发-UFUN单选菜单对话框uc1603

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

  4. bat命令自用其(一)

    每秒钟打印ping命令结果到指定文件: @echo off set /p ip=Input the IP required to monitor: :starts echo %date% %time% ...

  5. vmware压缩磁盘空间的方法, linux怎么卸载vmware

    家里笔记本40G,可用空间怎么挤都只剩7G,从单位copy回来的linux虚拟机要10G,经检查实际使用空间5.7,也就是其他都是空余空间,可以释放掉.(只适合independent.Persiste ...

  6. windows中创建流量转发规则

    #创建流量转发,其中listenaddress为内网ip(也就是ipconfig中所显示的IP),listenport为监听的本机对外端口,后两个分别是需转发的目标主机IP和端口. netsh int ...

  7. 解决ios10及以上Safari双击和双指缩放无法禁止的问题

    移动端web缩放有两种: 1.双击缩放: 2.双指手势缩放. 在iOS 10以前,iOS和Android都可以通过一行meta标签来禁止页面缩放 <meta content="widt ...

  8. 【HDUOJ】1213 How many tables

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意:Ignatius邀请了n个朋友来家里,朋友之间如果互相不认识的不想坐一起,所以至少要准备几 ...

  9. iOS开发系列-应用程序之间跳转

    概述 常见的涉及到应用程序之间的跳转场景,比如社交分享.支付宝.微信支付.链接跳转到应用. 在iOS中应用跳转的本质:打开一个应用只需要拿到对应应用的URL即可. 统一资源定位符 URL(统一资源定位 ...

  10. 什么是Java中的自动拆装箱

    1.基本数据类型 基本类型,或者叫做内置类型,是Java中不同于类(Class)的特殊类型.它们是我们编程中使用最频繁的类型. Java是一种强类型语言,第一次申明变量必须说明数据类型,第一次变量赋值 ...