我有这个界面:
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/