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

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)的更多相关文章
- Spring 基于Aspectj切面表达式
package com.proc; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; im ...
- Spring 基于 AspectJ 的 AOP 开发
Spring 基于 AspectJ 的 AOP 开发 在 Spring 的 aop 代理方式中, AspectJ 才是主流. 1. AspectJ 简介 AspectJ 是一个基于 java 语言的 ...
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...
- Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探
由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...
- Spring基于AspectJ的AOP的开发——注解
源码:https://gitee.com/kszsa/dchart 一, AspectJ的概述: AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法所以它有一个专 ...
- Spring AOP AspectJ Pointcut 表达式例子
主要来源:http://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with ...
- Spring基于AspectJ的AOP的开发之AOP的相关术语
1. Joinpoint(连接点) -- 所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点(任何一个方法都可以称为连接点) 2. Pointc ...
- 基于@AspectJ配置Spring AOP之一--转
原文地址:http://tech.it168.com/j/2007-08-30/200708302209432.shtml 概述 在低版本Spring中定义一个切面是比较麻烦的,需要实现特定的接口,并 ...
- Spring 3.x企业应用开发实战(11)----基于@AspectJ配置切面
1.@AspectJ的JDK必须是JDK 5.0+ 基于@AspectJ配置切面 @AspectJ采用注解描述切点.增强,两者只是表达式方式不同,效果相同. @AspectJ语法基础-----切点表达 ...
随机推荐
- c++ 在类函数后加const的意义
我们定义的类的成员函数中,常常有一些成员函数不改变类的数据成员,也就是说,这些函数是"只读"函数,而有一些函数要修改类数据成员的值.如果把不改变数据成员的函数都加上const关键字 ...
- 47 ubuntu指令整理学习
0 引言 在使用ubutnu时,积累了大量命令笔记.但是这些笔记比较零散,没有系统性,不便于查找和使用.通过系统性.分门别类地整理,希望可以增强对指令的记忆,提高工作效率,对ubuntu的使用更加地道 ...
- eclipse打包插件net.sf.fjep.fatjar
eclipse打包插件安装 1)将net.sf.fjep.fatjar_0.0.32.jar拷贝到eclipse安装目录中的plugins目录下,然后重启eclipse即可. 软件获取方式: 链接:h ...
- NX二次开发-UFUN创建倒角UF_MODL_create_chamfer
NX9+VS2012 #include <uf.h> #include <uf_modl.h> UF_initialize(); //创建块 UF_FEATURE_SIGN S ...
- NX二次开发-UFUN导入表达式UF_MODL_import_exp
最近在做表达式创建,发现UFUN的创建表达式函数UF_MODL_create_exp,UF_MODL_create_exp_tag没有办法创建字符串类型的表达式,例如AA="BB" ...
- P1831 杠杆数
P1831 杠杆数 题目描述 如果把一个数的某一位当成支点,且左边的数字到这个点的力矩和等于右边的数字到这个点的力矩和,那么这个数就可以被叫成杠杆数. 比如4139就是杠杆数,把3当成支点,我们有这样 ...
- maven项目打成jar包之前需要在pom文件做的准备工作
文章目录 pom.xml 很多时候在eclipse中没问题java -jar没有主类,或者classnotfound pom.xml <build> <plugins> < ...
- Scala 学习2
去map里面的数据 scala> val map = Map("a"->1, "b"->2, "c"->3) map ...
- spring boot thymeleaf简单示例
说实话,用起来很难受,但是人家官方推荐,咱得学 如果打成jar,这个就合适了,jsp需要容器支持 引入依赖 <dependency> <groupId>org.springfr ...
- java.sql.SQLException: validateConnection false
-- :: --- [Create-] com.alibaba.druid.pool.DruidDataSource : create connection error java.sql.SQLExc ...