zoukankan      html  css  js  c++  java
  • 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 !!

  • 相关阅读:
    会话状态服务器解决方法
    让笔记本在插上外置鼠标时触摸板自动关闭
    “检测到有潜在危险的 Request.Form(QueryString) 值”的解决方法
    SQL Server2008不能登录解决方法
    SqlHelper
    修改IE查看源代码编辑器
    由于启动用户实例的进程时出错,导致无法生成 SQL Server 的用户实例解决办法
    性能测试用户模型(二):用户模型图
    索引帖:性能测试新手误区系列
    性能测试用户模型(三):基础数据分析、场景数据
  • 原文地址:https://www.cnblogs.com/davidwang456/p/5553746.html
Copyright © 2011-2022 走看看