@Repository not necessary when implementing JpaRepository?
Asked Answered
L

5

35

I have a repository class in my spring boot app. First, I annotated it with @Repository and then I implemented JpaRepository. Now I got rid of the annotation and it still works.

I saw that JpaRepository has the @NoRepositoryBean annotation.

How does this work? Or should this not work and there's something weird going on in my application?

Loquitur answered 19/5, 2017 at 11:50 Comment(1)
did u make @EnableJpaRepositories in repositories configuration?Culch
R
63

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

The purpose of the @NoRepositoryBean annotation is to prevent Spring from treating that specific interface as a repository by itself. The JpaRepository interface has this annotation because it isn't a repository itself, it's meant to be extended by your own repository interfaces, and those are the ones that should be picked up.

Or should this not work and there's something weird going on in my application?

It works as it should and there is nothing weird going on in your application.

Racecourse answered 19/5, 2017 at 11:53 Comment(4)
Which of the spring data jpa's repository class has @Repository annotation?Downbow
@Downbow None of the classes and interfaces provided by Spring has an @Repository annotation. This annotation is meant to be used on interfaces that you define yourself.Racecourse
so spring will automatically recognise interface that extends JpaRepository interface as repository and the @EnableJpaRepositories("packagestoscan") will then create implementation class for the repository?Claiborn
@Claiborn Yes, when you use EnableJpaRepositories then Spring will look at the interfaces in the package(s) you specify and generate the implementation classes for the ones that have a Repository annotation.Racecourse
M
7

It is not mandatory. The reason it will be working is beacause, you would have specified to framework the packages to look for repositories using @EnableJpaRepositories("packagestoscan")

Misadvise answered 19/5, 2017 at 11:54 Comment(2)
Right, that was it! Thanks!Loquitur
This however does not explain how Spring knows that a repository is a repository without the @Repository annotation.Arabia
S
4

I am gussing you set it up with @EnableJpaRepositories. This will scan for Spring Data repositories even if they are not annotated with @Repository.

From the javadoc:

Annotation to enable JPA repositories. Will scan the package of the annotated configuration class for Spring Data repositories by default.

You can also specify basePackages/basePackageClasses to change the default.

Shark answered 19/5, 2017 at 11:53 Comment(1)
Yes, that was it! I am still struggling with all the annotations.Loquitur
T
4

When using spring data jpa you don't need to specify repository explicitly. The spring data jpa scans for all interfaces extending repository and generate implementation automatically.

Tristatristam answered 20/8, 2018 at 6:10 Comment(2)
its comment quality answeAnderer
I realized it after posting. My Bad!Tristatristam
I
2

I would suggest as a good practice to annotate with @Repository class you are implementing for example:

@Repository
JpaRepositoryImpl implements JpaRepository 
Interchangeable answered 19/5, 2017 at 12:33 Comment(1)
why though? this makes no senseHeadset

© 2022 - 2024 — McMap. All rights reserved.