Tangent points, facets: @Aspect, @PointCut related personal summary

Source: Internet
Author: User

Recently wrote the project has used a lot of facets, the knowledge of tangent points, after writing, from my summary:

As an example:
In a Class (show) There is a method of performing (display), before the performance, there should be a speech (say) method, after the performance, there should be applause (hand) method.
For high cohesion and low coupling, the performance method should be encapsulated separately within a class, the methods of speech and applause should be encapsulated separately, but must be invoked in order to invoke execution. It's time to set the tangent and tangent points!

The facets should be set on the class where the speech (say) and applause (hand) methods are located, and at the same time, to configure pointcuts within 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 the configuration is complete, the speech (say) and applause (hand) methods are automatically invoked when the display (Pointcut) method is called.

When testing a similar code, you should break the point in the tangent method and break the point in the method you want to execute.

In addition, the problem encountered in a project is that a method in a class is not called. It can then be resolved by setting a tangent point or tangent plane.
Such as:
The Xxfilter class has a updateinit () method, because the filter cannot be injected automatically, and the method cannot be called outside. Br/> Solution:
Write yourself a Xxservice class (set auto-inject @service), define an update () method inside, the method body is empty,

And then

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

This makes it possible to call the Updateinit () method by calling the update () method Xxservice.

    1. notification, enhanced Processing (Advice): Is the function you want, you define it first, and then use it where you want to use it, including a piece of aspect code.
    2. Connection point (Joinpoint): This is where spring allows you to be a notification (Advice), the first and last of each method (both have a row), or throws an exception can be a connection point, spring only supports method connection point. Others, such as ASPECTJ, can also allow you to work with constructors or property injections, but remember that all the way around the method is the connection point. The connection point is to obtain information about the tangent method (the class, the entry, the method, and so on)
    3. Entry point (Pointcut): On the basis of the connection point above, to define the pointcut, in one of your classes, there are 15 methods, there are more than 10 connection points, but do not want to use the notice near all methods (using called weaving), just want to let a few, before calling these methods, To do something later or throw an exception, use the Pointcut to define the methods, let the pointcut filter the connection points, and select the ones you want.
    4. Facets (Aspect): Facets are a combination of notifications and pointcuts. Connection point is to facilitate understanding of the pointcut, understand the concept is OK. The notice explains what to do and when to do it (when you can tell by Befor,after,around in the method name), and the pointcut shows where to go (specifying exactly which method), which is a complete definition of the facet.
@Aspect //声明切面,标记类  public class Wly {  @Pointcut("execution(* *.perform(..))") //定义切点,标记方法  public void performance() {}  @Before("performance()")  //切点之前执行  public ....        @AfterReturning("performance()")  //切点执行成功之后执行  public ...  //  @After("....")   // 在切点之后执行,无论方法执行成功与否@AfterThrowing("performance()")  //切点抛出异常后执行  public ...  }  

Pointcuts, Facets: @Aspect, @PointCut related personal summary

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.