我们应该在Spring启动时在@Entity类上使用@Component吗?

问题描述 投票:-1回答:1

好吧,我是春季靴子的新手。我正在开发一个项目,我需要在控制器类中@Autowired我的实体。但我最终得到了错误:

abc中的字段存储库需要一个无法找到的类型为'xyz'的bean。

但它在Entity类中添加@Component后解决了。

所以我的问题是:

  1. 为什么Spring引导不扫描我的Entity类,因为它是在@SpringBootApplication声明下?
  2. 我们应该在应用程序中何时何地使用@Component注释?
spring-boot
1个回答
0
投票

使用@Component将Pojo标记为Spring Bean,以便使用@Autowired将其注入其他bean

使用@Entity将您的Pojo标记为JPA或Spring Data托管bean,以将其读取或写入数据库

© www.soinside.com 2019 - 2024. All rights reserved.