Java Annotations in Spring Boot: Enhancing Projects with Custom Annotations
Java Annotation is a tag that represents a form of metadata that provides data about a program but are not part of the program itself. It can be attached to various Java elements like classes, methods, fields, parameters, etc., and are used to provide information about those elements to the compiler or runtime environment.
Afridi Rahman · · 4 min read

Java Annotation is a tag that represents a form of metadata that provides data about a program but are not part of the program itself. It can be attached to various Java elements like classes, methods, fields, parameters, etc., and are used to provide information about those elements to the compiler or runtime environment. Annotations start with the @ symbol followed by the annotation type. Spring Boot Annotations are a subset of Java annotations. While both types of annotations are written in Java syntax and serve similar purposes of providing metadata or instructions to the code, Spring Boot annotations are specific to the Spring Boot framework and are used to configure various aspects of Spring Boot applications. In this article, we will discuss the following concepts.
- What are the roles of Annotation in Spring Boot ?
- What is Custom Annotation in Spring Boot ?
- Benefits of having Custom Annotations.
- Creating a Custom Annotation in Spring Boot
What are the roles of Annotations in Spring Boot ?
In Spring Boot, annotations play a crucial role in configuring and managing various aspects of your application. These annotations are used to simplify the configuration process and reduce the amount of boilerplate code you need to write. Here are some key annotations commonly used in Spring Boot Java applications:
1. Component :
The @Component annotation is one of the core annotations in the Spring Framework and is used to declare a class as a Spring-managed component. In other words, it marks a Java class as a bean that Spring should manage, instantiate, and inject into other parts of the application.
Here’s a simple example that demonstrates the power of the @Component annotation:
@Component
public class ComponentExample {
public void doSomething() {
// write your logic
}
}
@Service
public class ServiceExample {
private final ComponentExample componentExample;
public void performAction() {
componentExample.doSomething();
}
}
The @Component annotation also serves as the foundation for more specialized annotations within Spring, including @Service, @Repository, and @Controller. These specialized annotations offer extra meaning and features designed to address particular scenarios.
2. Value :
The @Value annotation is an important tool utilized to inject values into your beans from diverse origins. These origins include properties files, environmental variables, and command line arguments.
For example, imagine you have the following content in your properties file named “application.properties”:
myapp.username=John
myapp.password=abcd123
then with @Value annotation, you can inject those values into your bean:
@Service
public class MyServiceClass {
@Value("${myapp.username}")
private String username;
@Value("${myapp.password}")
private String password;
}
to learn more about spring boot annotations please visit the below article : Spring Boot Annotations
What is Custom Annotation in Spring Boot ?
In Spring Boot, a custom annotation refers to an annotation that you define yourself for specific purposes within your application. These annotations are created by the developers to mark classes, methods, fields, or other program elements with metadata that serves a particular function or conveys additional information to the Spring framework or other components of your application.
Benefits of having Custom Annotations
Custom annotations help abstract away complex or repetitive configuration logic. Instead of repeatedly writing boilerplate code, developers can encapsulate common functionality into custom annotations, making the codebase more concise and easier to maintain. Also by creating custom annotations with descriptive names, developers can improve the readability of their code. Annotations serve as self-documenting markers, conveying the purpose or intention behind certain elements of the codebase. Custom annotations also promote consistency across the codebase by enforcing a standard approach to implementing specific behaviors or configurations. Developers can define conventions within the organization and ensure adherence through the use of custom annotations.
Creating a Custom Annotation in Spring Boot
Creating a custom annotation in Spring Boot involves defining a new annotation interface and then using it within your application code. Here’s a step-by-step guide on how to create a custom annotation in Spring Boot:
Step 1: Define the Annotation
Create a new Java file for your custom annotation. Annotations in Java are defined using the @interface keyword.
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CustomAnnotation {
// You can define elements within your annotation if needed
String value() default "";
}
Step 2: Use the Annotation
You can now use your custom annotation in your Spring Boot application. For example, you can annotate classes, methods, or fields with your custom annotation.
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@GetMapping("/")
@CustomAnnotation("This is a custom annotation")
public String hello() {
return "Hello, World!";
}
}
Step 3: Process the Annotation
Create an AspectJ aspect class that will define the behavior for your custom annotation.
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class CustomAnnotationAspect {
@Before("@target(customAnnotation) || @annotation(customAnnotation)")
public void beforeExecution(CustomAnnotation customAnnotation) {
System.out.println("Executing method with CustomAnnotation: " + customAnnotation.value());
}
}
With this setup, whenever a method annotated with @CustomAnnotation is invoked, the advice defined in your aspect class CustomAnnotationAspect will be executed before the method execution, printing out the value specified in the annotation.
Thank you for reading this article. Happy coding, Cheers !!😊😊
Was this useful? A like tells me what to write more of.
Comments (0)