Spring 基于Aspectj切面表达式
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切面表达式的更多相关文章
- Spring 基于Aspectj切面表达式(6)
1 package com.proc; 2 3 import org.aspectj.lang.JoinPoint; 4 import org.aspectj.lang.ProceedingJoinP ...
- 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语法基础-----切点表达 ...
随机推荐
- SpringMVC集成Swagger插件以及Swagger注解的简单使用
一.简介 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新 .接口的方法,参数和模型 ...
- hdu 1569 最小割
和HDU 1565是一道题,只是数据加强了,貌似轮廓线DP来不了了. #include <cstdio> #include <cstring> #include <que ...
- elasticsearch聚合--桶(Buckets)和指标(Metrics)的概念
写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------主要内容包括: 聚合的两个核 ...
- PHP 登录DEMO
logintest.php 页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...
- [转]Android中的android:layout_width和android:width
android:width 其实是定义控件上面的文本(TextView) 的宽度,当然这个宽度也是和 android:layout_width 配合起来作用的,如果 android:layout_ ...
- UESTC 2015dp专题 A 男神的礼物 区间dp
男神的礼物 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/65 Descri ...
- acdream 1735 输油管道 贪心
输油管道 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acdream.info/problem?pid=1735 Description ...
- maven 指定工程的 jdk 版本及编译级别
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> ...
- BZOJ 1032 JSOI 2007 祖码Zuma 区间DP
题目大意:依照祖玛的玩法(任意选颜色),给出一段区间.问最少用多少个球可以把全部颜色块都消除. 思路:把输入数据依照连续的块处理.保存成颜色和数量.然后用这个来DP.我们知道,一个单独的块须要两个同样 ...
- Ruby:Sublime中开发Ruby需要注意的Encoding事项
背景 最近在用Sublime作为开发环境学习Ruby,本文就记录一下Ruby和Sublime在编码方面的问题. Sublime相关 默认的文件存储编码:UTF8 Sublime文件默认存储编码为UTF ...