Comprehensive Analysis of Java annotations (learning notes), comprehensive analysis of java

Source: Internet
Author: User

Comprehensive Analysis of Java annotations (learning notes), comprehensive analysis of java
Chapter 1 Overview

Java provides a way and method for the elements in the original program to associate any information and any metadata.
Chapter 2 common annotations in Java 2-1 Annotations in JDK
  • @ Override
  • @ Deprecated
  • @ Suppvisewarnings ("deprecation ")
2-2 Java third-party annotation Spring
  • @ Autowired
  • @ Service
  • @ Repository
Mybatis
  • @ InsertProvider
  • @ UpdateProvider
  • @ Options
Chapter 3 Classification of annotations 3-1 Classification of Java annotations by runtime mechanism (lifecycle)
  • Source code annotation: annotation only exists in the source code, and the. class file compiled does not exist.
  • Annotation during compilation: The annotation exists in both the source code and the. class file. (JDK annotation)
  • Runtime annotation: annotations that work in the runtime stage and even affect the runtime logic. (@ Autowired)
Score by source
  • Annotation from JDK
  • Annotations from third parties
  • Annotation defined by ourselves
  • Meta annotation (annotation)
Chapter 4 custom annotations 4-1 Java custom annotation syntax requirements
@Target({ElementType.METHOD,ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Inherited@Documentedpublic @interface Description{    String desc();    String author();    int age() default 18;}
  • Use the @ interface keyword to define the Annotation
  • The Member is declared as a member without any parameters or exceptions.
  • You can use default to specify a default value for a member.
  • The member types are restricted. Valid types include the original type and String, Class, Annotation, and Enumeration.
  • If the annotation has only one member, the member name must be named value (). You can ignore the member name and value assignment number (=) during use)
  • Annotation classes can have no members. Annotations without members are called Identity annotations.
Meta annotation @ Target ({ElementType. METHOD, ElementType. TYPE })
  • CONSTRUCTOR: CONSTRUCTOR
  • FIELD: FIELD Declaration
  • LOCAL_VARIABLE: local variable Declaration
  • METHOD: METHOD declaration
  • PACKAGE: PACKAGE Declaration
  • PARAMETER: PARAMETER Declaration
  • TYPE: Class, Interface
@ Retention (RetentionPolicy. RUNTIME)
  • SOURCE
  • CLASS
  • RUNTIME
@ Inherited
Subclass inheritance is allowed. Invalid interface inheritance and valid class inheritance. And only the Annotation on the integrated class will not inherit the Annotation on the method of this class.
@ Brief ented
The generated javadoc will contain annotations.
4-2 use a custom annotation to use the annotation syntax
@ <Annotation Name> (<member name 1 >=< member value 1>, <member name 2 >=< member value 2> ,...)
@Description(desc="I am eyeColor", author="Mooc boy", age=18)public String eyeColor(){    return "red";}
4-3 code demonstration of custom annotations
4-2.
4-4 parse Annotation
Public static void main (String [] args) {// 1. use the Class loader to load the Class try {Class c = Class. forName ("com. imooc. annotation. child "); // 2. find the annotation boolean isExist = c. isAnnotationPresent (Description. class); if (isExist) {// 3. get the annotation instance Description d = (Description) c. getAnnotation (Description. class); System. out. println (d. value ();} // 4. locate the annotation Method [] MS = c. getMethods (); for (Method m: MS) {boolean isMExist = m. isAnnotationPresent (Description. class); if (isMExist) {Description d = (Description) m. getAnnotation (Description. class); System. out. println (d. value () ;}// another Parsing Method for (Method m: MS) {Annotation [] as = m. getAnnotations (); for (Annotation a: as) {if (a instanceof Description) {Description d = (Description) a; System. out. println (d. value () ;}}} catch (ClassNotFoundException e) {e. printStackTrace ();}}
Chapter 5 project practice 5-1 project requirement Project Description
The project is taken from the persistence layer architecture of a company and used to replace the Hibernate solution. The core code is implemented through annotations.
Requirement 5-2 project implementation (I)
For the code, see MOOC.
5-3 project implementation (II)
For the code, see MOOC.
Chapter 6 course Summary

Link: http://www.imooc.com/learn/456
Translated from MOOC.

Top
0
Step on
0
View comments

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.