Câu hỏi phỏng vấn Spring
Câu hỏi

What's the difference between @Component, @...

Câu trả lời

In Spring Framework, the annotations @Component, @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. Here's a detailed explanation of each annotation:

@Component

The @Component annotation is a generic stereotype for any Spring-managed component. It is a class-level annotation that tells Spring that a class is a Spring component. @Component serves as a superclass for more specific stereotypes such as @Repository, @Service, and @Controller, allowing for component scanning and automatic detection of beans by Spring's dependency injection facilities[1][3].

@Repository

@Repository is a specialization of the @Component annotation. It is used to annotate classes at the persistence layer, which act as a database repository. This annotation has a specific role in handling data access exceptions, translating them from the specific technology used for data access to Spring's DataAccessException. Thus, when you use @Repository on a DAO (Data Access Object), Spring will provide data access-related exception translation automatically. This annotation is intended to be used for database-related operations[1][3].

@Service

@Service is another specialization of the @Component annotation, but it is used to annotate classes at the service layer. This layer is typically used to define business logic, operations, or calls to the persistence layer, encapsulating business logic, data access, and custom functionality...

expert

expert

Gợi ý câu hỏi phỏng vấn

expert

What are some of the valid return types of a controller method?

middle

What is Aspect?

middle

What is the typical Bean life cycle in Spring Bean Factory Container?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào