如果没有@Repository 注释,代码可以正常工作,并且带有注释也可以正常工作.有什么区别? [英] Without @Repository annotation the code works fine and with annotation also works fine. What is the difference?

查看:23
本文介绍了如果没有@Repository 注释,代码可以正常工作,并且带有注释也可以正常工作.有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以通过这种方式声明存储库类

public interface DepartmentRepository extends JpaRepository

就这样

@Repository公共接口 DepartmentRepository 扩展 JpaRepository

提到的这两种方法有什么区别.因为如果我们删除 @Repository 注释,那么代码将可以正常工作,那么有什么区别,谁能解释一下.

解决方案

虽然其他答案详细介绍了构造型注释,但他们没有说明为什么不需要它,但它是 Spring Boot 功能 - 而不是 Spring Framework.

使用 Spring Boot,它将自动扫描来自主要 SpringBootApplication 类的子包并检测存储库接口并使用作为 SpringBootApplication 一部分的自动配置为您创建 bean

<块引用>

Spring Data 存储库通常从 Repository 或 CrudRepository 接口扩展.如果您使用自动配置,则从包含您的主配置类(用@EnableAutoConfiguration 或 @SpringBootApplication 注释的类)的包中搜索存储库.

https://docs.spring.io/autorepo/docs/spring-boot/current/reference/htmlsingle/#boot-features-spring-data-jpa-repositories

如果您没有使用 Spring Boot,或者存储库不在 Spring Boot 应用程序的包或子包中,则需要对其进行注释并扫描包,或者创建一个 bean.

<块引用>

Spring Data 可以创建各种风格的@Repository 接口的实现.Spring Boot 会为您处理所有这些,只要这些 @Repositories 包含在您的 @EnableAutoConfiguration 类的同一个包(或子包)中.

<块引用>

对于许多应用程序,您需要的只是将正确的 Spring Data 依赖项放在您的类路径中.JPA 有 spring-boot-starter-data-jpa,Mongodb 有 spring-boot-starter-data-mongodb 等.首先,创建一些存储库接口来处理您的 @Entity 对象.

<块引用>

Spring Boot 尝试根据它找到的 @EnableAutoConfiguration 猜测你的 @Repository 定义的位置.要获得更多控制,请使用 @EnableJpaRepositories 注释(来自 Spring Data JPA).

有关此行为的更多信息,请参阅文档,例如更好地控制存储库的创建.

https://docs.spring.io/autorepo/docs/spring-boot/current/reference/htmlsingle/#howto-use-spring-data-repositories

we can declare repository classes in this way

public interface DepartmentRepository extends JpaRepository<Department, Integer>

and this way

@Repository
public interface DepartmentRepository extends JpaRepository<Department, Integer>

What is the difference between this two approach mentioned. Because if we remove the @Repository annotation then the code will work fine then what is the difference, can anyone explain me on this.

解决方案

While the other answers go into detail about the sterotype annotations they don't document why exactly its not needed, its a Spring Boot feature - not Spring Framework.

With Spring Boot, it will automatically scan child packages from the main SpringBootApplication class and detect repository interfaces and create beans for you with the use of Autoconfiguration that is part of the SpringBootApplication

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

https://docs.spring.io/autorepo/docs/spring-boot/current/reference/htmlsingle/#boot-features-spring-data-jpa-repositories

If you were not using Spring Boot, or the repository was not in package, or child-package, of the Spring Boot Application it would need to be annotated and the package scanned, or a bean created.

Spring Data can create implementations of @Repository interfaces of various flavors. Spring Boot handles all of that for you, as long as those @Repositories are included in the same package (or a sub-package) of your @EnableAutoConfiguration class.

For many applications, all you need is to put the right Spring Data dependencies on your classpath. There is a spring-boot-starter-data-jpa for JPA, spring-boot-starter-data-mongodb for Mongodb, etc. To get started, create some repository interfaces to handle your @Entity objects.

Spring Boot tries to guess the location of your @Repository definitions, based on the @EnableAutoConfiguration it finds. To get more control, use the @EnableJpaRepositories annotation (from Spring Data JPA).

See the documentation for more information on this behavior e.g. taking more control over the creation of repositories.

https://docs.spring.io/autorepo/docs/spring-boot/current/reference/htmlsingle/#howto-use-spring-data-repositories

这篇关于如果没有@Repository 注释,代码可以正常工作,并且带有注释也可以正常工作.有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆