Spring Stereotype Annotation Hierarchy
Fast recall
Quick revision
Core answer
Understanding Spring's meta-annotation design pattern and how annotation composition enables framework extensibility
Thinking @Service, @Repository, and @Controller are independent annotations instead of recognizing they're composed from @Component
Explain how meta-annotations allow Spring to scan for one annotation and discover all derived ones, and why this design makes Spring extensible without modifying core scanning logic
Interview-ready guide
Interview answer
Spring's stereotype annotations form a hierarchy through meta-annotations.
All of them—@Service, @Repository, @Controller—are meta-annotated with @Component. That means when you look at Spring's source code for @Service, you'll see @Component in its definition. It's like annotation inheritance.
This design is really smart for component scanning. Spring only needs to scan for @Component, and it automatically finds all the specialized stereotypes because they include @Component. You get efficiency without losing the semantic meaning of each stereotype.
The hierarchy goes further. @RestController is meta-annotated with @Controller, which itself has @Component. So @RestController inherits from both, getting request mapping from @Controller and automatic JSON serialization from @ResponseBody.
This composition pattern is why Spring is so extensible. You can create your own stereotype by meta-annotating it with @Component, and Spring will detect it automatically without any configuration changes. The common mistake Learners make is thinking @Service and @Repository are independent, unrelated annotations.
They're not—they're all built on @Component, which is why component scanning finds them all with one mechanism.
Spring Stereotype Annotation Hierarchy
Direct Answer
All Spring stereotype annotations are meta-annotated with @Component. This means @Service, @Repository, and @Controller have @Component inside their annotation definitions, forming an annotation hierarchy. Component scanning finds them all by looking for @Component, while each stereotype preserves its semantic meaning. @RestController is meta-annotated with @Controller, extending the hierarchy further.
Code Example
// Spring's actual definition of @Service (simplified)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component // Meta-annotation: @Service includes @Component
public @interface Service {
@AliasFor(annotation = Component.class)
String value() default "";
}
// Spring's actual definition of @RestController (simplified)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller // Meta-annotated with @Controller (which has @Component)
@ResponseBody // Adds behavior: auto-serialize return values to JSON
public @interface RestController {
@AliasFor(annotation = Controller.class)
String value() default "";
}
// Your code using the composed annotations
@Service // Implicitly includes @Component
public class OrderService {
// Spring finds this during component scan because @Service has @Component
}
@RestController // Has @Controller, which has @Component
public class OrderController {
// Spring finds this AND applies @ResponseBody behavior automatically
}
Common Mistakes & Pitfalls
- Thinking stereotypes are independent: Learners often believe @Service and @Repository are completely separate annotations with no relationship. They don't realize component scanning works by looking for @Component, which all stereotypes inherit. This misunderstanding causes confusion when learning about custom component scanning filters.
- Not recognizing annotation composition: When using @RestController, Learners don't realize it combines @Controller and @ResponseBody. They might redundantly add @ResponseBody to methods, not knowing it's already inherited from the class-level annotation.
- Missing the extensibility point: Learners don't see that they can create their own stereotype annotations by meta-annotating with @Component. This leads to overusing @Component directly instead of creating semantically meaningful custom stereotypes like @Adapter or @Gateway that document domain concepts while still being detected as beans.
Check your understanding
Answer these without looking back at the article.
- What is meta-annotation in Java?
- How would you create a custom stereotype annotation?
- How does Spring detect @Service as a @Component?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.