Understanding Spring’s Core Stereotype Annotations: @Component, @Controller, @Repository, @Service

This article explains the purpose, usage, and underlying meta‑annotations of Spring’s core stereotype annotations—@Component, @Controller, @Repository, and @Service—showing how they enable classpath scanning, define component roles, and improve application startup performance.

JavaEdge
JavaEdge
JavaEdge
Understanding Spring’s Core Stereotype Annotations: @Component, @Controller, @Repository, @Service

Since Spring 2.5 introduced annotation‑driven development, a set of stereotype annotations was added in the org.springframework.stereotype package to simplify component identification and automatic detection.

1. @Component

Marks the annotated class as a generic “component” that can be discovered via classpath scanning.

It also serves as a base for more specific stereotypes such as @Repository and @Aspect.

The annotation itself is defined with several meta‑annotations: @Target(ElementType.TYPE) – can be applied to classes, interfaces, annotations, or enums. @Retention(RetentionPolicy.RUNTIME) – retained at runtime for reflection. @Documented – included in Javadoc. Indexed – a Spring 5.0 specific annotation that adds an index to improve startup speed.

2. @Controller

Identifies the annotated class as a web controller.

It is a specialization of @Component and is typically used together with handler methods annotated with @RequestMapping.

3. @Repository

Marks the annotated class as a repository, a concept originally defined in Domain‑Driven Design (DDD) as “a mechanism for encapsulating storage, retrieval, and search behavior that mimics a collection of objects”.

It can also be applied to traditional DAO classes, but developers should understand the distinction between DDD repositories and classic Data Access Objects.

When used with PersistenceExceptionTranslationPostProcessor, the annotation enables automatic translation of persistence exceptions into Spring’s DataAccessException hierarchy.

Since Spring 2.5, @Repository is also a specialization of @Component, allowing automatic detection via classpath scanning.

4. @Service

Indicates that the annotated class provides a “service” – an operation offered by an interface, independent of the domain model and without encapsulated state (as defined by DDD).

It can also denote a business‑service façade from a J2EE perspective.

Like the other stereotypes, @Service is a specialization of @Component, making it eligible for automatic detection during classpath scanning.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentspringComponentannotationsControllerServiceRepository
JavaEdge
Written by

JavaEdge

First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.