Spring data repository works without annotations

前端 未结 3 1232
花落未央
花落未央 2020-12-03 22:58

I\'m using Spring Data JPA repositories (like MyRepo extends JpaRepository) and it works without @Repository and without @EnableJpaRepositori

相关标签:
3条回答
  • 2020-12-03 23:13

    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.

    0 讨论(0)
  • 2020-12-03 23:17

    you don't need @Repository to make use of Spring Data JPA. The Interface extending the CrudRepository or JPARepository would work even without annotating it with @Repository. The Core reason why you need to have this annotation in place is it makes unchecked exceptions thrown in the DAO layer eligible to be translated into Spring DataAccessException. Which in turn would be easier to work with. This is the important aspect of using @Repository

    More details see this -> https://www.youtube.com/watch?v=z2re1MfWtz0&list=PLO0KWyajXMh4fGMvAw1yQ1x7mWayRcmX3&index=8&t=0s

    0 讨论(0)
  • 2020-12-03 23:18

    For more information look into these class which is used to auto-configure Spring Data JPA Repositories:

    JpaRepositoriesAutoConfigureRegistrar

    Docs : http://www.atetric.com/atetric/javadoc/org.springframework.boot/spring-boot-autoconfigure/1.2.0.RELEASE/org/springframework/boot/autoconfigure/data/jpa/JpaRepositoriesAutoConfigureRegistrar.html

        @EnableJpaRepositories
        private static class EnableJpaRepositoriesConfiguration {
    
        }
    
    0 讨论(0)
提交回复
热议问题