1、切点定义

切点定义包含两个部分

一个切入点表达式

一个包含名字和任意参数的方法签名

package com.sysker.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut; @Aspect
public class CreatePointcut {
@Pointcut("execution(* transfer(..))")
private void anyOldTransfer() { }
}
  • 在@AspectJ风格的AOP中,切入点签名采用一个普通的方法定义(方法体通常为空)来提供,且该方法的返回值必须是void;切入点表达式需要使用@Point注解来标注,如上代码
  • 切入点表达式,也就是@Pointcut 注解的值,是正规的AspectJ 切入点表达式
  • 如果需要使用本切面类中的切入点,则可以在使用@Before、@After、@Around等注解定义Advice时,使用pointcut或value属性值引用已有的切入点
package com.sysker.aspect;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut; @Aspect
public class CreatePointcut {
@Pointcut("execution(* transfer(..))")
private void anyOldTransfer() {} @Pointcut("execution(* transfer(..))")
public void myPointcut() {} @AfterReturning(pointcut = "mypointcut()", returning = "retVal")
public void writeLog(String msg, Object retVal) { }
}
  • private修饰的切点,其他切面类不能使用,而且在引用其他切面类中的切点时,必须添加类名前缀
    @After(value="CreatePointcut.myPointcut()")
public void show() {
System.out.println("myPointcut模拟释放资源");
}

2、切入点指示符

2.1 execution:匹配执行方法的连接点,用法相对复杂,表达式如下:

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
  • modifiers-pattern:指定方法的修饰符,支持通配,该部分可省略;
  • ret-type-pattern:指定方法的返回值类型,支持通配符,可以使用“*”通配符来匹配所有的返回值类型
  • declaring-type-pattern:指定方法所属的类,支持通配符,该部分可省略;
  • name-pattern:指定匹配指定的方法名,支持通配,可以使用“*”通配符来匹配所有方法
  • parapn-pattern:指定方法声明中的形参列表,支持两个通配符,即“*”和“..”,其中“*”代表一个任意类型的参数,而(..)代表零个或多个任意类型的参数
  • throws-pattern:指定方法声明抛出的异常,支持通配符,该部分可省略

2.2 within:用于限定匹配特定类型的连接点,当使用Spring AOP的时候,只能匹配方法执行的连接点

// 在com.sysker.service包中的任意连接点(在Spring AOP中只是方法执行的连接点)
within(com.sysker.service.*)
// 在com.sysker.service包或其子包中的任意连接点(在Spring AOP中只是方法执行的连接点)
within(com.sysker.service..*)

2.3 this:用于限定AOP代理必须指定类型的实例,匹配该对象的所有连接点,当使用Spring AOP的时候,只能匹配方法执行的连接点

// 匹配实现了 com.sysker.service.AccountService接口的AOP代理的所有连接点
// 在SpringAOP中只是方法执行的连接点
this(com.sysker.service.AccountService)

2.4 target:用于对连接点的参数类型进行限制,要求参数类型是指定类型的实例,当使用Spring AOP的时候,只能匹配方法执行的连接点

// 匹配实现了 com.sysker.service.AccountService.AccountService 接口的目标对象的所有连接点
// 在Spring AOP中只是方法执行的连接点
target(com.sysker.service.AccountService.AccountService)

2.5 args:用于对连接点的参数类型进行限制,要求参数类型是指定类型的实例。当使用Spring AOP的时候,只能匹配方法执行的连接点。

// 匹配只接受一个参数,且传入的参数类型是serializable
// 在Spring AOP中只是方法执行的连接点
target(com.sysker.service.AccountService.AccountService)

2.6 bean:用于限定只匹配指定bean实例内的连接点,实际上只能使用方法执行行为作为连接点

  • 定义bean表达式时需要传入bean的id或name,表示只匹配该bean实例内的连接点,支持使用“*”通配符
// 匹配tradeService Bean实例内方法执行的连接点
bean(tradeService)
// 匹配名字以service结尾的Bean实例内方法执行的连接点
bean(*Service)

3 组合切入点表达式

&& :要求连接点同时匹配两个切入点表达式

|| :只要连接点匹配任意一个切入点表达式

! : 要求连接点不匹配指定的切入点表达式

AOP切点相关的更多相关文章

  1. 如何通过自定义注解实现AOP切点定义

    面向切面编程(Aspect Oriented Programming, AOP)是面向对象编程(Object Oriented Programming,OOP)的强大补充,通过横切面注入的方式引入其他 ...

  2. springboot+aop切点记录请求和响应信息

    本篇主要分享的是springboot中结合aop方式来记录请求参数和响应的数据信息:这里主要讲解两种切入点方式,一种方法切入,一种注解切入:首先创建个springboot测试工程并通过maven添加如 ...

  3. Spring AOP 切点(pointcut)表达式

    这遍文章将介绍Spring AOP切点表达式(下称表达式)语言,首先介绍两个面向切面编程中使用到的术语. 连接点(Joint Point):广义上来讲,方法.异常处理块.字段这些程序调用过程中可以抽像 ...

  4. Spring AOP切点表达式用法总结

    1. 简介        面向对象编程,也称为OOP(即Object Oriented Programming)最大的优点在于能够将业务模块进行封装,从而达到功能复用的目的.通过面向对象编程,不同的模 ...

  5. AOP切点表达式

    Aspectj切入点语法定义 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义切入点表达式  execu ...

  6. Spring Aop切点

    切点用于准确定位应该在什么地方应用切面的通知.通知和切点是切面的最基本的元素.在Spring AOP中要使用AspectJ的切点表达式来定义切点.下面我们列出Spring AOP所支持的AspectJ ...

  7. Spring系列25:Spring AOP 切点详解

    本文内容 Spring 10种切点表达式详解 切点的组合使用 公共切点的定义 声明切点@Poincut @Poincut 的使用格式如下: @Poincut("PCD") // 切 ...

  8. 使用注解匹配Spring Aop切点表达式

    Spring中的类基本都会标注解,所以使用注解匹配切点可以满足绝大部分需求 主要使用@within()/@target @annotaton() @args()等... 匹配@Service类中的所有 ...

  9. Spring基于AspectJ的AOP的开发之AOP的相关术语

    1. Joinpoint(连接点) -- 所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点(任何一个方法都可以称为连接点) 2. Pointc ...

随机推荐

  1. datepicker

    准备工作 首先请到jqueryui.com官网下载datepicker插件代码,注意官网提供了整个jquery ui的所有插件下载,但是您可以选择其中几个用到的插件下载,本文中只用到datepicke ...

  2. uboot中的TEXT_BASE

    转载:http://blog.csdn.net/xxblinux/article/details/6281295 我们都知道U-BOOT分为两个阶段,第一阶段是(~/cpu/arm920t/start ...

  3. Swing编程练习。可能这篇会有错误哦

    总结:21岁的思思是华为的初级女java工程师,我等女流怎么办呢? Swing.图形用户界面的编程,panel起了很大作用 package com.da; import java.awt.Color; ...

  4. spring容器启动的三种方式

    一.在Web项目中,启动Spring容器的方式有三种,ContextLoaderListener.ContextLoadServlet.ContextLoaderPlugin. 1.1.监听器方式: ...

  5. Spark Tungsten in-heap / off-heap 内存管理机制--待整理

    一:Tungsten中到底什么是Page? 1. 在Spark其实不存在Page这个类的.Page是一种数据结构(类似于Stack,List等),从OS层面上讲,Page代表了一个内存块,在Page里 ...

  6. 管理react路由的history对象的插件history的使用介绍

    本文介绍如何使用history插件管理浏览记录 history插件的使用 history这个插件可以方便管理你的浏览记录 cnpm install history --save import crea ...

  7. webscheduler 开源定时服务和延迟服务

    源码地址:https://gitee.com/eabeat/webscheduler 架构上采用 asp.net + access ,实现简单的管理界面,可以维护调用API,查看日志等功能.内核采用Q ...

  8. web.config中namespace的配置(针对页面中引用)

    1,在页面中使用强类型时: @model GZUAboutModel @using Nop.Admin.Models//命名空间(注意以下) 2,可以将命名空间提到web.config配置文件中去,此 ...

  9. DAY17-Django之logging

    LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'standard': { 'format': ...

  10. js实现鼠标拖拽

    主要原理: 1.当鼠标按下时,记录鼠标坐标,用到的是 onmousedown: 2.当鼠标移动时,计算鼠标移动的坐标之差,用到的是 onmousemove: 3.当鼠标松开时,清除事件,用到的是 on ...