Explain in detail the common writing method of pointcut pointcut expression in Spring framework

  • 2020-06-19 10:19:28
  • OfStack

Since using AspectJ-style slice configuration, spring's slice configuration has been greatly simplified, but AspectJ is another open source project with a slightly weird syntax for regular expressions.

Some common examples are given below. For example, here is a section configuration for all methods on the Service package:


<aop:config> 
  <aop:pointcut id="serviceOperation" expression="execution(* *..service*..*(..))"/> 
  <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/> 
</aop:config>

The expression is in the same position as pointcut. Configure this to better control transactions on the plane. Here is a simple example of how to configure a thing:


<tx:advice id="txAdvice" transaction-manager="transactionManager"> 
   <tx:attributes> 
      <tx:method name="delete*" rollback-for="Exception"/> 
      <tx:method name="save*" rollback-for="Exception"/> 
      <tx:method name="update*" rollback-for="Exception"/> 
      <tx:method name="*" read-only="true" rollback-for="Exception"/> 
    </tx:attributes> 
</tx:advice>

With faceted, notification configuration, 1 transactional transaction has been added to all methods starting with delete, save, and update, and read-only transactions have been added to other methods.

This is not enough detail, if you want to write more detailed control, you will need to study the syntax of AspectJ pointcut configuration, in fact, studying these standards, it is better to take a few examples and solve the real problem. Just like regular expression 1, the standard is obvious, but it is not easy to write well, and you can quickly pick it up and understand it from an example.

The following documents are from Spring Chinese development Guide 2.5 documents, translated by MJH:

Spring AOP users may frequently use the execution pointcut indicator. The format of the execution expression is as follows:


execution ( modifiers-pattern? 
ret-type-pattern declaring-type-pattern? 
name-pattern ( param-pattern ) throws-pattern? ) 

All sections are optional except for the return type mode (ES32en-ES33en-ES34en in the code snippet above), the name mode, and the parameter mode. The return type pattern determines that the return type of a method must in turn match 1 join point. The most frequent pattern of return types you will use is *, which matches any return type. A fully qualified type name will only match methods that return a given type. The name pattern matches the method name. You can use * wildcards as all or part of the naming scheme. The parameter pattern is a little more complicated, () matches a method that takes no arguments, while (..) Matches a method that takes any number of arguments (zero or more). Pattern (*) matches a method that accepts 1 argument of any type. The pattern (*,String) matches a method that takes two arguments, the first of which can be of any type and the second of which must be of String type. See the language semantics section of the AspectJ Programming Guide for more information. Examples of 1 common pointcut expressions are given below.

Execution of any public method:


execution ( public * * ( .. )) 

Execution of any 1 method whose name begins with set:


execution ( * set* ( .. )) 

Execution of any method defined by the AccountService interface:


execution ( * com.xyz.service.AccountService.* ( .. )) 

Execution of any method defined in the service package:


execution ( * com.xyz.service.*.* ( .. )) 

Execution of any method defined in an service package or its subpackages:


execution ( * com.xyz.service..*.* ( .. )) 

Any join point in the service package (in Spring AOP only method execution) :


within ( com.xyz.service.* ) 

Any join point in an service package or its children (in Spring AOP only method execution) :


within ( com.xyz.service..* ) 

Any join point of a proxy object that implements the AccountService interface (in Spring AOP only method execution) :


<tx:advice id="txAdvice" transaction-manager="transactionManager"> 
   <tx:attributes> 
      <tx:method name="delete*" rollback-for="Exception"/> 
      <tx:method name="save*" rollback-for="Exception"/> 
      <tx:method name="update*" rollback-for="Exception"/> 
      <tx:method name="*" read-only="true" rollback-for="Exception"/> 
    </tx:attributes> 
</tx:advice>
0

Any join point of the target object implementing the AccountService interface (in Spring AOP only method execution) :


<tx:advice id="txAdvice" transaction-manager="transactionManager"> 
   <tx:attributes> 
      <tx:method name="delete*" rollback-for="Exception"/> 
      <tx:method name="save*" rollback-for="Exception"/> 
      <tx:method name="update*" rollback-for="Exception"/> 
      <tx:method name="*" read-only="true" rollback-for="Exception"/> 
    </tx:attributes> 
</tx:advice>
1

Any 1 takes only 1 argument and the arguments passed in at run time are the join point of the Serializable interface (in Spring AOP it is only method execution) :


<tx:advice id="txAdvice" transaction-manager="transactionManager"> 
   <tx:attributes> 
      <tx:method name="delete*" rollback-for="Exception"/> 
      <tx:method name="save*" rollback-for="Exception"/> 
      <tx:method name="update*" rollback-for="Exception"/> 
      <tx:method name="*" read-only="true" rollback-for="Exception"/> 
    </tx:attributes> 
</tx:advice>
2

Note that the pointcut given in the example is different from execution(* *(Java.io.Serializable)). The args version matches only if the parameter is Serializable when passed in dynamically at run time, whereas the execution version matches only if there is only one parameter of type Serializable declared in the method signature.

Any join point with 1 @Transactional annotation in the target object (in Spring AOP only method execution) :


@target ( org.springframework.transaction.annotation.Transactional ) 

Any 1 type declared by the target object has 1 @ES105en annotated join point (in Spring AOP only method execution) :


<tx:advice id="txAdvice" transaction-manager="transactionManager"> 
   <tx:attributes> 
      <tx:method name="delete*" rollback-for="Exception"/> 
      <tx:method name="save*" rollback-for="Exception"/> 
      <tx:method name="update*" rollback-for="Exception"/> 
      <tx:method name="*" read-only="true" rollback-for="Exception"/> 
    </tx:attributes> 
</tx:advice>
4

Any method that executes has a join point annotated by @Transactional (in Spring AOP only method execution) :


<tx:advice id="txAdvice" transaction-manager="transactionManager"> 
   <tx:attributes> 
      <tx:method name="delete*" rollback-for="Exception"/> 
      <tx:method name="save*" rollback-for="Exception"/> 
      <tx:method name="update*" rollback-for="Exception"/> 
      <tx:method name="*" read-only="true" rollback-for="Exception"/> 
    </tx:attributes> 
</tx:advice>
5

Any 1 join point that accepts only 1 argument and that the parameter type passed in at runtime has the @ES117en annotation (in Spring AOP only method execution) :


<tx:advice id="txAdvice" transaction-manager="transactionManager"> 
   <tx:attributes> 
      <tx:method name="delete*" rollback-for="Exception"/> 
      <tx:method name="save*" rollback-for="Exception"/> 
      <tx:method name="update*" rollback-for="Exception"/> 
      <tx:method name="*" read-only="true" rollback-for="Exception"/> 
    </tx:attributes> 
</tx:advice>
6

Any 1 join point above Spring bean named tradeService (in Spring AOP only method execution) :


<tx:advice id="txAdvice" transaction-manager="transactionManager"> 
   <tx:attributes> 
      <tx:method name="delete*" rollback-for="Exception"/> 
      <tx:method name="save*" rollback-for="Exception"/> 
      <tx:method name="update*" rollback-for="Exception"/> 
      <tx:method name="*" read-only="true" rollback-for="Exception"/> 
    </tx:attributes> 
</tx:advice>
7

Any 1 join point above Spring bean whose name matches the wildcard expression *Service (in Spring AOP only method execution) :


bean ( *Service ) 

Among them, this , tagart , args , @target , @with , @annotation and @args It is more commonly used in bound forms.


Related articles: