编程知识 cdmana.com

Spring AOP internal skill cultivation

ha-ha Finally here

Today, let's share this with our friends Spring AOP   Knowledge points of

 picture
image-20210913072321674

Here I recommend you to read Spring Official documents of ,core Document 5,6 chapter , and AOP Related concepts can be found here ~, And what we need to know Spring AOP APIs

 picture
image-20210910074335232

Let's introduce this AOP

AOP Concept

This is an old question ha-ha Familiar friends can skip

Section oriented programming Aspect-Oriented Programming , It's right OOP A supplement to

The usage scenarios are : journal , Business , Permission authentication , cache etc.

and Spring AOP Well , It will go through this proxy class To implement the method enhance

 picture
image-20210910073611972

Here are some important concepts ( Just look at the picture )

 picture
image-20210910074649844

Concept diagram

adopt PointCut To match matching JoinPoint , Do it again Weaving· operation , take  Advice Weave into it .

and PointCut and Advice Store in Aspect   in .

 picture
image-20210910081828619

Tangent expression graph

Such as :execution ,within , this ,target ,args ,bean

as well as Match annotation With @target ,@args ,@within , @annotation

 picture
image-20210910082639157

Advice Annotation diagram

Here are five common annotations

Be careful :@After be equal to finally Sentence block , It must be carried out , also @Around When the annotation , The first parameter of the method must be ProceedingJoinPoint , It is JoinPoint Sub interface of

 picture
image-20210913074020835

The following is the focus of this article ~

programmatic AOP

How about this one , We wrote AOP plug-in unit The basis of

 picture
image-20210912074920354

Here we need to understand these concepts

  • Pointcut :   Corresponding @Pointcut
  • Advice Corresponding @Before,@After,@Around wait
  • Advisor@Aspect Annotation parsed class , be equal to Pointcut + Advice
  • Advised :   Spring AOP Proxy classes can be converted to this interface !

Example I put Github Come on , The address is at the end of the text

Pointcut

There are two return types  ClassFilter and  MethodMatcher   Methods , Their functions are as follows

 picture
image-20210912100228554

4ye It's also about understanding ha-ha Mainly with Spring Implementation class provided , As used in the following example NameMatchMethodPointcut , You can guess by name , It matches the method name

Small example

There are many code examples , Here's one Pointcut   Just have a look ~  ( Other case codes can be found on my own Github Get from warehouse )

Through to ProxyFactroy add to section To the target sayService Do increase , there Pointcut Only aim at setName Method

 picture
AOP_Pointcut

effect

You can see that only enhanced setName Method

 picture
image-20210912094544020

Advice

You can see here Spring Provides this Five types . Except for the last one Introduction The type is not mentioned above , Is there another type missing

 picture
image-20210912161447604

you 're right , This is this. @After The notes correspond to Final notice

Where have you been ( ̄﹏ ̄;)

After looking in the source code , Found this AspectJAfterAdvice

 picture
image-20210912104757851

You can see that not only is it realized here AfterAdvice   Interface , And realize this MethodInterceptor Interface , Finally, really in finally This is implemented in the statement block Enhanced operation , In order to achieve Final notice The effect of !

So we need Final notice In terms of effect , Just imitate it directly ~

and Introduction Advice This is quite special

effect : Implement a new interface for the proxy class

be equal to @DeclareParents annotation

Advisor

effect

@Aspect Annotation parsed class , be equal to Pointcut + Advice

Advised

effect

AOP Proxy classes can be converted to this interface

Why can these proxy objects be converted to this Advised Interface ?

Of course, the answer is self-evident ha-ha , Let's look at the source code Spring How is it written

JDK

 picture
image-20210911231149301

Cglib

 picture
image-20210911233943416

Conclusion

You can find , Whether it's JDK still Cglib , Will call the same method , Implement several more interfaces for the proxy class , One of them is Advised

The diagram

 picture
SpringAOP

Last

So much for this article

The key content is programmatic AOP   This piece of ~ Internal skill +1

The code can be obtained here   You can run more code yourself , There will be more problems waiting for you to find out !

https://github.com/Java4ye/springboot-demo-4ye/tree/main/springboot-aop

Mind mapping

http://processon.com/chart_image/6134d6cb7d9c081c753b5d54.png

ps: It actually appeared when sharing this time Gateway Timeout abnormal , If you can't wait for the official repair , It can be obtained on the network disk

link :https://pan.baidu.com/s/1KVWw0DaYCKrISIKJgqqyuA

Extraction code Need to reply in official account background. bdwp

 picture
image-20210913074314344

I also uploaded part of the code of the plug-in , I need to draw and write ha-ha ( These two days should be arranged )

If you like, you can Focus on and Star standard Next official account Java4ye Under the support 4ye ah , In this way, you can receive more information at the first time

I am a 4ye We should …… See you soon !!


版权声明
本文为[Java4ye]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/10/20211002145641037d.html

Scroll to Top