Author's Message
Hello, everyone, this is my first article. I hope to summarize the knowledge I have learned and share it with you. I will share it with you in the next period of time. Publish a series of Java, Python and other entry-level related articles, and share them systematically so that you can go further by laying a solid foundation. I hope you all will give me some advice! Without further ado, let’s get down to the practical stuff! (If there is any infringement involved, please contact me through this platform to delete)
Preface
XML as a configuration file is popular among most programmers However, some people prefer to use annotations. In fact, I personally feel that the choice is not the point. The point is to understand the essence of the birth of each technology; XML as a configuration file and code is a "loosely coupled" code description, but when XML configuration When there are too many files, it is difficult to manage. At the same time, the IDE cannot verify the correctness of the XML configuration file, which increases the difficulty of testing. Annotations are "tightly coupled" code descriptions. Its purpose is to make the application easier to expand while also "Zero" configuration.
1. What is annotation
Annotation is annotation, which is the metadata in the code (metadata: data describing the data). By using annotation, Program developers can embed some supplementary information in the source files without changing the original logic. Please take a look at the following code snippet:
For beginners, in fact, they often see similar code and wonder what the hell is @Override? In fact, it is an annotation. Adding @Override to the toString() method means that the toString() method under the annotation must reconstruct the parent class method.
After seeing this, I think some people will think that I will introduce various annotations to you next? ! I don't!
2. Grammar standards of annotation types
Annotations are a special type in Java. Next, let’s take a look at how to design an annotation type.
1. Grammar standard:
public @interface 注解類型名稱 { [ 數(shù)據(jù)類型 變量名 () [ default 初始值 ]; ] }
Note:
1) The content in "[ ]" is optional. If the annotation is empty inside, it means the current annotation Annotate the logo.
2) Annotations should intelligently include variables and cannot include methods.
3) Annotations are special marks in the code and cannot be used alone. They need to be used together with classes or interfaces.
4) Annotation types can be used to set metadata for program elements (program elements: classes, methods, member variables, etc.).
2. Case: Design the annotation type Testable, and the method identified by this annotation is a testable method. The annotation is empty internally, indicating that the annotation is an identification annotation.
public @interface Testable { }
public class Test { @Testable public void info() { System.out.println(“我是info方法”); } public void info1() { System.out.println(“我是info1方法”); } }
The @Testable annotation is added to this class to indicate that the info method is an executable method. It only describes that the method is an executable method and does not have any dynamic interaction capabilities. If you want To achieve the function of this annotation, a supporting Java application must be written. For specific code, please refer to the following code.
You can think about it, if we want to parse the internal structure of a class, what technology can we use to achieve it?
The answer is: reflection mechanism (for friends who are unclear about the reflection mechanism in the following paragraph, please follow the code below to debug. The specific knowledge about the reflection mechanism will be released later).
Common tool classes with reflection function in the java.lang.reflect package: Method (method class), Field (field class), Constructor (constructor method class), etc.
The above tool classes expand the ability to read runtime annotations, that is, implement the java.lang.annotation.AnnotatedElement interface; this interface is the parent interface of all program elements, and this interface provides functions for obtaining annotations information related methods.
getAnnotation(Class
annotationClass): Returns the annotation of the specified type on the program element. If the annotation of this type does not exist, returns null Annotation [] getAnnotations(): Returns all annotations that exist on the program element.
Annotation is the parent interface of all annotations. By default, any interface type implements this interface.
boolean isAnnotationPresent(Class Extends Annotation> annotationClass): Determines whether the program element contains annotations of the specified type. If it exists, it returns true, otherwise it returns false.
Code reference:
Parse the Test class and execute the method marked with @Testable.
import java.lang.reflect.Method; public class UseTest { public static void main(String[] args)throws Exception { Class c=Class.forName(“Test”); Object o=c.newInstance(); Method[] me=c.getDeclaredMethods(); for(Method temp:me) { if(temp.isAnnotationPresent(Testable.class)) temp.invoke(o,new Object[0]); } } }
Okay, now you can run the program to see the effect!
. . . . . . .
Isn’t it particularly speechless (ˉ▽ˉ;)..., by executing the code, you will find that the program has no results, which is different from what we thought? !
If you want to know what happened next time, please read the breakdown next time!
3. Summary:
Next, let us summarize the knowledge points that friends need to master.
1. The difference between XML and annotations
2. What are annotations
3. Grammar standards for annotation design
4. Reflection mechanism
5. Methods and functions of java.lang.annotation.AnnotationElement
4. Conclusion
Let me tell you the reason why I ended in a hurry. This is the first time When I write an article, I don’t know what the content format will be like. Please read the next article for the remaining relevant knowledge. Thank you for your support.
The above is the detailed content of Java annotations - Java's own configuration files. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.

Comments cannot be careless because they want to explain the reasons for the existence of the code rather than the functions, such as compatibility with old interfaces or third-party restrictions, otherwise people who read the code can only rely on guessing. The areas that must be commented include complex conditional judgments, special error handling logic, and temporary bypass restrictions. A more practical way to write comments is to select single-line comments or block comments based on the scene. Use document block comments to explain parameters and return values at the beginning of functions, classes, and files, and keep comments updated. For complex logic, you can add a line to the previous one to summarize the overall intention. At the same time, do not use comments to seal code, but use version control tools.

The key to writing good comments is to explain "why" rather than just "what was done" to improve the readability of the code. 1. Comments should explain logical reasons, such as considerations behind value selection or processing; 2. Use paragraph annotations for complex logic to summarize the overall idea of functions or algorithms; 3. Regularly maintain comments to ensure consistency with the code, avoid misleading, and delete outdated content if necessary; 4. Synchronously check comments when reviewing the code, and record public logic through documents to reduce the burden of code comments.

PHP has 8 variable types, commonly used include Integer, Float, String, Boolean, Array, Object, NULL and Resource. To view variable types, use the gettype() or is_type() series functions. PHP will automatically convert types, but it is recommended to use === to strictly compare the key logic. Manual conversion can be used for syntax such as (int), (string), etc., but be careful that information may be lost.

The key to writing PHP comments is clear, useful and concise. 1. Comments should explain the intention behind the code rather than just describing the code itself, such as explaining the logical purpose of complex conditional judgments; 2. Add comments to key scenarios such as magic values, old code compatibility, API interfaces, etc. to improve readability; 3. Avoid duplicate code content, keep it concise and specific, and use standard formats such as PHPDoc; 4. Comments should be updated synchronously with the code to ensure accuracy. Good comments should be thought from the perspective of others, reduce the cost of understanding, and become a code understanding navigation device.

PHP variables start with $, and the naming must follow rules, such as they cannot start with numbers and are case sensitive; the scope of the variable is divided into local, global and hyperglobal; global variables can be accessed using global, but it is recommended to pass them with parameters; mutable variables and reference assignments should be used with caution. Variables are the basis for storing data, and correctly mastering their rules and mechanisms is crucial to development.

The first step is to select the integrated environment package XAMPP or MAMP to build a local server; the second step is to select the appropriate PHP version according to the project needs and configure multiple version switching; the third step is to select VSCode or PhpStorm as the editor and debug with Xdebug; in addition, you need to install Composer, PHP_CodeSniffer, PHPUnit and other tools to assist in development.

Laravel's Storage facade provides a unified API to simplify file storage management. 1. The configuration driver sets disk type and parameters through filesystems.php and .env; 2. Common operations include uploading put, reading get, deleting delete, checking exists and generating urls; 3. When processing multiple files, you can use putFileAs and traversing directory files methods; 4. Notes cover disk selection, unique file name prevention, permission configuration and caching issues. For example, uploading avatars uses $path=$file->store('avatars','public') and creating soft links to ensure access, and batch uploads will traverse and process each
