Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why put @Repository on top of Spring Data JPA interfaces?

Very often I see code like the following:

@Repository
public interface CustomerRepository extends JpaRepository<Customer, Long>

And my question is why do we need this @Repository annotation?
I understand why @Repository exist, and why we may need to put in on top of some class.
But classes didn't inherit annotations from interfaces in Java. Of course, I can miss something about Spring 'magic', but the default JPA implementation class is the following:

@Repository
@Transactional(readOnly = true)
public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T, ID>

and it contains @Repository on its own, so why we need it on our interface?

Spring Data documentation also didn't say anything about @Repository in the related section, but some occasional examples from the other parts of documentation contain code fragment where @Repository is present, so maybe something was changed over time?

like image 376
Vova Yatsyk Avatar asked Oct 27 '22 15:10

Vova Yatsyk


People also ask

What does @repository do in Spring?

Spring @Repository annotation is used to indicate that the class provides the mechanism for storage, retrieval, search, update and delete operation on objects.

Can we use @repository in interface?

We don't. It is redundant to annotate the interfaces/classes with @Repository which extend/implement JpaRepository or some other predefined interfaces extending Repository , both because of @EnableJpaRepositories and the fact that they extend/implement Repository interface.

Is @repository necessary?

It is indeed not necessary to put the @Repository annotation on interfaces that extend JpaRepository ; Spring recognizes the repositories by the fact that they extend one of the predefined Repository interfaces.

What is @repository in JPA?

JpaRepository is a JPA (Java Persistence API) specific extension of Repository. It contains the full API of CrudRepository and PagingAndSortingRepository. So it contains API for basic CRUD operations and also API for pagination and sorting.


1 Answers

It is rather interesting that the spring official documentation shows @Repository on an interface in two different examples; though there is simply no need for this.

For the fun of it, I scanned our source-code (around 100 micro-services) and I found too many places where @Repository is present on the interface. I think it happens because people are facing some types of problems (like No qualifying bean of type...) and just start adding annotations everywhere they can, in order for the code to work.

I even doubted myself thinking that may be indeed spring boot in the recent versions did some (weird) magic, but no. After some tests it just showed the obvious : @Repository is useless on interfaces. In theory, Spring could find out that a certain interface extends its JPARepository, see that it is annotated with @Repository and apply the same to some class that it would provide as a @Bean. But

  • implementations of JPARepository already have that annotation

  • this would make little sense anyway

like image 153
Eugene Avatar answered Nov 08 '22 13:11

Eugene