java - 将存储库接口(interface)注释为 @Component 有什么缺点吗?

标签 java spring intellij-idea interface spring-annotations

我有这个界面:

public interface liteRepository extends CrudRepository<liteEntity, Long>, JpaSpecificationExecutor<liteEntity> {...}

一切正常。

但是,intellij 不会将此类注册为 Spring 组件。如果我用 @Component 注释此接口(interface),那么 intellij 会将其识别为 spring bean,并且我可以在集成测试中使用 @Autowire 来此存储库。

我的代码在注释后仍然可以工作,但我不确定我没有弄乱我不应该弄乱的东西。

问题:
在这个接口(interface)上添加@Component注解有什么坏处吗?

最佳答案

唯一的事情是@Component注解的意思是该类有资格在 Spring 的组件扫描期间成为 Spring bean。

因此,如果您希望它是一个 Spring bean,并且您没有在其他任何地方将其定义为 Spring bean,则可以安全地添加 @Component注释。

当然,这只有在你在某处配置了实际的组件扫描时才有效(例如,在某些 Spring 配置文件中的 <context:component-scan base-package="..."> ),我假设你已经举起,因为 bean 在你之后正确地 Autowiring 了添加注释。

关于java - 将存储库接口(interface)注释为 @Component 有什么缺点吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33529601/

相关文章:

java - 无法读取 IntelliJ 中的属性文件

java - 从 Linux 终端卸载 IntelliJ

javascript - Intellij Idea/Webstorm jsdoc分析问题, Unresolved 变量

java json模式验证相对路径不起作用(找不到URI)

spring - 由 : java. lang.IllegalStateException 引起:您需要为 git 存储库配置 uri

java - 如何在 Spring 中只销毁一个 bean?

spring - 无法将 'java.lang.String' 类型的属性值转换为 thymeleaf 形式的属性 'java.time.LocalDate' 所需的类型 'date'

java - 如何使用ResetAbandonedTimer避免由于removeAbandonedTimeout而关闭连接

Java SOAP 请求

Java - 小程序中的 java.lang.UnsupportedClassVersionError