In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to understand Java comments". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Thinking about annotations caused by @ Autowired
When we write Spring applications, we will find that @ Autowired is used to mark member variables, methods, and constructors to achieve automatic assembly. If you click @ Autowired to look at the source code in IDE, you will find that there are more things that start with @. This is the focus of our talk today-- Java notes!
Target ({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE}) @ Retention (RetentionPolicy.RUNTIME) @ Documented public @ interface Autowired {/ * Declares whether the annotated dependency is required. *
Defaults to {@ code true}. * / boolean required () default true;} Java Notes
Java annotations (Annotation), also known as Java annotations, is an annotation mechanism introduced by JDK5.0. Annotation is an auxiliary class, which is widely used in Junit, Struts, Spring and other tool frameworks. Classes, methods, variables, parameters, and packages in the Java language can all be annotated. Unlike Javadoc, Java dimensions can obtain the content of dimensions through reflection. Annotations can be embedded in bytecode when the compiler generates class files. The Java virtual machine can retain the annotation content, which can be obtained at run time. Of course, it also supports custom Java annotations.
Java defines a set of built-in annotations, including:
Override-check to see if the method is overridden. If the parent class is found, or if the method is not found in the referenced interface, a compilation error is reported.
@ Deprecated-Mark obsolete methods. If you use this method, a compilation warning is reported.
SuppressWarnings-instructs the compiler to ignore the warnings declared in the comments.
Annotations (or meta-annotations) that act on other annotations are:
Retention-identifies how this annotation is saved, whether it is only in code, in a class file, or can be accessed through reflection at run time. These three lifecycles correspond to: Java source files (.java files)-- > .class files-- > bytecode in memory.
1. RetentionPolicy.SOURCE: the comments are retained only in the source file, and are discarded when the Java file is compiled into a class file
2. RetentionPolicy.CLASS: comments are retained to the class file, but jvm is discarded when loading the class file, which is the default life cycle
3. RetentionPolicy.RUNTIME: the comment is not only saved to the class file, but still exists after jvm loads the class file.
Documented-Mark whether these annotations are included in the user document.
Target-marks what kind of Java member this annotation is intended for.
@ Target (ElementType.TYPE)-- interfaces, classes, enumerations, comments
@ Target (ElementType.FIELD)-- constants of fields and enumerations
@ Target (ElementType.METHOD)-- method
@ Target (ElementType.PARAMETER)-- method parameter
@ Target (ElementType.CONSTRUCTOR)-- Constructor
@ Target (ElementType.LOCAL_VARIABLE)-- Local variable
@ Target (ElementType.ANNOTATION_TYPE)-comments
@ Target (ElementType.PACKAGE)-- package
@ Inherited-Mark which annotation class this annotation is inherited from (the default annotation does not inherit from any subclass)
Starting with Java 7, three additional annotations have been added:
@ SafeVarargs-Java 7 begins to support, ignoring any warnings generated by method or constructor calls that use generic variables.
@ FunctionalInterface-Java 8 begins support, identifying an anonymous function or functional interface.
@ Repeatable-Java 8 is supported, indicating that an annotation can be used multiple times on the same declaration.
Custom annotation development
Next, let's learn how to develop custom annotations.
Define an annotation using the @ interface method
Import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.annotation.ElementType; import java.lang.annotation.RetentionPolicy; @ Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.METHOD) public @ interface MyTag {String name (); / / define the variable name} in the comments
Write a class and apply @ MyTag to the method
Public class TestTag {@ MyTag (name= "case1") public void login () {System.out.println ("login");} @ MyTag (name= "case2") public void info () {System.out.println ("info");} @ MyTag (name= "case3") public void logout () {System.out.println ("logout");}}
Write a test class to get the name value in @ MyTag in the login method of the TestTag class. We talked a lot about how to define annotations and where to put them. Now we can start to learn how to extract annotation attributes. This is the key to using annotations. Getting the value of attributes is the purpose of using annotations. To get the annotation properties, you need to use reflection technology (I will focus on reflection in the following article), the code is as follows:
Public static void main (String [] args) throws NoSuchMethodException, SecurityException, ClassNotFoundException {/ / load the class and instantiate it. Class tag = Class.forName ("com.my.demo.TestTag"); / / call the login method (which must be of type public) Method m=tag.getMethod ("login"); / / return the comment MyTag name=m.getAnnotation (MyTag.class) of type MyTag; / / output the name of the comment System.out.println ("value is:" + name.name ());}
Run the code and the result is as follows:
Value is: this is the end of case1's "how to understand Java Notes". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.