How to Build Custom Java Annotations

What is a Java Annotation?

An annotation is a pure Metadata for a piece of code in the form of decorators. It provides data about a program that is not part of the program itself.

Join a Global Telecom Software Leader – We’re hiring!

It can be meaningful to visualize an annotation as a sort of tag’s that we insert into the source code to provide extra information about the code. Annotations are a form of metadata, despite others kinds.

What is Metadata for you?

It could be anything. There are many available types of general annotations, such as:

  • Compiler Information
  • RunTime processing
  • Deployment time
  • Compilation time

For example, as a Compiler information type, we have three Built-in Annotations(@Override, @SuppressWarnings, @Deprecated). These bring information from the programmer to the compiler.

Another important distinction is that we can annotate a Class, a Method, a Field, a Variable, a Parameter and a Package. This means annotations are intrinsically and indirectly related to Java scopes. Those types are defined in the ElementType type element.

Annotations can be used in different contexts, as a way to make better code, more maintainable, concise and intelligible.

For example, you could generate a virtual set of related annotations to validate or check guidelines acquisition. You could also generate code that depends on conditions (context parameters) checked, for instance, in deployment or runtime to the system.

It is interesting to mention that annotations are great for IDE’s, as a way to enlighten the internal dynamism and forces interacting within software components.

How To Build A Custom Java Annotations

As an example, we are going to code a custom annotation called CustomAnnotation.

An annotation definition resembles the definition of a standard interface. The exception is when the interface keyword is preceded by the @ character, as in the code below.

A few comments about the lines of code showed above:


A retention policy determines at what point annotation should be discarded. Java defined 3 types of retention policies through java.lang.annotation.RetentionPolicy enumeration. It has SOURCE, CLASS and RUNTIME.

The retention policy is specified by using java built-in annotation @Retention. We have to pass the retention policy type, as we did in the source code section above.

@Target(value=ANNOTATION_TYPE) >> Indicates the kinds of program element to which an annotation type is applicable.

Below, we are using the recently created annotation (in the middle of others)

Usually, annotations are processed by Java reflection. In normal conditions, java reflection is not used by the standard programmer. It is often useful to processing Metadata. Most times we tend to be the users rather than the creators of annotationsIf you examine the source code, you will realize that we are not setting the enabled parameter. We can ignore it because the value is provided by default.

Spring Boot basic Annotations

Sprint is a Java application framework based on annotations, some of them are:

@SpringBootApplication – enables Spring Boot auto configuration and component scanning. These two fundamental design principles about Spring Boot, are intrinsically related to “keep it simple,” the top philosophy concept.

@Configuration – indicates that the Spring IoC container can use the class as a source of bean definitions.

@Component – is the most generic Spring annotation. A Java class decorated with @Component is found during classpath scanning and registered in the context as a Spring bean.

@Service, @Controller, @Repository – are specializations of @Component for more specific use cases.

@Bean – servers a similar purpose as @Component. It is not autodetected.

@Autowired– we inject the spring bean into the target field.

There are a vast amount of Annotations in the Java ecosystem that you should probably cross daily with.  Here we showed you just a few of them.

Like this content? We’re looking for you, come work with us!

You may also like

Telecommunications, fertile ground for FPGAs

system modeling

System modeling in the software development


Kubernetes in a Cloud Development Environment