【问题标题】:Priority between service beans in a spring application implementing same interface实现相同接口的spring应用程序中服务bean之间的优先级
【发布时间】:2018-12-17 09:43:41
【问题描述】:

我有一个@RestController 有一个自动连接的UserService 接口,我有两个服务bean UserInMemoryService 和UserJpaService,它们都实现了UserService 接口。

现在UserInMemoryService 使用内存存储库,UserJpaService 使用 JPA 存储库进行数据操作。问题是spring如何决定走哪条路?因为在控制器中我所拥有的@Autowired 是接口,没有详细说明要选择什么具体类。

【问题讨论】:

  • 没有看到代码和配置,这或多或少是不可能回答的。默认情况下会抛出异常,如果没有发生这种情况,您的代码中有任何东西阻止了这种情况并阻止配置 2 个实例。

标签: spring spring-boot autowired


【解决方案1】:

问题是spring如何决定走哪条路?因为在 控制器我所使用的@Autowired 是没有详细信息的接口 具体课程。

春天不会决定。它只会以一个异常结束,说“找到了多个 UserService 类型的 bean”

Spring Couldn't autowired,there is more than one bean of `` type

您可能需要使用 @Qualifier 来告诉 Spring 使用哪个 bean

【讨论】:

  • 这正是我会发生的事情,但应用程序正在工作:D 并且默认情况下它会使用 UserJpaService!
猜你喜欢
  • 2019-09-03
  • 2021-09-03
  • 2011-02-09
  • 1970-01-01
  • 1970-01-01
  • 2019-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多