0

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.

kodali
  • 297
  • 1
  • 4
  • 19
  • 6
    Are there 2 `Jaxb2Marshaller` beans available ? Are the bean names mentioned in the console ? – SKumar Sep 08 '21 at 17:16
  • 1
    I agree with @SKumar, its `Jaxb2Marshaller` that probably has 2 implementations and Spring doesn't know which one to inject, the problem probably is not in your beans Try to find this `Jaxb2Marshaller`. also share your `pom.xml` probably a dependency there brings the extra implementation or have you defined it with `@Beans` somewhere and how? – pleft Sep 08 '21 at 17:21
  • @SKumar, Yes 2 Jaxb2Marshller beans available in two different modules, I need two modules, so I compile both in my build gradle file. So Ambiguity is happening. Kindly let me know how to solve this error. – kodali Sep 09 '21 at 06:01
  • @pleft, thanks for your comment. I attached my gradle file and full error. – kodali Sep 09 '21 at 06:01
  • @kodali Please post the full code of the configuration classes `ProposalClientConfig` and `EddClientConfig` – pleft Sep 09 '21 at 06:58
  • Do you have control over those 2 sub modules ? Can you change them ? – SKumar Sep 09 '21 at 07:03
  • Share the code where you create both `Jaxb2Marshaller` beans! – ray Sep 09 '21 at 09:06
  • Yes, I have control over those 2 sub modules, I can change it. – kodali Sep 09 '21 at 10:44

2 Answers2

3

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) {
        
    }
}

You need to see where these beans are created in your autoconfiguration or create your own bean of type Jaxb2Marshaller and use that.

Fermi-4
  • 645
  • 6
  • 10
1

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) {
        ........     
    }
}
SKumar
  • 1,940
  • 1
  • 7
  • 12