@Aspect
@Order(value=3)
public class OperateInterceptor{
protected Log log = LogFactory.getLog(getClass());
- /**
- * 定义切入点
- * 第一个*表示方法的返回值,这里使用通配符,只有返回值符合条件的才拦截
- * 第一个..表示com.orient.crm.service包及其子包
- * 倒数第二个*表示包下的所有Java类
- * .update表示update方法(所有方法用*表示)
- * (..)表示方法的参数可以任意多个
- */
//当数据更新后执行
public void updatePointcut(JoinPoint joinPoint) throws UnsupportedEncodingException {
//业务代码
}
@After("execution(* com.orient.crm.*.service..*.save(..))")
public void savePointcut(JoinPoint joinPoint) throws UnsupportedEncodingException {
//业务代码
}
//当数据删除后执行
public void deletePointcut(JoinPoint joinPoint) {
log.info("log delete info begin...");
//业务代码
}
说明
1.pointcut语法
execution ——for matching method execution join points
within —— 指定连接点所在的Java类型。
this ——bean reference (Spring AOP proxy) is an instance of the given type
args —— 指定传入到连接点的参数
@target —— the class of the executing object has an annotation of the given type
@args —— the runtime type of the actual arguments passed have annotations of the given type(s)
@within —— limits matching to join points within types that have the given annotation
@annotation —— the subject of the join point (method being executed in Spring AOP) has the given annotation
组合pointcut表达式
可以使用 && 、|| 、! 组合pointcut表达式。
execution格式
execution(
可见性(可选) —— 使用public、protected、private指定可见性。也可以为*,表示任意可见性
返回值类型(必须) —— 指定返回值类型
声明类型(可选)——
方法名(参数模式)(必须) —— 方法名称,和方法接收的参数
异常模式(可选) —— 指定方法签名中是否存在异常类型
)
在pointcut表达式中可以使用 * 、.. 、 +等通配符。
* :表示若干字符(排除 . 在外)
.. :表示若干字符(包括 . 在内)
+ :表示子类 ,比如Info+ 表示Info类及其子类