【发布时间】:2020-07-29 22:34:58
【问题描述】:
我对 Spring Boot IoC 和 @Bean 的概念完全陌生
假设这段代码实现了
@SpringBootApplication
public class MyApplication{
@Bean
public WebClient rest(ReactiveClientRegistrationRepository clients,
ServerOAuth2AuthorizedClientRepository authz) {
您在这里看到,只有 1 个类实现了接口 ReactiveClientRegistrationRepository,所以我的正确猜测是,如果只有一个类实现它,那么 autowired 将自动成为 clients 参数
public final class InMemoryReactiveClientRegistrationRepository
implements ReactiveClientRegistrationRepository, Iterable<ClientRegistration> {
另一方面,ServerOAuth2AuthorizedClientRepository 由 3 个不同的类实现,即
public final class AuthenticatedPrincipalServerOAuth2AuthorizedClientRepository
implements ServerOAuth2AuthorizedClientRepository {
public class UnAuthenticatedServerOAuth2AuthorizedClientRepository implements ServerOAuth2AuthorizedClientRepository {
public final class WebSessionServerOAuth2AuthorizedClientRepository
implements ServerOAuth2AuthorizedClientRepository {
调试后,得到的类是AuthenticatedPrincipalServerOAuth2AuthorizedClientRepository
spring boot 框架如何知道哪个是哪个?我认为会有bean冲突,比如这个问题Spring boot autowiring an interface with multiple implementations
【问题讨论】:
标签: java spring spring-boot