This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Today to share the knowledge of Spring AOP with friends 👇

We recommend that you read the official Spring documentation, which includes sections 5 and 6 of the Core documentation. AOP related concepts can be found here, as well as the Spring AOP APIs 😄

Let’s start with the AOP 😄

AOP concepts

This is a platitude question ah ha ha familiar partners can skip 😄

Aspect-Oriented Programming is a supplement to OOP

The application scenarios include log, transaction, permission authentication, and cache

Spring AOP, on the other hand, implements enhancements to methods through this proxy class 👇

Here are some important concepts 👇(just look at the picture)

Conceptual diagram

We need to match the JoinPoint with PointCut and Weaving Advice.

Pointcuts and Advice reside in aspects.

Tangent expression graph

Execution, within, this, target, args, bean

And @target, @args, @within, @annotation for matching annotations

Advice note figure

Here are five commonly used notes

Note: @After equals the finally block, which must be executed. With the @around annotation, the first argument to the method must be ProceedingJoinPoint, which is a subinterface of JoinPoint


The following is the focus of this article ~ 😝

Programmatic AOP

This is the basis for writing AOP plug-ins

Here are some concepts to understand 👇

  • Pointcut: corresponds to @pointcut
  • Advice: corresponding to @before, @after, @around, etc
  • Advisor: @aspect Annotation parsed class, equal to Pointcut + Advice 🐷
  • The Advised Spring AOP proxy classes can be converted to this interface! 🐷

Example I put on Github, the address put at the end of the article 👇

Pointcut

There are two methods that return type ClassFilter and MethodMatcher, which do the following: 👇

NameMatchMethodPointcut (😄) = NameMatchMethodPointcut (NameMatchMethodPointcut

Small example

The Pointcut can be accessed from my Github repository at 😄.

The target sayService is enhanced by adding facets to the ProxyFactroy, where the Pointcut is for the setName method only

The effect

You can see that only the setName method is enhanced

Advice

You can see that Spring provides these five types here. In addition to the last Introduction type not mentioned above, is there still a type missing 😝

\

Yes, the @after annotation corresponds to the final notification 😱

Where did this go (@ $@ $@ $)

After digging around in the source code, I found AspectJAfterAdvice 👇

You can see that AfterAdvice is implemented as well as the MethodInterceptor interface, and that the enhancement is actually implemented in the finally block to give the final notification!

So if we need the final notification effect, we can directly imitate it to achieve it

And Introduction Advice is quite special 🐖

Effect: Implements a new interface for the proxy class

Is equal to at sign DeclareParents annotation

Advisor

Role 👇

The @aspect annotation parsed class is equivalent to Pointcut + Advice

Advised

Role 👇

Any AOP proxy class can be converted into this interface

Why can these proxy objects all be converted to the Advised interface?

Of course, the answer is self-evident haha, let’s look at the source code to see how Spring is written 😝

JDK

Cglib

conclusion

You can see that both the JDK and Cglib call the same method to implement several more interfaces for the proxy class, one of which is the Advised 😋

The diagram

The last

This article is shared here 🐖

Key content in programming AOP this piece ~ internal work +1 😋

Code can be obtained here 👇 you can run more code, there will be more problems waiting for you to find!

Github.com/Java4ye/spr…

Mind mapping 👇

Processon.com/chart_image…

Ps: There was a Gateway Timeout exception when sharing this time. If you can’t wait for the official repair, you can get it from the network disk

Link: pan.baidu.com/s/1KVWw0DaY…

The extraction code shall be replied to BDWP in the background of the official account

Plug-in part of the code I also uploaded, the difference in this drawing diagram to write the text ha ha (this two days should be arranged)😆

If you like, you can pay attention to the public number Java4ye and support 4YE 😝, so that you can receive more news for the first time 🐷

I’m 4ye. We should… next time. See you soon!! 😆