Core Java Tutorials


Introduction

Java Hello World

Java Compilation

JDK New Features

JVM Architecture

Java OOPs Concepts

Java Package

Array

Exception Handling

String

Immutable Class

File Handling

Serialization

MultiThreading

Volatile

Concurrent Package

Collection

Collection Internal

Generics

Cloning

Reflection In Java

Annotation

Class Loader

Java Inner classes

Garbage Collector

JDBC

References In Java

Heap Dump & Thread Dump

/java/java-annotationsPrevious             Next

Java Annotations

java-annotations Java annotations are one of the very important features of core java which are used to associate meta data with your Java code and these meta data with annotations can be processed at compile time, deployment time and runtime time as per the need.
it does not play any role in business logic of the java code and simply associate meta data which can be processed for following purposes:-
 Compiler instructions
 Build-time or Deployment time instructions
 Runtime instructions

Java annotations were added to Java since Java 5 (Tiger).


Compiler instructions:-

Java has three built-in annotations which are used to give instructions to the Java compiler. These annotations are:
 @Deprecated
 @Override
 @SuppressWarnings

Build-time or Deployment time instructions:-

These annotation are processed at the deployment time or build time.

Runtime instructions:-

these annotations are processed at the runtime and it is used in many popular frameworks like Spring ,Hibernate ,JSF, JAX-WS, JAX-WS , JSF 2.x etc. and some annotations are :-
 Spring Annotations - @Service ,@Autowired , @Controller, @PostConstruct etc.
 Hibernate Annotations- @Entity , @Table , @Column, @Id, @OneToOne etc
 JAX-RS Annotations- @Path, @Consumes, @Produces , @QueryParam, @FormParam etc.


 Structure or Definition of a Annotation


 @Target Annotation

 @Retention Annotation


 Multi annotation
As we know that Java allows us to define our own annotation. The annotations which are used to define costume annotations are known as "Meta Annotation".
There are four annotation types in the "java.lang.annotation" package. These so-called meta-annotations are used to annotate other annotation types.


 Annotation Types
 Marker Annotations
The Annotation which does not have any attributes is known as marker annotation.
Example :- @Override , @ Remote, @Aspect , @RequestBody, @ResponseBody etc.

 Single Value Annotations
The Annotation which has only one attribute is known as Single Value annotation.
Example :- @Autowired, @ Qaulifier,@Inject etc.

 Multi Values Annotations
The Annotation which has more that one attribute is known as Multi Values annotation.
Example :- @Entity, @ Table, @Cacheable, @CacheEvict, @Column, @Scope, @RequestMapping etc.


 Define and Process a Custom Annotation in Java