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

Today, 4ye will share this Spring AOP plug-in 😝

The project in

The demo is divided into two modules:

👉 plug-in module Springboot-aOP-plugin

👉 Business module Springboot -aop-plugin-used

Module Function description

The 👉 plug-in module springboot-aOP-plugin provides two plug-ins

  1. Plug-in A MethodCountingTimesPlugin: statistical method call number
  2. Plugin B MethodSpendTimePlugin: Calculates the time it takes to call a method

👉 Business module Springboot -aop-plugin-used

  1. Provide business apis
  2. Plug-in configuration class, which is responsible for parsing
  3. Plug-in factory, which is responsible for loading, activating, and deactivating plug-ins

use

Package the plugin module into a JAR package, then configure plugins.json jar address in the business module, then activate/disable the plugin, you can see the different output in the console 😄

Schematic diagram 👇

Main knowledge points

  1. Class loader
  2. Spring AOP programming

Results demonstrate

The following API 🐖

Activate Plug-in 1

The number of times a method is called is counted

Close plug-in 1

Activate plug-in 1 again

Activate plugin 2 effects while you are at it

Also quite fun haha other partners on their own optimization 🐷

Main source code description

MethodCountingTimesPlugin plug-in by implementing the MethodBeforeAdvice @ Before annotations to achieve the effect

The MethodSpendTimePlugin plugin implements the @around annotation by implementing this MethodInterceptor

This part of the knowledge can see the previous article 👉 “Spring AOP internal practice!!”

The code is also very simple, without more introduction 👇

PluginConfig

This configuration class is loaded at initialization, parses the plugins.json configuration file, and then puts it into the map

DefaultPluginFactory

To activate the plug-in, do the following: 👇

That is, through this programmatic AOP

The full project is on Github, and the link is at the end of the article

Next, let’s talk about the pit encountered in building this small demo 🕳

Pit 🕳

  1. Spring-boot-maven-plugin is a plugin that has a main method, and the jar structure is changed to boot- INF/classes/. The jar structure is changed to boot- INF/classes/. The jar structure is changed to boot- INF/classes/. ClassNotFoundException all the time
  1. This demo uses JDK11. In JDK11, AppclassLoader cannot be converted to URLClassLoader. The differences are as follows: 👇

JDK11

JDK8

Therefore, in JDK11, there is no way to determine whether a jar package has been loaded by converting AppclassLoader to URLClassLoader

thinking

After completing this demo, 4ye has the following thoughts on AOP

Conditions for the occurrence of AOP

We all know that AOP is section-oriented programming, so we have to tell it where to cut in order to have a chance to create this proxy object

Take these annotations provided by Spring

  • Transaction @ Transactional
  • Asynchronous @ Async
  • Caching @cacheable, @cacheevict, @cacheput, @caching, etc

These are in the Spring-Aspects module

For more information about Spring modules, see the article 👉 “Do you know the seven modules of Spring?”

Also, when creating proxy objects, CGLIB can only proxy non-final, non-static methods of non-final classes.

Second, why use programmatic AOP

This highlights its advantages! After all, programming is the most flexible. Just like programmatic transactions, you can control the granularity of a transaction. With programmatic AOP, you can control how Advice is started and stopped.

Third, optimize the place

  1. MethodCountingTimesPlugin is through the map to hold the call number of different approaches, the key need to consider how to hook and methodology, and the only stay (optimization)
  2. Implementing hot updates to configuration files and refreshing cached Advice (to be optimized)

The newly discovered

Our topic is plug-in, plug-in pluggable features are very convenient, at the same time, we use the ClassLoader to achieve hot loading!

However, I learned that it not only has this function, but also can encrypt and decrypt class files. At the same time, 4ye also indirectly learned about Alibaba’s Pandora and unlocked a new source chapter, Spring Boot DevTools, which is very interesting. Strive for early share hey hey 😋

conclusion

Through the project to implement this AOP plug-in, learn a install 13 skill hahaha

The last

This article is shared here 🐖

Warehouse address 👇 (thank you for every star!)

Github.com/Java4ye/spr…

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!! 😆

\