Recently in use springboot When you do section programming , We find that there are many ways to define section points , Each way has its own characteristics . It is hereby recorded
<>execution expression
The basic syntax format is :execution(< Modifier mode >?< Return type mode >< Method name pattern >(< Parameter mode >)< Abnormal mode >?) Except for the return type pattern , Method mode and parameter mode , All other items are optional .
for example :
@Pointcut("execution(public * cn.hjljy.*.controller..*.*(..))") public void
logCut(){} @Around("logCut()") public Object validateParam(ProceedingJoinPoint
joinPoint) throws Throwable { System.out.println(" Enter the section for verification "); Object obj =
joinPoint.proceed(); return obj; }
Pattern description
publicpublic express public Level method . You don't have to write it , Not writing is all the way (public,private,protected Hierarchical approach )
initial * Represents the type of the return value of a method * Means all
cn.hjljy.*.controller Represents the specific package name , Intermediate use * Make wild card
… Represents a package and its subpackages
* Means all
.*(…) Represents all methods
<>@args expression
args It is mainly used to limit the parameters of the method ,args There are two forms :@args and args
use @args It needs to be annotated , If there are parameters in the method, hold this annotation , Can .
for example :
@Pointcut("@args(cn.hjljy.mlog.common.annotation.MlogLog)") public void
logCut(){} @Around("logCut()") public Object validateParam(ProceedingJoinPoint
joinPoint) throws Throwable { System.out.println(" Enter the section for verification "); Object obj =
joinPoint.proceed(); return obj; }
If a method such as :
public void test(MlogCommentEntity entity) {
// If entity This entity class has MlogLog This note , It's going to be cut by the section . }
If it is args The method parameter type needs to be written and is a match execution Expressions, such as :
// The first way of writing @Around("execution( *
cn..*.controller..*.*(..))&&args(..org.springframework.validation.BindingResult)\"")
public Object validateParam(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println(" Enter the section for verification "); Object obj = joinPoint.proceed(); return obj; }
// The second way of writing @Around("execution( * cn..*.controller..*.*(..))&&
args(..bindingResult)") public Object validateParam(ProceedingJoinPoint
joinPoint, BindingResult bindingResult) throws Throwable {
System.out.println(" Enter the section for verification "); Object obj = joinPoint.proceed(); return obj; }
Pattern description
Two ways to express the beginning of any parameter , The last entry to the method is BindingResult type
<>@annotation expression
This is very common , Section directly through annotation . Just add the corresponding annotation to the method that needs to be faceted .
for example :
@Pointcut("@annotation(cn.hjljy.mlog.common.annotation.MlogLog)") public void
logCut(){} @Around("logCut()") public Object validateParam(ProceedingJoinPoint
joinPoint) throws Throwable { System.out.println(" Enter the section for verification "); Object obj =
joinPoint.proceed(); return obj; }
Then add the corresponding annotation to the method that needs to be faceted .
Technology
Daily Recommendation