标签:
原文地址:http://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with-examples/
The most typical pointcut expressions are used to match a number of methods by their signatures.
For example, the following pointcut expression matches all of the methods declared in the EmployeeManager
interface. 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.*(..)) |
You can omit the package name if the target class or interface is located in the same package as this aspect.
execution(* EmployeeManager.*(..)) |
Use public keyword in start, and use * to match any return type.
execution( public * EmployeeManager.*(..)) |
Use public keyword and return type in start.
execution( public EmployeeDTO EmployeeManager.*(..)) |
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, ..)) |
Use public keyword and return type in start. Also, specify all parameter types as well.
execution( public EmployeeDTO EmployeeManager.*(EmployeeDTO, Integer)) |
When applied to Spring AOP, the scope of these pointcuts will be narrowed to matching all method executions within the certain types only.
It’s much like previous example.
within(com.howtodoinjava.*) |
For including, sub-packages use two dots.
within(com.howtodoinjava..*) |
Much like previous example using execution keyword.
within(com.howtodoinjava.EmployeeManagerImpl) |
In case of same package, drop package name.
within(EmployeeManagerImpl) |
Use + (plus) sign to match all implementations of an interface.
within(EmployeeManagerImpl+) |
You can match all beans as well having a common naming pattern e.g.
It’s quite easy one. Use an * to match anything preceding in bean name and then matching word.
bean(*Manager) |
In AspectJ, pointcut expressions can be combined with the operators && (and), || (or), and ! (not). e.g.
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 !!
Spring AOP AspectJ Pointcut Expressions With Examples
标签:
原文地址:http://www.cnblogs.com/davidwang456/p/5553746.html