Explain how to use AOP of spring boot to process custom annotations

The previous article introduced Java annotations, explained the basic usage of Java annotations, and implemented a simple testing tool through custom annotations; This article will introduce how to use AOP of spring boot to simplify the processing of custom annotations, and explain these contents by implementing a simple method and taking the execution time statistics tool as an example.

AOP concept

Aspect oriented programming (AOP) is a term in computer science, which refers to a programming paradigm. This paradigm is called side programming Based on the language structure of aspect, the side is a new modular mechanism to describe crosscutting concerns scattered in objects, classes or functions.

The concept of side comes from the improvement of object-oriented programming, but it is not limited to this. It can also be used to improve traditional functions. Side related programming concepts also include meta object protocols, subjects, mixins, and delegates.

Note: the above definitions are derived from Chinese Wikipedia (if you can't access it, you can access it by modifying the system's hosts file. 198.35.26.96 zh.wikipedia.org # Chinese Wikipedia can only help you. If you still can't access it, please search the Internet for how to modify the system's hosts file. The location of the hosts file is different under different systems. If it's Linux or MAC system, I'll tell you directly, general files The path is / etc / hosts). The translation of the word AOP is a little inconsistent with the domestic mainstream. The domestic mainstream translates AOP as "aspect oriented programming". Don't stick to the name, just know that it refers to the same thing.

It's estimated that you are ignorant after reading this definition. If you want to know more about it, you can see how the big guys break it. What is aspect oriented programming AOP. Let me just give you an example.

AOP environment preparation for spring boot

In POM Introducing corresponding dependency modules into XML

First, implement a simple web request processing

A simple controller to handle web requests.

For a simple HelloWorld service implementation class, I won't show the code for the definition of the interface.

Such a simple web service is ready. You can start the project and call it with curl command, such as curl - xget - I“ http://127.0.0.1:8080/hello?name=Java "If everything goes well, you will get a response like the following:

Use custom annotations to count the execution time of methods

First, define an annotation to count the execution time of the method.

Then define a section to handle the annotation just defined.

Finally, just add the @ analysisactor annotation to the method that needs to count the execution time.

Start the project and call it casually with curl command. If it goes well, you can observe the log printed in the section.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>