What’s the difference between @Component, @Control...
What’s the difference between @Component, @Control...
In Spring Framework, the annotations @Component, @Controller, @Repository, and @Service are used to define different aspects and behaviors of beans within the application context. Each serves a specific role, helping in the organization, readability, and functionality of Spring applications.
The @Component annotation is a generic stereotype for any Spring-managed component. It marks a Java class as a bean, a managed component of Spring's application context. This annotation is the most general-purpose among the stereotypes and can be used across the application to mark a bean as Spring-managed. It is a base annotation from which other annotations like @Repository, @Service, and @Controller are derived[2][3].
The @Repository annotation is a specialization of @Component. It is specifically used in the persistence layer, which interacts with the database. This annotation indicates that the annotated class is a Data Access Object (DAO), responsible for encapsulating storage, retrieval, and search behavior which emulates a collection of objects[2][3]. It provides additional benefits like translation of database-related exceptions into Spring's data access exception hierarchy.
The @Service annotation is another specialization of @Component, used in the service layer of an application. This layer is typically used to define business logic and calls methods defined in the repository layer. Classes annotated with @Service contain business logic and call methods defined in @Repository, acting as a transaction boundary[2][3]. This annotation emphasizes the service's role in the business layer, differentiating it from the lower...
senior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào