Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring data repository works without annotations

I'm using Spring Data JPA repositories (like MyRepo extends JpaRepository) and it works without @Repository and without @EnableJpaRepositories annotations. Could someone explain why?

like image 916
Nikita Avatar asked Aug 17 '17 08:08

Nikita


People also ask

Is repository annotation mandatory?

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. From the javadoc: Annotation to enable JPA repositories.

Is it required to write @repository annotations in Spring data JPA?

What's the best practice - to annotate or not? No, you don't need to when using Spring Data JPA. The Spring Data infrastructure scans for all interfaces extending Repository and automatically generates proxied implementations for them.

How do spring data repositories work?

These Repositories are Java interfaces that allow you as the developer to define a data access contract. The Spring Data JPA framework can then inspect that contract, and automatically build the interface implementation under the covers for you.

What is no repository bean annotation?

Annotation Type NoRepositoryBean This will typically be used when providing an extended base interface for all repositories in combination with a custom repository base class to implement methods declared in that intermediate interface.


1 Answers

Probably you are using Spring Boot.

Spring Data repositories usually extend from the Repository or CrudRepository interfaces. If you are using auto-configuration, repositories will be searched from the package containing your main configuration class (the one annotated with @EnableAutoConfiguration or @SpringBootApplication) down.

Please check https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-spring-data-jpa-repositories for more details.

like image 101
Mykhailo Hodovaniuk Avatar answered Sep 28 '22 19:09

Mykhailo Hodovaniuk