【发布时间】:2021-09-08 17:03:59
【问题描述】:
我想使用组件类之一中的 WebServiceTemplate 对象,但在两个配置类中声明,下面是代码
@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;
......
}
错误是
EddClientConfig 中方法 eddTemplate 的参数 0 需要一个 bean,但找到了 2 个:
- jaxb2EddMarshaller:由类路径资源中的“jaxb2EddMarshaller”方法定义 [....EddClientConfig.class]
- jaxb2PropposalDataMarshaller:由类路径资源中的方法 'jaxb2PropposalDataMarshaller' 定义 [...../ProposalDataClientConfig.class]
我的 Gradle.build 文件
dependencies {
compile(project(':edd-connector')) // EddClientConfig class exists here
compile(project(':proposal-connector')) //ProposalClientConfig class exists here
......
}
我尝试了什么
@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;
......
}
但没有运气。让我知道我做错了什么。提前致谢。
【问题讨论】:
-
是否有 2 个
Jaxb2Marshallerbean 可用?控制台中是否提到了 bean 名称? -
我同意@SKumar,它的
Jaxb2Marshaller可能有2 个实现,而Spring 不知道要注入哪一个,问题可能不在你的bean 中尝试找到这个Jaxb2Marshaller。还分享您的pom.xml可能是一个依赖项带来了额外的实现,或者您是否在某处使用@Beans定义了它以及如何定义它? -
@SKumar,是的 2 个 Jaxb2Marshller bean 在两个不同的模块中可用,我需要两个模块,所以我在我的构建 gradle 文件中编译这两个模块。所以歧义正在发生。请让我知道如何解决此错误。
-
@pleft,感谢您的评论。我附上了我的 gradle 文件和完整的错误。
-
@kodali 请贴出配置类
ProposalClientConfig和EddClientConfig的完整代码
标签: java spring spring-boot qualifiers