javaspringspring-bootqualifiers

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


I want use WebServiceTemplate object from one of the component class but declared in two Configuration classes, below is code

@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;
......
}

Error is

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]

My Gradle.build file

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;
......
}

But no luck. Just let me know what I did wrong. Thanks In Advance.


Solution

  • 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]

    This exception should go away if you use Qualifier with a bean name, just how you tried in ProposalDataClientImpl. Since, there are 2 beans - jaxb2EddMarshaller and jaxb2PropposalDataMarshaller. We can use them like below so that Spring would know that eddTemplate should use jaxb2EddMarshaller and the other config the other 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) {
            ........     
        }
    }