Spring AOP (二)
来源:互联网 发布:吴大伟淘宝店铺号 编辑:程序博客网 时间:2024/04/27 02:24
在Spring AOP初尝试里面,每个切面方法前面的切面都是一样的,此处可以将它们提出处理,用一个统一的空实现的方法来表示。
将上一篇的例子修改之后,如下。
package com;
import java.util.Arrays;
import java.util.List;
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.core.annotation.Order;
import org.springframework.stereotype.Component;
// Order 在多个切面的情况下,用来指定切面的优先级,数值越小,优先级越高
@Order(1)
@Aspect
@Component
public class ArithLoggingAspect {
// 此处方法一定要是空实现
// 类在同一个包下面的话,可以不用全类名
@Pointcut("execution(* Calculator.*(..))")
public void declarePointcut() {}
@Before("declarePointcut()")
public void beforeMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method " + methodName + " begins with " + args);
}
@After("declarePointcut()")
public void afterMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " ends");
}
// 此处returning="rtv"中的rtv与方法的参数Object rtv要名称一致
@AfterReturning(value="declarePointcut()",returning="rtv")
public void afterReturn(JoinPoint joinPoint, Object rtv) {
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " return " + rtv);
}
// 此处throwing="e"指定了Exception e参数变量名
@AfterThrowing(value="declarePointcut()",throwing="e")
public void afterException(JoinPoint joinPoint,Exception e) {
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " exception ");
e.printStackTrace();
}
// @Around必须有返回值,而且返回值必须是proceed的返回值
@Around("declarePointcut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
List<Object> args = Arrays.asList(pjp.getArgs());
System.out.println("around " + args);
return pjp.proceed();
}
}
PS:如果是要引用其他类里面的切面声明,就在declarePointcut()前面加上类的全类名
- Spring AOP (二)
- Spring AOP (二)
- Spring AOP详解(二)
- spring随笔(二) AOP
- spring初学二(AOP)
- Spring IOC AOP (二)
- Spring AOP初探(二)
- Spring AOP 基础(二)
- Spring之AOP(二)
- Spring(二)Spring AOP配置详解
- [Spring]一步步实现Spring AOP(二)
- Spring(二)Spring AOP配置详解
- 探析Spring AOP(二):Spring AOP的实现机制
- Spring AOP(二)(Spring中基于AOP的@AspectJ)
- Spring:AOP(二)编程方式实现AOP
- 自己实现Spring AOP(二)JDK代理实现AOP
- AOP学习笔记(二)——Spring AOP
- Spring 学习笔记 (二)--AOP
- [matlab/C++混合编程时错误] 值“2”不匹配值“0”
- 函数IsValid()
- 个推技术实现原理介绍
- HTTP中GET与POST的区别
- README
- Spring AOP (二)
- 【阅读】文章
- spring aop的一个问题
- 计算Android App占用的各种空间大小
- 代理设计模式[备份]
- django column缺失
- java实现权重随机算法
- C函数
- IT人员的职业生涯规划