Spring AOP is based on annotation explanation and example code

Spring AOP is based on annotation explanation and example code

1. Enable spring's support for @ AspectJ annotation:

You can also configure the annotation awareaspectjautoproxycreator bean to start spring's support for @ AspectJ annotations

2. In order to start @ AspectJ support in the application, two AspectJ libraries need to be added by Yaao: aspectjweaver Jar and aspectjrt jar。 In addition, spring AOP also needs to rely on an aoplliance Jar package

3. Define cut bean

4. Define an enhanced processor, such as before

@The afterreturning annotation will be woven in after the target method completes normally. The annotation specifies the following two attributes: 1 > pointcut / value: used to specify the entry expression corresponding to the entry point; 2 > returning: used to specify a formal parameter name to access the return value of the target method. Meanwhile, if this parameter type is specified in the advice method, the return value of the target method will be restricted to this type

@The afterthrowing annotation is used to handle unhandled exceptions in the program. The annotation specifies the following attributes: 1 > pointcut / value: used to specify the entry expression corresponding to the entry point; 2 > throwing: the attribute value also specifies a formal parameter name to represent the unhandled exception thrown by the target method. At the same time, if the parameter type is specified in the advice method, it will restrict the target method from throwing exceptions of the specified type

@The around annotation is similar to the sum of before and afterreturning enhanced processing. It can determine when the target method is executed, because the first formal parameter of the advice method modified by the annotation is of type proceedingjoinpoint. The proceedingjoinpoint parameter has a processed () method, which can be called to execute the target method. If it is not shown in the advice method that the method is called, the target method will not be executed:

5. If you need to obtain the relevant information of the target method, you can define the first parameter as the joinpoint type when defining the enhancement processing method. When the enhancement processing method is called, the joinpoint parameter represents the connection point woven into the enhancement processing. Joinpoint is similar to the proceedingjoinpoint of around enhancement processing, but the latter is specific to around enhancement processing. Joinpoint contains the following common methods:

The example before enhancement process obtains information about the target method

6. If two advisories in two different aspects need to be implanted at the same joinpoint connection point, spring AOP will weave the two enhancement processes in random order. If you need to specify their priorities, there are two ways:

7. Define pointcut: it consists of two parts: a pointcut expression and a method signature containing name and arbitrary parameters:

8. Pointcut indicator: that is, the execution class specified in the previous enhancement processing, which is used to specify the conditions to be met by the target method. Spring AOP supports the following pointcut indicators:

9> Finally, we need to use the element in the spring configuration file to specify the automatic search aspect class

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>