In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the grammatical standard of Java annotation type". In daily operation, I believe that many people have doubts about what the grammatical standard of Java annotation type is. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "what is the grammatical standard of Java annotation type?" Next, please follow the editor to study!
Preface
XML is loved by most programmers as a configuration file, but some people prefer to use annotations. In fact, I feel that what to choose is not the key point, but the key point is to understand the nature of the birth of each technology. XML as a configuration file and code is a "loosely coupled" code description, but it is difficult to manage when there are too many XML configuration files. At the same time, IDE can not verify the correctness of the XML configuration file, increasing the difficulty of testing. Annotations, on the other hand, are "tightly coupled" code descriptions, which are designed to make the application easier to extend while achieving "zero" configuration.
1. What is Annotation
Annotations are the metadata in the code (metadata: data that describes the data). Through the use of annotations, program developers can embed some supplementary information in the source file without changing the original logic. Look at the following code snippet:
For beginners, they often see similar code and wonder what the heck @ Override is. In fact, it is an annotation, adding @ Override to the toString () method, indicating that the toString () method below the annotation must reconstruct the parent method.
Seeing this, I think someone will think that I will introduce all kinds of notes to you next. I won't!
II. Grammatical standards of annotation types
Annotations are a special type of Java, so let's take a look at how to design an annotation type.
1. Grammar standards:
Public @ interface Annotation Type name
{
[data type variable name () [default initial value];]
}
Note:
1) the content in "[]" is optional. If the inside of the comment is empty, it means that the current comment is an identification comment.
2) the annotation contains variables intelligently, but not methods.
3) annotations are special tags in the code, which cannot be used alone, but need to be used 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 annotation type Testable, and the method identified by this annotation is testable. The inside of the comment is empty, indicating that the comment is an identifying comment.
Public @ interface Testable
{
}
Public class Test
{
@ Testable
Public void info ()
{
System.out.println ("I am the info method")
}
Public void info1 ()
{
System.out.println ("I am the info1 method")
}
}
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 executable and does not have any dynamic interaction ability. If you want to achieve the role of this annotation, you must write a supporting Java application. Please refer to the following code. You can think, if you want to parse the internal structure of a class, what technology can we use to achieve it? The answer is: reflection mechanism (for those friends who are not clear about the reflection mechanism in the next paragraph, follow the following code to debug, and the specific knowledge of the reflection mechanism will be released later).
Common tool classes with reflection function in java.lang.reflect package: Method (method class), Field (field class), Constructor (constructor class), and so on. These utility classes extend the ability to read run-time comments, that is, to implement the java.lang.annotation.AnnotatedElement interface, which is the parent interface to all program elements and provides methods for obtaining annotation information.
GetAnnotation (Class annotationClass): returns a comment of the specified type on the program element, or null if the comment of that type does not exist
Annotation [] getAnnotations (): returns all comments that exist on the program element.
Annotation is the parent interface of all annotations, which is implemented by default for any interface type.
Boolean isAnnotationPresent (Class
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.