Annotations

  • Annotations help us in determining basic errors before hand before compilation.
    • For example if we misspell an overridden method then in that case we can check it before compilation only if it has @override annotation above it.
    • In Spring framework many issues can be predicted in advanced i.e. before application compiler.
    • Before annotations we used to do all configurations in Spring framework in the xml configuration file.
      • for example @bean/@component annotation directly class can be used as a bean. 
  • We can create a custom annotation using @interface MyAnnotation {}
  • Important meta annotation properties are
    • @Target
      • This annotation meta tells where we can apply the particular annotation on method on class or constructor, et cetera
      • It has following properties
        • ElementType.Constructor
        • ElementType.Method
        • ElementType.Parameter
        • ElementType.Field
    • @Retention
      • This annotation meta Tells how the particular annotation will be stored in Java.
      • It has following values
        • RetentionPolicy.Source
          • Annotations will be discarded by compiler itself and not even recorded in class file.
        • RetentionPolicy.Class
          • Annotation will be recorded in class file, but ignored by JVM during runtime.
        • RetentionPolicy.Runtime
          • Annotation will be recorded in class file and also available during runtime.
  • Return type Of the custom annotation is Restricted to
    • Primitive type(type,boolean,double)
    • String
    • Enum
    • Class<?>
    • Annotation
    • Array of above types.

No comments:

Post a Comment