Pavan Biradar
Pavan Biradar

Reputation: 29

Can I| use @Repository for DAO layer in spring boot

Can we use @Repository for DAO layer in spring boot ?

What is the difference between @Component and @Repository ? Which one should we use for DAO layer in Spring boot ?

Upvotes: 0

Views: 486

Answers (2)

Efim Matytsin
Efim Matytsin

Reputation: 11

@Repository is an annotation marker. No difference between @Component and @Repository, spring already can create bean instance from your interface if you extends JpaRepository or other spring repository interfaces

For example:

public interface UserRepository extends JpaRepository<User, BigInteger> {

    User findByUsernameOrEmail(String username, String email);
}

For more information please follow the link

Upvotes: 0

Olivier Depriester
Olivier Depriester

Reputation: 1625

@Repository is a specialization of @Component whose purpose is to handle DAO. So the answer is yes.

@Repository javadoc states :

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".
Teams implementing traditional Java EE patterns such as "Data Access Object" may also apply this stereotype to DAO classes, though care should be taken to understand the distinction between Data Access Object and DDD-style repositories before doing so. This annotation is a general-purpose stereotype and individual teams may narrow their semantics and use as appropriate.

Upvotes: 1

Related Questions