Cut point, cut face: personal summary related to @Aspect and @PointCut

The recently written projects have used a lot of aspects and knowledge of point-cutting. After writing, I will summarize it myself:

For example:
a class (Show) has a method of show (display), before the show, there should be a method of say (say), after the show, there should be a method of applause (hand).
For high cohesion and low coupling, the method of performance should be encapsulated in a separate class, and the methods of speech and applause should be encapsulated separately, but they must be called and executed in order. At this time, you need to set the cut plane and cut point!

The aspect should be set on the class where the say and hand methods are located. At the same time, the pointcut should be configured in the class, similar to:

 //定义切点--功能权限更新com.eastcom.bbf.bs.management.service
@Pointcut("execution( com.eastcom.bbf.bs.management.service.FunctionRightsService.updateFunctionRight(..))")
  private void updateFunctionRightPointCut() {
        // 这是一个标记方法
  }*    
    》》即:@PointCut()里面应该配置 表演(display)方法的路径
如果上面没有定义标记方法,则下面的pointcut 应该等于"execution(......)"

@AfterReturning(pointcut = "updateFunctionRightPointCut()",returning = "rvt")
  public void afterUpdateFunctionRight(JoinPoint joinPoint,String rvt) {
    **// 此方法是在切入点方法执行之后执行**
        // joinPoint.getArgs() 就是获取切入点方法的入参
        // rvt 就是切入点方法的返回值
        ..............
        》》此方法相当于 鼓掌(hand)方法。
  }
@Before("updateFunctionRightPointCut()")
  public void beforeSaveServiceNode(JoinPoint joinPoint) {
    **  // 此方法在切入点之前执行**
    ServiceNode node = (ServiceNode) joinPoint.getArgs()[0];
    .............
        》》此方法相当于 致辞(say) 方法。
  }

When this configuration is completed, when the display (entry point) method is called, the say and hand methods will be automatically called.

When testing similar code, you should break the point in the method of the pointcut, and then in the method you want to execute.


In addition, a problem encountered in the project is that a method in a class cannot be called. Then it can also be solved by setting tangent points and tangent planes.
Such as:

There is an updateInit() method in the XXFilter class, because the filter cannot be injected automatically, and this method cannot be called from outside.

solve:

Write a XXService class by yourself (set automatic injection @Service), define an update() method in it, and the method body is empty,

Set @Aspect above the XXFilter class, and set @PointCut("execution(path to the update() method)") inside,

Then , write an XXService class by yourself (set up automatic injection @Service), define an update() method in it, the method body is empty,  set @Aspect on the XXFilter class, and set @PointCut("execution(update() method in it) route of)"),

Then :

@AfterReturning(...)
void  xxUpdate(){
    // 在里面只是调用updateInit()方法。
    updateInit( );
}

In this way, the updateInit() method can be called by calling the update() method of XXService.


  1. Notification and enhanced processing (Advice): It is the function you want, you define it first, and then use it where you want to use it, including a piece of processing code for Aspect.
  2. JoinPoint: It is the place where spring allows you to be an Advice. Basically, before and after each method (both are also possible), or when an exception is thrown, it can be a join point. Spring only supports methods Junction. Others such as AspectJ also let you do it in constructor or property injection, but just remember that there are connection points before and after methods. The connection point is to obtain the relevant information of the pointcut method (the class, input parameter, method, etc.)
  3. Pointcut: On the basis of the connection points mentioned above, define the pointcut. If you have 15 methods in a class, there are more than a dozen connection points, but you don't want to use them near all the methods. Notification (using called weaving), just want to let a few of them do something before, after calling these methods or when an exception is thrown, then use pointcut to define these methods, and let pointcut filter Connect the dots and select the ones you want.
  4. Aspect: Aspect is a combination of advice and pointcut. The connection point is to facilitate the understanding of the pointcut, and it is enough to understand this concept. The notification explains what to do and when to do it (you can know when to use the before, after, around, etc. in the method name), and the pointcut explains where to do it (specify which method it is), which is a complete aspect definition.
@Aspect //声明切面,标记类  
public class Wly {  

@Pointcut("execution(* *.perform(..))") //定义切点,标记方法  
public void performance() {}  

@Before("performance()")  //切点之前执行  
public ....        

@AfterReturning("performance()")  //切点执行成功之后执行  
public ...  
//  @After("....")   // 在切点之后执行,无论方法执行成功与否

@AfterThrowing("performance()")  //切点抛出异常后执行  
public ...  
}  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324371007&siteId=291194637
cut