Spring AOP AspectJ Pointcut Expressions With Examples

原文地址:http://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with-examples/

1) Matching Method Signature Patterns

The most typical pointcut expressions are used to match a number of methods by their signatures.

Matching all methods within a class in another package

For example, the following pointcut expression matches all of the methods declared in the EmployeeManagerinterface. The preceding wildcard matches methods with any modifier (public, protected, and private) and any return type. The two dots in the argument list match any number of arguments.

execution(* com.howtodoinjava.EmployeeManager.*(..))

Matching all methods within a class within same package

You can omit the package name if the target class or interface is located in the same package as this aspect.

execution(* EmployeeManager.*(..))

Matching all public methods in EmployeeManager

Use public keyword in start, and use * to match any return type.

execution(public * EmployeeManager.*(..))

Matching all public methods in EmployeeManager with return type EmployeeDTO

Use public keyword and return type in start.

execution(public EmployeeDTO EmployeeManager.*(..))

Matching all public methods in EmployeeManager with return type EmployeeDTO and first parameter as EmployeeDTO

Use public keyword and return type in start. Also, specify your first parameter as well. Rest parameters can be matched through two dots.

execution(public EmployeeDTO EmployeeManager.*(EmployeeDTO, ..))

Matching all public methods in EmployeeManager with return type EmployeeDTO and definite parameters

Use public keyword and return type in start. Also, specify all parameter types as well.

execution(public EmployeeDTO EmployeeManager.*(EmployeeDTO, Integer))

2) Matching Type Signature Patterns

When applied to Spring AOP, the scope of these pointcuts will be narrowed to matching all method executions within the certain types only.

Matching all methods defined in classes inside package com.howtodoinjava

It’s much like previous example.

within(com.howtodoinjava.*)

Matching all methods defined in classes inside package com.howtodoinjava and classes inside all sub-packages as well

For including, sub-packages use two dots.

within(com.howtodoinjava..*)

Match all methods with a class in another package

Much like previous example using execution keyword.

within(com.howtodoinjava.EmployeeManagerImpl)

Match all methods with a class in same package

In case of same package, drop package name.

within(EmployeeManagerImpl)

Match all methods within all all implementing classes of EmployeeManager interface

Use + (plus) sign to match all implementations of an interface.

within(EmployeeManagerImpl+)

3) Matching Bean Name Patterns

You can match all beans as well having a common naming pattern e.g.

Match all methods defined in beans whose name ends with ‘Manager’.

It’s quite easy one. Use an * to match anything preceding in bean name and then matching word.

bean(*Manager)

4) Combining Pointcut Expressions

In AspectJ, pointcut expressions can be combined with the operators && (and), || (or), and ! (not). e.g.

Match all methods with names ending with Manager and DAO

Use ‘||’ sign to combine both expressions.

bean(*Manager) || bean(*DAO)

I hope that above information will help you when you face any difficulty in determining the correct pointcut expression in your application.

Happy Learning !!

时间: 2024-10-10 15:29:43

Spring AOP AspectJ Pointcut Expressions With Examples的相关文章

Spring AOP AspectJ Pointcut 表达式例子

主要来源:http://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with-examples/ 1. 方法标签匹配方式 假设定义了接口EmployeeManager接口. 1) execution(* com.howtodoinjava.EmployeeManager.*( .. )) 以上切入点表达式可以匹配EmployeeManger接口中所有的方法. 2) 当切面方

Spring基础系列12 -- Spring AOP AspectJ

Spring基础系列12 -- Spring AOP AspectJ 转载:http://www.cnblogs.com/leiOOlei/p/3613352.html 本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代码. Pointcut:注入Advice的位置,切入点,一般为某方法. Advisor:Advice和Pointcut的结合单元,以便将Advice和Pointcut分开实现灵活配置. Aspe

Spring AOP + AspectJ Annotation Example---reference

In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simple, Spring AOP + AspectJ allow you to intercept method easily. Common AspectJ annotations : @Before – Run before the method execution @After – Run aft

关于 Spring AOP (AspectJ) 你该知晓的一切

[版权申明]未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) http://blog.csdn.net/javazejian/article/details/54629058 出自[zejian的博客] 关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上最近比较忙,所以这篇文件写得比较久,也分了不同的时间段在写,已尽最大能力去连贯博文中的内容

Spring源码阅读:使用标准AOP的API模拟Spring AOP + AspectJ的设计与实现

在上一篇博客中,提到了标准AOP与Spring AOP.这一篇就来把他们模拟出来. 在模拟之前,还需要提及的是,在Spring框架中,对于AOP的支持: Spring 支持的AOP AspectJ是另外一个有名的AOP框架,Spring也集成AspectJ,同时Spring AOP与AspectJ有一定程度的集成,这样一来Spring中就支持两种AOP:1)Spring AOP.2)AspectJ.而使用AOP的方式却又三种: 1)完全使用Spring AOP 2)完全使用AspectJ(分为注

关于 Spring AOP (AspectJ) 该知晓的一切

关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上最近比较忙,所以这篇文件写得比较久,也分了不同的时间段在写,已尽最大能力去连贯博文中的内容,尽力呈现出简单易懂的文字含义,如文中有错误请留言,谢谢. OOP的新生机 OOP新生机前夕 神一样的AspectJ-AOP的领跑者 AspectJ的织入方式及其原理概要 基于Aspect Spring AOP

spring aop中pointcut表达式完整版

spring aop中pointcut表达式完整版 本文主要介绍spring aop中9种切入点表达式的写法 execute within this target args @target @within @annotation @args 0. 示例代码git地址 https://gitee.com/likun_557/spring-aop-demo 1.execute表达式 拦截任意公共方法 execution(public * *(..)) 拦截以set开头的任意方法 execution(

Spring AOP中pointcut expression表达式解析

Pointcut 是指那些方法需要被执行"AOP",是由"Pointcut Expression"来描述的. Pointcut可以有下列方式来定义或者通过&& || 和!的方式进行组合. args() @args() execution() this() target() @target() within() @within() @annotation 其中execution 是用的最多的,其格式为: execution(modifiers-patt

Spring AOP AspectJ 代码实例

本文参考来源 http://examples.javacodegeeks.com/enterprise-java/spring/aop/spring-aop-aspectj-example/http://oss.org.cn/ossdocs/framework/spring/zh-cn/aop.html 概念 面向方面编程 (AOP) 提供从另一个角度来考虑程序结构以完善面向对象编程(OOP). 面向对象将应用程序分解成 各个层次的对象,而AOP将程序分解成各个方面 或者说 关注点 . 这使得可