What is Custom Annotation in Java?
Custom annotation in Java is a user-defined annotation created by developers to provide custom metadata for classes, methods, fields, constructors, or parameters.
In simple words:
Custom annotations allow developers to create their own special annotations for framework processing, validation, security, configuration, or business rules.
Why Custom Annotations are Important?
Custom annotations are widely used for:
- Framework development
- Validation rules
- Security checks
- Logging systems
- Role-based access
- Transaction management
- Code generation
- Automation
Custom Annotation Overview Diagram
Developer Creates Annotation
|
v
Annotation Applied to Code
|
v
Reflection/API Detects Annotation
|
v
Custom Logic Executed
How to Create Custom Annotation?
Use:
@interface
Basic Syntax
@interface AnnotationName {
}
Simple Custom Annotation Example
@interface Author {
String name();
}
Usage Example
@Author(name = "Naresh")
class Employee {
}
What Happens Internally?
- Annotation metadata stored in class
- Reflection API reads annotation
- Framework applies custom logic
Custom Annotation Processing Flow
Custom Annotation Added
|
v
Compiler Stores Metadata
|
v
JVM Loads Metadata
|
v
Reflection Reads Annotation
|
v
Custom Logic Applied
Can Custom Annotations Have Variables?
Yes.
Annotation members behave like methods.
Example
@interface EmployeeInfo {
int id();
String department();
}
Usage
@EmployeeInfo(
id = 101,
department = "IT"
)
class Employee {
}
Annotation Member Rules
- No method body
- No parameters
- No throws clause
- Only constant/default values allowed
Allowed Annotation Member Types
- Primitive types
- String
- Class
- Enum
- Annotation
- Arrays
Default Values in Annotations
@interface Author {
String name()
default "Unknown";
}
Usage
@Author
class Employee {
}
Output
Default value becomes:
Unknown
Important Meta Annotations
Custom annotations usually use meta annotations.
Common Meta Annotations
- @Target
- @Retention
- @Inherited
- @Documented
- @Repeatable
@Target Annotation
Defines where annotation can be used.
Example
@Target(ElementType.METHOD)
Meaning
Annotation can only be applied to methods.
@Retention Annotation
Defines how long annotation exists.
Retention Policies
| Policy | Meaning |
|---|---|
| SOURCE | Available only in source code |
| CLASS | Stored in .class file |
| RUNTIME | Available during runtime |
Most Common Retention
@Retention(
RetentionPolicy.RUNTIME
)
Why Runtime Retention Important?
Because Reflection API can access annotations during runtime.
Complete Custom Annotation Example
import java.lang.annotation.*;
@Retention(
RetentionPolicy.RUNTIME
)
@Target(ElementType.TYPE)
@interface Author {
String name();
}
Using the Annotation
@Author(name = "Naresh")
class Employee {
}
Reading Custom Annotation Using Reflection
Class> c =
Employee.class;
Author author =
c.getAnnotation(
Author.class
);
System.out.println(
author.name()
);
Reflection Processing Flow
Class Loaded
|
v
Reflection Reads Annotation
|
v
Annotation Metadata Retrieved
|
v
Custom Logic Executed
Custom Annotation for Validation
@interface NotNull {
}
Possible Use
Framework validates field before saving data.
Validation Flow
DTO Object Received
|
v
Reflection Detects @NotNull
|
v
Validation Performed
|
v
Error or Success Returned
Custom Annotation in Spring Boot
Spring Boot heavily uses custom annotations.
Examples
- @RestController
- @Service
- @Autowired
- @RequestMapping
- @Transactional
Spring Boot Internal Flow
Spring Starts
|
v
Classpath Scanned
|
v
Annotations Detected
|
v
Reflection Processes Metadata
|
v
Beans Created Automatically
Custom Security Annotation Example
@Retention(
RetentionPolicy.RUNTIME
)
@Target(ElementType.METHOD)
@interface AdminOnly {
}
Usage
@AdminOnly
public void deleteUser() {
}
What Framework Can Do?
- Check logged-in user role
- Allow only admin access
- Block unauthorized users
Security Processing Flow
Request Received
|
v
Reflection Detects @AdminOnly
|
v
Role Validation Performed
|
v
Access Granted or Denied
Custom Annotation in Hibernate/JPA
Hibernate uses annotations for ORM mapping.
Example
@Entity
@Table(name = "employees")
class Employee {
@Id
int id;
}
What Happens?
- Reflection scans annotations
- Database mapping generated
- SQL queries created dynamically
Hibernate Flow
@Entity Found
|
v
Reflection Reads Metadata
|
v
ORM Mapping Created
Custom Annotation in JUnit
JUnit uses annotations for test execution.
Example
@Test
public void loginTest() {
}
JUnit Flow
@Test Detected
|
v
Reflection Invokes Test Method
Difference Between Built-in and Custom Annotation
| Feature | Built-in Annotation | Custom Annotation |
|---|---|---|
| Created By | Java | Developer |
| Purpose | General Metadata | Application-Specific Logic |
| Examples | @Override | @AdminOnly |
Advantages of Custom Annotations
- Improves code readability
- Supports automation
- Reduces boilerplate code
- Framework-friendly
- Enables reusable processing logic
Disadvantages of Custom Annotations
- Overuse reduces readability
- May increase framework complexity
- Reflection processing affects performance
Custom Annotations in Banking Systems
Banking applications use custom annotations for:
- Security validation
- Transaction auditing
- Role-based access
- Fraud detection rules
Banking Example
@SecureTransaction
public void transferMoney() {
}
What Framework Can Do?
- Audit transaction
- Validate user permissions
- Apply fraud checks
Custom Annotations in E-Commerce Systems
E-commerce systems use annotations for:
- Payment security
- Discount validation
- Inventory rules
- API monitoring
Custom Annotations in Microservices
Microservices architectures use annotations for:
- Distributed tracing
- Service monitoring
- Logging
- Security
- Configuration management
Microservice Flow
@Traceable Added
|
v
Framework Detects Annotation
|
v
Distributed Logging Enabled
Custom Annotations and JVM
Annotations are stored as metadata inside class files and processed using Reflection API.
JVM Architecture
Java Source Code
|
v
Annotation Metadata Compiled
|
v
JVM Loads Metadata
|
v
Reflection API Reads Metadata
Common Interview Mistake
Many developers think annotations directly execute code.
Actually:
- Frameworks or reflection APIs process annotations and apply logic.
Another Common Mistake
Many developers think annotations replace business logic.
Actually:
- Annotations only provide metadata.
Best Practices
- Use meaningful annotation names
- Keep annotation logic simple
- Use runtime retention only when needed
- Avoid excessive custom annotations
- Document annotation behavior clearly
Realtime Enterprise Example
Secure Banking Transaction
@SecureTransaction Applied
|
v
Reflection Detects Annotation
|
v
Security Validation Triggered
|
v
Transaction Executed Safely
Related Learning Topics
- What is Annotation in Java
- What are Marker Annotations in Java
- What is Reflection API in Java
- What is Java Reflection Used For
- How JVM Works Internally
- What is JPA in Java
- Memory Management in Java
- What is Spring Boot
- What are Microservices
Professional Interview Answer
Custom annotation in Java is a user-defined annotation created using the @interface keyword to provide application-specific metadata for classes, methods, fields, constructors, or parameters. Custom annotations are heavily used in enterprise applications, Spring Boot frameworks, Hibernate ORM systems, banking platforms, cloud-native microservices, security frameworks, and validation engines for dependency injection, transaction management, logging, authorization, auditing, monitoring, and runtime automation. Annotations themselves do not execute logic directly; instead, Reflection API or frameworks process annotation metadata and apply behavior dynamically during runtime or compile time. Custom annotations improve code readability, reduce boilerplate code, and enable flexible framework-driven architecture in large-scale enterprise systems.
Frequently Asked Questions
What is custom annotation in Java?
A custom annotation is a user-defined annotation created using @interface.
How do we create custom annotation?
Using the @interface keyword.
Can custom annotations have variables?
Yes, annotation members can store metadata values.
How are custom annotations processed?
Using Reflection API or framework-level annotation processing.
Which frameworks heavily use custom annotations?
Spring Boot, Hibernate, and JUnit heavily use custom annotations.