basic usage of java annotation and reflection

less than 1 minute read

Spring Annotation JAXB Annotation

Java Annotation

  • started java version 5
  • mapping of java attrivutes
  • good to use AOP (Aspect Oriented Programming)
  • can be used run-time and compile-time
    • java reflection enables to use in runtime environment

Built-in Annotation

  • @Override
  • @Deprecated
  • @SuppressWarnings
  • @SafeVarargs
  • @FunctionalInterface

Meta Annotaions

  • @Retention
  • @Documented
  • @Target
  • @Inherited
  • @Repeatable

Annotion Usage : @Retention

  • RetentionPolicy
    1. SOURCE : just comment, Not included in the source (ex: @Override, @SupressWarnings)
    2. CLASS : included when compiling, ignored in VM
    3. RUNTIME : included when compiling, used in VM (used by java reflection)
  • examples
    • @Retention(RetentionPolicy.RUNTIME)

Annotation Ragne : @Target

  • ElementType
    1. TYPE
    2. FILED
    3. METHOD
    4. PARAMETER
    5. CONSTRUCTOR
    6. LOCAL_VARIABLE
    7. ANNOTATION_TYPE
    8. PACKAGE
  • examples
    • @Target(ElemenyType.FIELD)
    • @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})

@interface

Java Reflection

  • find the attributes of class objects

Spring Framework Annotation

Dependency Injection

  • @Required
  • @Autowired
  • @Inject

Controller

  • @Controller
  • @RestController
  • @RequestMapping
  • @PathVariable
  • @RequestBody
  • @RequestParam
  • @ResponseBody

Data Access

  • @Service
  • @Repository

Spring Boot Annotation

Tags:

Categories:

Updated:

Leave a comment