Understanding Annotations in Spring

Introduction

This is a series and it would make a lot of sense if read in a sequence. You can find all the posts under the heading “Spring Beginners to Professionals“. This is the third post of the series.

The next important concept is Annotations. Recent versions of Spring Framework shifted to annotation based programming. Prior to version 3.0 majority of Spring configuration was done using XMLs. This post is dedicated for understanding Annotations so that we can have a better understanding of Spring Framework.

Basics of Annotations

An annotation is a form of syntactic metadata that can be added to the source code. In a Java program an annotation can appear at defined syntactic locations. These locations are known as the target of the annotation. Few valid Target as per Java 8 are:

  • Type : If the annotation is marked with the target Type then it can appear for a class, interface or annotation declaration
  • Method : If the annotation is marked with the target Method, it can appear on any method declaration
  • Field : An annotation marked with target Field can appear on a filed declaration
  • Constructor : The target Constructor makes the annotation eligible to appear on a constructor declaration
  • Parameter : Annotations with target Parameter can appear in a method parameter declaration
  • Local Variable : Annotations having target Local Variable can appear on a local variable declaration within a code block.

There are few more target types which are not frequently used.  Java annotations can be embedded in class files generated by the compiler and can be retained by the virtual machine to be made retrievable at the run time.

Java annotations are often misunderstood as a supplement to java comments and java docs. Well, this is one of the use cases of annotations. Apart from this, annotations bring in bigger possibilities like automated processing and passing information to compiler and in some cases to the virtual machine.

The core Java library offers built in annotations you might be familiar with. Few of them are:

  • Override
  • Deprecated
  • FunctionalInterface
  • SuppressWarnings
  • SafeVarargs
  • Native
  • Transient
  • ConstructorProperties  and many more…

Meta Annotations

Let me describe it in the simplest way possible. Meta annotations are annotations, which are used to annotate another annotations! Java as of 1.8 declares 5 meta annotations which are:

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

These are really important annotations and they make other annotations possible. I would love to explain all these so that later while learning Spring annotations, we don’t end up getting confused.

The Target meta-annotation defines the target of the annotation on which it is applied. For e.g. If I created a Markable annotation and I define the Target to be method, then I can use the @Markable only on methods.

The Retention meta-annotation defines the scope of the annotation on which it is applied. It indicates how long the annotations are to be retained. For e.g. if I apply a Retention Policy named SOURCE, then my annotation will be discarded by the compiler, it will only remain in the source code. If I apply the Retention Policy CLASS, then my annotation will be retained in the class file post compilation. This is the default Retention Policy if nothing is provided. And if I apply the Retention Policy RUNTIME, then my annotation will be recorded by the compiler in the class file and retained by the JVM and we can retrieve them at run time using reflection.

The Inherited meta-annotation when applied on an annotation, indicates that the annotation is inherited to sub classes of the class on which it is applied. For e.g.: If I mark my annotation using the Inherited meta annotation and then apply this annotation to a Test class. Then all the sub classes of the Test class will inherit my annotation by default.

The Documented meta-annotation when applied on any annotation indicates that if the annotation appears on a Type(per se class or interface), then the type has to be documented by Javadoc or similar tools.

By default an annotation can only be applied once on the target. The Repeatable meta-annotation, removes this limitation and if an annotation is decorated with the Repeatable, then it can appear multiple times over their targets.

Summary

We learnt about the default annotations available in the JDK and also understood the importance of meta annotations. This is a really important concept in Spring and so I request you to please understand it completely before moving further. In case you do not find this easy to understand, please write down your queries in comments and I would be happy to explain in more detail.