简体   繁体   中英

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?

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

I can miss something about Spring 'magic'

Most likely you miss @EnableJpaRepositories . From the doc : "Will scan the package of the annotated configuration class for Spring Data repositories by default. "

why we need it on our 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.

Relevant quote from the Repository (not @Repository ): "General purpose is to hold type information as well as being able to discover interfaces that extend this one during classpath scanning for easy Spring bean creation ."

Now why then SimpleJpaRepository is annotated with @Repository ? Because it is in the different package than the (once again the same doc ) "package of the annotated configuration class ".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM