SpringBoot Learning (03): Similarities and differences between weak language annotations and SpringBoot annotations

Weak language representative: Hyperf , a resident memory framework based on PHP Swoole extension

An illustration of the concept of annotation;

  1. To put it bluntly, you are the leader and ask the secretary to help you arrange the mundane things. You only need to comment. For example, if you want to hold an event next week, the secretary will hand over the plan to you for approval. After you read it, you can comment on it. Find 4 well-known experts to be guests, and take 10 high-quality pictures to make a promotional brochure. After receiving the notice, the secretary will help you invite these 4 well-known experts according to your requirements, and will also take a bunch of photos for you. , and at the same time selected 10 high-quality ones to make a small manual.

  2. Because the annotation is a kind of annotation, the annotator does not need to execute it, so no code details can be seen in the annotation, but only defines the optional parameters passed in by the annotation

  3. This is a leader’s way of understanding annotations. It can also be understood from the perspective of an assembly line worker. After each assembly line worker is recruited, they don’t know much about the company or how many departments there are, but they have to get started as soon as they come in. Start working. For example, what worker A is doing now is to add pins to his job tag, and the purpose of the pins is to hang the nameplate. However, the current assembly line worker doesn’t know who is going to add the nameplate, so he adds a pin to the pin. Note: When hanging nameplates, the manager will ask the workers who come to hang the nameplates to hang the nameplates when sorting the clothes.

    1. In this way, the worker's behavior is disassembled in sufficient detail, and the entire scheduling process is controlled by another system, so the work of each worker becomes easier. He only needs to know what he wants to do and what he needs. As for the previous or He doesn't care who the next process is, so complete decoupling is achieved.

  4. Transformed to the code level to understand, the code you add comments will not be run directly. There will be an organizer, which is the starter in Spring Boot. This starter will automatically create all classes (recruitment lists) with annotations. into objects (workers who meet the list), just like recruiting assembly line workers in advance, and each worker is equipped with their own tools. After the assembly line is started, the starter starts to distribute requests, and then at each link, schedules the corresponding The worker performs processing, and after the processing is completed, it is handed over to the next worker for processing, so the source code with annotations you see is just a logical node in the entire pipeline. This logical node is also called an embedded node;

  5. During the execution process, because strong and weak languages ​​​​process annotations differently, two solutions have been produced. SpringBoot is based on Java, and annotations are language features, so many processing logic details are done for you at the bottom of the language. It is not exposed, which is more friendly to developers. Hyperf is based on PHP. PHP does not have the concept of annotations, so it is necessary to completely simulate the process of processing annotations. Therefore, Java generates a new set of code after compilation. PHP does not have a compilation process, so hyperf has to implement the compilation process by itself. Hyperf will generate Proxy code from the annotation code, and the Proxy code is actually executed. Java executes the class code, and the actual execution is also the class code. At this time, the two The two are unified.

  6. If a person only learns SpringBoot, this process is difficult to understand, because JAVA has not exposed this process to you, and you can only learn about this process from higher non-JAVA programmers, so single-language programmers It will be in a hazy cognition for a long time;

  7. Similarly, there is the concept of coroutines. Understanding coroutines from the GO language alone (no details, just go ahead, but it is still a synchronous language, coroutines are inherently asynchronous languages, you will definitely be confused if you start with synchronization), it will make people wonder. It’s not clear. If you come into contact with coroutines from Python, too many details will be exposed (and Python is also a synchronous language, which makes it even more confusing). It is easiest to start with Javascript in the intermediate state, because the browser itself has already implemented coroutines for you. , but some events and event monitoring are all coroutine programming, so you can thoroughly experience the gameplay of coroutines (use of yield syntax) in the middle process. After you are familiar with yield, you can run to python to simulate starting one yourself. When you execute the browser event loop service and then execute yield, your understanding suddenly becomes clear. After you understand this, and then learn Go, you will basically become more comfortable, so that you can play well.

I believe that after the above explanation, Xiaobai users should suddenly understand the annotations.

https://www.bookstack.cn/read/hyperf-2.0/annotation.md

What is Annotation What is Annotation?

Before explaining annotations, we need to define the difference 注解between and 注释:

  • Comments: Show the programmers, help understand the code, and explain and illustrate the code.

  • Note: For the application program, the definition used for metadata has no effect when used alone, and it needs to cooperate with the application program to use its metadata to have any effect.

The application, you can think of it as the hyperf startup program bin/hyperf.php, recognizes this process and performs certain processing, especially hyperf customizing some annotation tags. At startup bin/hyperf.php, the source code with annotation tags will be translated into execution code. Used during specific implementation. The process of generating proxy classes is the same as the npm pre-compilation process, and the code is regenerated. The actual packaged code is the npm pre-compiled code, not the source code, and the code that hyperf actually participates in running is not the source code you wrote. It knows how to generate proxy classes, so when you want to debug, type in your own source code. Breakpoints are useless, you must break points in the proxy class.

SpringBoot annotations

  1. There is not much difference in the implementation logic of Hyperf. The difference lies in the language characteristics. The PHP language does not have the concept of annotations. There is only one PHPDoc, which is used to automatically generate documents. Since documents can be automatically generated, it must be identifiable to the degree, so some people just Use PHPDoc to implement annotations, as long as you encounter a character with @, and then get the content after the @ character, you can execute the processing logic according to the content, and it will be OK

  2. Java annotations are an attribute of the language itself, which can be obtained directly at compile time and runtime, which directly saves a parsing process, and only needs to collect the classes and attributes marked by annotations, so that the application can You can assign and collect the data you have collected for unified processing.

  3. SpringBoot has written the entire web process, and has reserved a lot of content to be filled in. These places to be filled in are named, for example, , , @Controllerand @Servicethen @Repositoryyou mark the content on the code you wrote, then SpringBoot At runtime, you know that you have filled in the blank. When you need to use your logic, it will be extracted from the collector and executed. At the same time, you also want to get something. For example, these annotations can put @Valuesome @Configurationcontent Fill it in directly, but this process happens at runtime, you just use it here to implement the logic, and then it can run normally after assembling it. This can also be understood as embedded Web development, accept The external data is processed completely independently, and then the result is returned.

  4. SpringBoot annotations are executed when SpringBoot starts after the compilation is completed, so compilation checks can still be performed.

Python/PHP Thinking SpringBoot Series Exploration

Learning knowledge is all interconnected. Let’s first take a look at what we have explored before.

  1. SpringBoot learning (01): javac, src/main/java directory, Maven and pip weak type language why there is no submodule compilation
  2. SpringBoot Learning (02): The evolution of SpringBoot from embedded systems to embedded Servlets_Sen Ye's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/wangsenling/article/details/132383491