Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze @ Aspect annotations in detail

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces you how to conduct a detailed analysis of @ Aspect annotations, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Nine @ Aspect annotated functions are supported in spring, which represent the join points of the target class in different ways, and there are four main types:

Method tangent function: define the connection point by describing the target class information

Method input point function: define the connection point by describing the information of the method input parameters of the target class

Target class tangent point function: define the connection point by describing the target class type information

Agent class tangent point function: defines the connection point by describing the information of the agent class of the target class

Let's take a look at the specific use of the tangent function.

Wildcard characters for function input parameters

* matches any character, but it may match an element in the context

.. Match any character, you can match multiple elements in the context

+ means to match all classes of the specified class by type, which must be followed by the class name, that is, all classes that inherit or extend the specified class, including the specified class.

@ Aspect annotations can be divided into three categories according to whether they support wildcards and the degree of support:

All wildcards are supported: excution (), within (),

Support + wildcard characters: args (), this (), target ()

Wildcards: @ args (), @ within (), @ target (), @ annotation () are not supported

Logical operator

We know that pointcuts are composed of functions, and logical operations can be performed between pointcuts, which are combined into compound pointcuts. Let's take a look at the pointcut operators supported in spring:

The & & operator, that is, performs the intersection of tangent points. In addition to the & & symbol in spring, we can also directly use the and keyword.

| | or operator, that is, to perform the union operation of tangent points. Similarly, in spring, we can use the keyword or in addition to the | | symbol.

! Non-operator is to perform anti-set operation, also in spring we can only use! Besides symbols, we can also use the not keyword.

Different types of enhancements

@ Before

The pre-enhancements, which are equivalent to the BeforeAdvice function, have two attributes:

Value: used to define tangent point

ArgNames: because the input parameter name of the method cannot be obtained through Java reflection, if we want to parse the pointcut through reflection technology during runtime, we can add the parameter name of the method to this property, with multiple parameters separated by commas

@ AfterRetruning

The post enhancement, which is equivalent to AfterRetruningAdvice, has four attributes:

Value: ditto

Pointcut: indicates the pointcut information. Like the value attribute, value will be overridden if the setting pointcut is displayed.

Returning: binds the return value of the target object method to the enhanced method

ArgNames: ditto

@ Around

Surround enhancement, which is equivalent to MethodInterceptor, has two main attributes:

Value: ditto

ArgNames: ditto

@ AfterThrowing

An enhancement is thrown, which is equivalent to ThrowsAdvice, and the annotation has four attributes:

Value: ditto

Pointcut: ditto

Throwing: bind the thrown exception to the enhanced method

ArgNames: ditto

@ After

The Final enhancement, whether it throws an exception or exits normally, is performed. The main function is for resource release, and the annotation also has two attributes:

Value: ditto

ArgNames: ditto

@ DeclareParents

Introduction enhancements, which are equivalent to IntroductionInterceptor, have two attributes:

Value: ditto

DefaultImpl: default interface implementation class

This is the end of the detailed analysis of how to do @ Aspect annotations. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report