JavaSE annotations

  • Starting with JDK5.0, Java added support for metadata (MetaData), which is Annotation
  • Annotation is actually a special mark in the code , these marks can be read at compile, class load, runtime, and perform the corresponding processing. By using Annotation , programmers can embed some supplementary information in the source file without changing the original logic. Code analysis tools, development tools and deployment tools can be verified or deployed through these supplementary information
  • Annotation can be used like a modifier. It can be used to modify the declaration of packages, classes, constructors, methods, member variables, parameters, and local variables . This information is stored in the "name = value" pair of Annotation .
  • JavaEE development model is basically based on annotations
  • To a certain extent, frame = annotation + reflection + design pattern

Common Annotation examples

Generate documentation related notes

  • @author: Identify the author who developed the module, use among multiple authors, and split
  • @version: indicate the version of the module
  • @see: Reference steering (related topics)
  • @since: from which version to increase
  • @param: Description of a parameter in the method
  • @return: Description of the method return value
  • @exception: Explain the exceptions that the method may throw. If the method does not explicitly throw exceptions with theows, you cannot write

Format check at compile time (three basic notes built into JDK)

  • @Override: restrict overriding parent class methods, this annotation can only be used for methods
  • @Deprecated: used to indicate that the modified element is outdated
  • @SupperssWarnings: suppress compiler warnings

Track code dependencies and implement alternative configuration file functions

  • @WebServlet: Annotations provided after Servlet3.0, you can not deploy Servlet in web.xml

  • @Transactional: Management of "transactions" in the Spring framework

    @Transactional(propagation=Propagation.REQUIRES_NEW,Isolation=Isolation.READ_COMMITTED,isolation=Islation.READ_COMMITTED,readOnly=false,timeout=3)
    

Junit unit testing

  • @Test: Marked on non-static methods, only methods marked with @Test can be used as test methods
  • @BeforeClass: Marked on a static method, only executed once, and executed when the class is initialized
  • @AfterClass: Marked on a static method, only executed once, and executed after all methods are completed
  • @Before: mark on non-static method, execute before @Test, execute before every @Test method
  • @After: Mark on non-static methods, execute after @Test, execute after every @Test method
  • @Ignore: Marked on the method that does not participate in the test this time

Note: The last five annotations are used with @Test, it is meaningless to use alone

Custom Annotation

  • Use the @interface keyword to define a new Annotation type

  • Custom annotations automatically inherit the java.lang.annotation.Annotation interface

  • Annotation member variables are declared in the Annotation definition as a parameterless method. The method name and return value define the name and type of the member. We call this configuration parameter. The type can only be an array of eight basic types, String type, Class type, enum type, Annotation type, all of the above types

  • You can specify the initial value of the Annotation member variable when you define it. You can use the default keyword to specify the initial value of the member variable

  • If only one member of parameters, it is recommended to use a parameter called value

  • If the defined annotation contains configuration parameters, then the parameter value must be specified when using

  • No members defined Annotation called marker Annotation, comprising a member variable called metadata Annotation

Note: Custom annotations must be accompanied by annotated information processing flow to make sense

Meta annotations in the JDK

JDK's Meta Annotation is used to modify other Annotation definitions!

JDK5.0 provides 4 standard meta-annotation types, namely:

  • Retention: used to modify an Annotation definition to specify the life cycle of the Annotation. @Rentention contains a member variable of type RetentionPolicy . When using @Rentention, you must specify a value for the value member variable
    • RetentionPolicy.SOURCE: effective in the source file, the compiler directly discards the comments of this strategy
    • RetentionPolicy.CLASS: valid in the class file, the JVM will not retain comments when running Java programs, the default value
    • RetentionPolicy.TUNTIME: effective at runtime, when running Java programs, the JVM will retain comments. The program can get the annotation through reflection
  • Target: used to modify the Annotation definition, used to specify which program elements can be modified by the modified Annotation, @Target also contains a member variable named value
    • CONSTRUCTOR: Describe the constructor
    • FIELD: description field
    • LOCAL_VARIABLE: describe local variables
    • METHOD: description method
    • PACKAGE: description package
    • PARAMETER: description parameter
    • TYPE: Describe the class, interface or enum declaration
  • Documented: Used to specify that the Annotation class modified by the meta Annotation will be extracted into a document by the javadoc tool. By default, javadovc does not include annotations
    • Annotations that define Documented must set the Retention value to RUNTIME
  • Inherited: is it modified Annotation will have inherited , if a class is @Inherited modified using the Annotation, then it will automatically have the subclass comment
    • Less application

Guess you like

Origin www.cnblogs.com/yfyyy/p/12736011.html