Scrivio Interview library
InternalsIntermediate5 min read · updated 12 November 2026

Spring lets you write @RestController instead of @Controller plus @ResponseBody, and @GetMapping instead of @RequestMapping(method = GET). Java annotations cannot extend each other. How does this composition work?

Java has no annotation inheritance, so Spring builds composition itself. @RestController is declared with @Controller and @ResponseBody placed on it as meta-annotations, and Spring's annotation model, MergedAnnotations, searches annotations on annotations recursively, so a class marked @RestController is treated as if it had @Controller, @Component and @ResponseBody too. @AliasFor tells Spring that an attribute of the composed annotation, such as @GetMapping's path, maps to an attribute of a meta-annotation, here @RequestMapping's path. Plain Class.getAnnotation does not follow this convention, so we must use Spring's utilities to see it.

Why this question is asked

Spring's composed annotations are so natural to use that most developers never ask how they work, until a custom aspect or a library cannot find @Component on a @Service class, or a composed annotation of their own silently ignores an attribute. Interviewers ask this question because it separates knowing the annotations from knowing the mechanism. In this answer, we will see why Java cannot do this alone, look at how @RestController and @GetMapping are declared, follow Spring's meta-annotation search, explain @AliasFor, and see what breaks with plain reflection.

1Why Java Cannot Do This Alone

Annotation types in Java cannot extend each other. There is no @interface RestController extends Controller. What Java does allow is placing annotations on an annotation type declaration, as long as their @Target permits it. To Java, those are just annotations sitting on another annotation; the language gives them no meaning.

Spring gives them meaning by convention. When Spring looks for an annotation, it checks the element itself and then the annotations on each annotation present, recursively. It calls those meta-annotations, and an annotation found that way is called meta-present.

2How @RestController and @GetMapping Are Declared

Simplified, the Spring declarations look like this.

Java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller { ... }
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
@AliasFor(annotation = Controller.class)
String value() default "";
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = RequestMethod.GET)
public @interface GetMapping {
@AliasFor(annotation = RequestMapping.class)
String[] path() default {};
...
}
Step throughWhat Spring sees on a @RestController class
  1. @RestControllerdeclared directly on OrderController
  2. @Controller and @ResponseBodymeta-annotations, distance 1
  3. @Componentmeta-annotation of @Controller, distance 2
  4. @Indexedmeta-annotation of @Component, distance 3

4 layers, from the top down. Step through them one at a time.

Spring walks down this tree, so component scanning finds @Component and MVC finds @ResponseBody.

So @RestController carries @Controller and @ResponseBody, and @Controller carries @Component. @GetMapping carries a @RequestMapping whose method is already fixed to GET. Nothing here is special Java syntax; it is ordinary annotations placed on annotation types.

3How Spring Searches and Merges

Since Spring Framework 5.2, all of this goes through the MergedAnnotations API. Given a class or method, it collects directly present annotations, then walks each one's meta-annotations recursively, recording the distance of each from the element. Component scanning asks whether @Component is present, directly or as a meta-annotation, and finds it on OrderController at distance 2. Spring MVC asks the same about @ResponseBody and @RequestMapping.

Merging is the second half. When Spring reads @RequestMapping from a method annotated @GetMapping("/orders/{id}"), it does not just return the meta-annotation with its declared defaults. It returns a synthesized @RequestMapping whose path comes from @GetMapping and whose method is GET, as if we had written the long form ourselves.

Java
@RestController
class OrderController {
@GetMapping("/orders/{id}")
Order get(@PathVariable long id) { ... }
}
Method m = OrderController.class.getDeclaredMethod("get", long.class);
RequestMapping rm = AnnotatedElementUtils.findMergedAnnotation(m, RequestMapping.class);
System.out.println(Arrays.toString(rm.path())); // [/orders/{id}], taken from @GetMapping
System.out.println(Arrays.toString(rm.method())); // [GET]
boolean isComponent = MergedAnnotations.from(OrderController.class).isPresent(Component.class); // true

4What @AliasFor Does

Merging needs to know which attribute of the composed annotation feeds which attribute of the meta-annotation. That is what @AliasFor declares. In @GetMapping, @AliasFor(annotation = RequestMapping.class) on path says that @GetMapping.path overrides RequestMapping.path. Without an attribute name, it means the attribute with the same name; with attribute = "...", we can map to a different name.

@AliasFor also declares aliases within one annotation, such as value and path being interchangeable in @RequestMapping, and Spring checks that aliased attributes are not given conflicting values. In older versions Spring also matched attributes by name without @AliasFor, but that convention-based override was deprecated in Spring Framework 6, so explicit @AliasFor is the rule now; check your version's notes for when it was removed.

5What Breaks With Plain Reflection

The JDK's reflection API knows nothing about Spring's convention. Class.getAnnotation(Component.class) only returns an annotation directly present, or inherited through @Inherited from a superclass. A @RestController or @Service class does not have @Component directly, so plain reflection returns null.

Java
OrderController.class.getAnnotation(Controller.class); // null
OrderController.class.getAnnotation(Component.class); // null
AnnotatedElementUtils.hasAnnotation(OrderController.class, Component.class); // true
@Service class Billing { }
Billing.class.isAnnotationPresent(Component.class); // false

So a custom aspect, bean post-processor or library that looks for @Component, @Transactional or our own annotation with plain reflection will miss every composed or stereotype use. Inside Spring applications we use AnnotatedElementUtils, AnnotationUtils.findAnnotation or MergedAnnotations instead. And because this is a Spring convention, other libraries that use plain reflection, or other frameworks with their own rules, will not honour our composed annotations; a JPA provider, for example, will not recognise an @Entity hidden inside a custom meta-annotation.

6Follow-up questions

What breaks if you use plain reflection to look for @Component on beans?

Every bean declared with a stereotype or composed annotation is missed, because @Service, @Repository, @Controller, @RestController and @Configuration all carry @Component only as a meta-annotation. Class.getAnnotation(Component.class) returns null for them, so a registry built that way would contain almost none of the application's components. Using AnnotatedElementUtils.hasAnnotation or MergedAnnotations fixes it.

What is the difference between findMergedAnnotation and getMergedAnnotation?

The get variants search only the element itself and its meta-annotations, plus @Inherited annotations on superclasses. The find variants also search interfaces, superclasses and, for methods, overridden methods in the hierarchy. Use find when annotations may be declared on an interface, as with Spring Data repositories or methods inherited from a base controller.

Does annotation composition cost anything at run time?

The search is done mostly at startup, when beans and handler methods are analysed, and Spring caches the results. The per-request cost is negligible. With AOT processing, much of the analysis is moved to build time.

7Conclusion

In this answer, we saw that Java annotations cannot extend each other, so Spring composes them by convention: annotations placed on annotation types are treated as meta-annotations and searched recursively through the MergedAnnotations model. That is why @RestController counts as @Controller, @Component and @ResponseBody, and why @GetMapping produces a merged @RequestMapping with method GET. @AliasFor declares which attributes map to which, plain Class.getAnnotation does not see any of this, and libraries outside Spring may not honour it.

What to say in the interviewAbout 80 seconds aloud

Open with

Java annotations cannot inherit from each other, so this is a Spring convention, not a language feature. Java does let you put annotations on an annotation type, and Spring treats those as meta-annotations. @RestController is declared with @Controller and @ResponseBody on it, and @Controller is declared with @Component.

Then, if they want more

When Spring looks for an annotation, it uses its MergedAnnotations model, which checks the element and then walks the meta-annotations recursively. So component scanning finds @Component two levels down on a @RestController class, and MVC finds @ResponseBody.

The second part is merging attributes. @GetMapping is meta-annotated with @RequestMapping with method GET, and its path attribute has @AliasFor pointing at RequestMapping's path.

When MVC asks for the merged @RequestMapping on the method, Spring synthesizes one with my path and method GET, as if I had written the long form. @AliasFor is the explicit mapping between attributes; the older name-based convention was deprecated in Spring 6. The catch is that plain reflection knows nothing about this.

OrderController.class.getAnnotation(Component.class) returns null, so any custom code that looks for annotations should use AnnotatedElementUtils or MergedAnnotations. And since it is a Spring convention, other libraries may not honour composed annotations.

About this answer

Last updated
12 November 2026
Versions it names
Spring Framework 5, Spring Framework 6
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Spring lets you write @RestController instead of @Controller plus @ResponseBody, and...": https://scrivio-interview-library.netlify.app/java/annotations/f0d9b31a41/