如何使用@Qualifier 来识别应该使用的bean

how to use @Qualifier to identify the bean that should be consumed

我想使用组件之一的 WebServiceTemplate 对象 class 但在两个配置 class 中声明,下面是代码

@Configuration
public class EddClientConfig {
    @Bean
    public WebServiceTemplate eddTemplate(Jaxb2Marshaller jaxb2EprsMarshaller) {
        ........  
    }
}

@Configuration
public class ProposalClientConfig {
    
    @Bean
    public WebServiceTemplate ProposalTemplate(Jaxb2Marshaller jaxb2EprsMarshaller) {
        ........
        
    }
}

@Component
public class ProposalDataClientImpl implements  ProposalDataClient{
......
    @Autowired
    private WebServiceTemplate eddTemplate;
......
}

错误是

Parameter 0 of method eddTemplate in EddClientConfig required a single bean, but 2 were found:
- jaxb2EddMarshaller: defined by method 'jaxb2EddMarshaller' in class path resource [....EddClientConfig.class]
- jaxb2PropposalDataMarshaller: defined by method 'jaxb2PropposalDataMarshaller' in class path resource [...../ProposalDataClientConfig.class]

我的Gradle.build文件

dependencies {
compile(project(':edd-connector')) // EddClientConfig class exists here
compile(project(':proposal-connector')) //ProposalClientConfig class exists here
......
}

What I tried

@Configuration
public class EddClientConfig {
    @Bean(name = "eddTemplate")
    public WebServiceTemplate eddTemplate(Jaxb2Marshaller jaxb2EprsMarshaller) {
        ........  
    }
}

@Configuration
public class ProposalClientConfig {
    
    @Bean(name = "ProposalTemplate")
    public WebServiceTemplate ProposalTemplate(Jaxb2Marshaller jaxb2EprsMarshaller) {
        ........     
    }
}

@Component
public class ProposalDataClientImpl implements  ProposalDataClient{
......
    @Autowired
    @Qualifier("ProposalTemplate")
    private WebServiceTemplate eddTemplate;
......
}

但运气不好。让我知道我做错了什么。提前致谢。

Parameter 0 of method eddTemplate in EddClientConfig required a single bean, but 2 were found

@Configuration
public class EddClientConfig {
    // this is the method in the trace
    @Bean(name = "eddTemplate")
    // parameter 0 = jaxb2EprsMarshaller --> there are 2 impl of this bean in your app context
    public WebServiceTemplate eddTemplate(Jaxb2Marshaller jaxb2EprsMarshaller) {
        
    }
}

您需要在自动配置中查看这些 bean 的创建位置,或者创建您自己的 Jaxb2Marshaller 类型的 bean 并使用它。

Parameter 0 of method eddTemplate in EddClientConfig required a single bean, but 2 were found:

  • jaxb2EddMarshaller: defined by method 'jaxb2EddMarshaller' in class path resource [....EddClientConfig.class]
  • jaxb2PropposalDataMarshaller: defined by method 'jaxb2PropposalDataMarshaller' in class path resource [...../ProposalDataClientConfig.class]

如果您将 Qualifier 与 bean 名称一起使用,这个异常应该会消失,就像您在 ProposalDataClientImpl 中尝试的那样。因为,有 2 个 bean - jaxb2EddMarshallerjaxb2PropposalDataMarshaller。我们可以像下面这样使用它们,这样 Spring 就会知道 eddTemplate 应该使用 jaxb2EddMarshaller 而另一个配置另一个 Marshaller -

@Configuration
public class EddClientConfig {
    @Bean(name = "eddTemplate")
    public WebServiceTemplate eddTemplate(@Qualifier("jaxb2EddMarshaller")Jaxb2Marshaller jaxb2EprsMarshaller) {
        ........  
    }
}

@Configuration
public class ProposalClientConfig {

    @Bean(name = "ProposalTemplate")
    public WebServiceTemplate ProposalTemplate(@Qualifier("jaxb2PropposalDataMarshaller")Jaxb2Marshaller jaxb2EprsMarshaller) {
        ........     
    }
}