Spring Framework provides stereotyped annotations, which indicate a class’s role in the context of the application. These annotations are located in the org.springframework.stereotypepackage. When we use the stereotype annotations, Spring automatically imports the annotated classes as beans into the application context and injects them into dependencies.
Spring Stereotypes
@Component
The @Component annotation marks Java classes as candidates for automatic detection, which means that Spring will automatically create an instance of this class and manage its lifecycle.
@Component
public class BookCache {
...
}
@Repository
Indicates that an annotated class is a “Repository”, originally defined by Domain-Driven Design (Evans, 2003) as “a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects”. This means that it marks a class as a repository, which encapsulates the logic required to access data from a data source.
By annotating a class with @Repository, the class is eligible for the database-related exception translation mechanism in Spring DataAccessException.
@Repository
public interface BookRepository extends JpaRepository<Book, Long> {
...
}
@Service
@Service annotation is a specialized stereotype annotation that indicates that a class is a “Business Service Facade” (in the Core J2EE patterns sense), or something similar. It is a general-purpose stereotype and individual teams may narrow their semantics and use it as appropriate.
@Service
public class BookService {
...
}
@Controller
@Controller is the main annotation that indicates the annotated class serves as a Controller of the MVC framework. The Dispatcher Servlet scans classes annotated with it to map the web requests to the methods annotated with @RequestMapping. It inherits from the @Component anno-
tation like other Spring annotations, such as @Service and @Repository.
@Controller
@RequestMapping("/book")
public class bookController {
...
}
@Indexed
It indicates that a class should be indexed by the Spring container. It was introduced as part of Spring Framework 5 to optimize component scanning for large applications. It is not used for creating Spring beans (like @Component , @Service , @Repository , etc..) but helps Spring scan for beans more efficiently by pre-indexing certain metadata.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component { }
Every class annotated with @Component becomes a candidate and is added to the component index. It is also possible to index all implementations of a certain interface or all the subclasses of a given class by adding @Indexed on it.
import org.springframework.stereotype.Indexed;
@Indexed
public class BookService {
...
}
When a Spring application has a large number of beans (for example, hundreds or thousands), we may consider using @Indexed to improve the performance of component scanning.
Conclusion
In this post, we learned what Spring stereotype annotations are. By using these annotations, developers can take advantage of Spring’s powerful dependency injection and component management features, leading to a more modular and maintainable code base.
Thank you for reading!! See you in the next post.