Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
Ravi Vishwakarma
24-Apr-2025Once you've got defined and carried out custom annotations, you may use Java reflection to system them at runtime. This is especially useful in frameworks where you scan metadata to drive behavior (for example, dependency injection, configuration, validation).
1. Custom Annotation Example
Let's say we have a custom annotation for marking methods to be executed:
2. Class with Annotated Methods
3. Processing Annotations with Reflection
You can now write a class that scans for methods with
@RunMe
and invokes them.Output:
4. Key Reflection APIs Used
Class.getDeclaredMethods()
method.isAnnotationPresent()
method.getAnnotation()
method.invoke(obj)
5. Use Cases in Real Projects
@Test
to identify test methods.@Autowired
,@RequestMapping
and processes them at runtime.@Entity
,@Table
, etc., to map Java classes to database tables.