Spring aliasFor for Annotations with TargetPARAMETER
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Spring's @AliasFor is not limited to type-level annotations. It also works on annotations that target method parameters, but there is an important nuance: aliasing only helps when Spring or your own code reads that annotation through Spring's merged-annotation model.
What @AliasFor Actually Does
@AliasFor declares that two annotation attributes represent the same underlying setting. That gives callers a more ergonomic API and helps when one annotation attribute is meant to mirror another.
A parameter annotation can use the same mechanism as any other annotation:
With that declaration, @CurrentUser("id") and @CurrentUser(name = "id") are equivalent from Spring's point of view.
Reading Aliases from a Parameter
The alias does not become useful by magic. Something still has to inspect the parameter annotation and resolve the merged view. A common example is a custom Spring MVC argument resolver.
Using MergedAnnotations is the important part. If you read raw reflection values naively, you can miss the alias semantics that Spring provides.
A controller can then use either attribute name:
When @AliasFor Helps on Parameters
The main value is API design. Many parameter annotations want a short unnamed attribute for the common case, plus a descriptive named attribute for clarity. value and name are a typical pair.
This pattern is especially useful when you are building:
- custom MVC argument annotations
- validation or mapping annotations processed by Spring infrastructure
- meta-annotations that mirror attributes from another Spring annotation
If you are only using plain Java reflection and never pass through Spring's annotation utilities, @AliasFor adds no benefit. The annotation compiles, but nothing resolves the alias relationship for you.
Meta-Annotation Use Is Different
Spring also supports @AliasFor(annotation = SomeAnnotation.class, attribute = "value") for composed annotations. That is a related but separate use case. On parameter annotations, you can use the same pattern if you are composing another annotation and need to expose one of its attributes under a new name.
The important rule is consistency:
- aliases must point at each other
- both attributes should have the same return type
- both should usually declare the same default value
If those conditions do not match, Spring will treat the annotation as misconfigured.
Common Pitfalls
The biggest mistake is expecting @AliasFor to change how Java annotations work by themselves. It is a Spring feature layered on top of Java metadata, so it only matters when Spring code reads the annotation in an alias-aware way.
Another common problem is asymmetric configuration. If value aliases name but name does not alias value, Spring will reject the setup. The contract must be two-way and internally consistent.
Developers also sometimes use aliases to paper over unclear API design. If the annotation has three or four names for the same concept, readability gets worse instead of better. Usually one short alias pair is enough.
Finally, be careful with examples copied from type-level annotations like @RequestMapping. Those patterns are valid, but parameter processing is different because your resolver or framework integration has to inspect the method parameter itself.
Summary
- '
@AliasForworks onElementType.PARAMETERannotations.' - The alias is only meaningful when Spring reads the annotation through merged-annotation support.
- Use
MergedAnnotationsor other Spring utilities instead of raw reflection when resolving parameter aliases. - Keep alias pairs symmetric with matching types and default values.
- Use parameter aliases to improve API ergonomics, not to hide confusing annotation design.
Related reading
- Spring AMQP - Sender and Receiving Messages
- Spring AMQP + RabbitMQ 3.3.5 ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN
- Spring amqp converter issue using rabbit listener
- Spring AMQP (Rabbit) Listener goes in a loop in case of exception
- Spring AMQP RabbitMQ implementing priority queue
- Spring and scheduled tasks on multiple instances
- Spring AOP vs AspectJ
- Spring Apache Kafka onFailure Callback of KafkaTemplate not fired on connection error

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.