Spring中需要多个相同类型的bean

Required Multiple beans of same type in Spring(Spring中需要多个相同类型的bean)
本文介绍了Spring中需要多个相同类型的bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在将其标记为重复之前的请求.我浏览了论坛,在任何地方都找不到问题的解决方案.

A request before you mark it as duplicate. I have gone through the forum and couldn't find the solution for the problem anywhere.

我正在使用 Spring 3.2 编写代码,所有内容都纯粹基于注释.该代码接收从不同 XSD 文件派生的 XML 文件.

I am writing a code using Spring 3.2 and everything is purely annotation based. The code receives XML files which are derived form different XSD files.

所以我们可以说,有五种不同的 XSD(A1、A2、A3、A4、A5),我的代码接收任何类型的 XML,并且我有逻辑在到达时识别 XML 的类型.

So we can say, there are five different XSD ( A1, A2, A3, A4, A5) and my code receives XML of any type, and I have the logic to identify the type of the XML upon arrival.

现在,我正在尝试使用 Spring OXM 取消编组这些内容.但是因为涉及到多个 XSD,我们实际上不能使用一个 Un-marshaller.所以我们需要大约五个.

Now, I am trying to un-marshal these using Spring OXM. But because there are multiple XSDs involved, we cannot actually using one Un-marshaller. So we need around five of them.

Configuration 类中,我添加了五个 bean,如下所示:

In the Configuration class, I added five beans like below:

@Bean(name="A1Unmarshaller")
public Jaxb2Marshaller A1Unmarshaller(){
    Jaxb2Marshaller unMarshaller = new Jaxb2Marshaller();
    unMarshaller.setContextPath("package name for the classes generate by XSD A1");
}

@Bean(name="A2Unmarshaller")
public Jaxb2Marshaller A2Unmarshaller(){
    Jaxb2Marshaller unMarshaller = new Jaxb2Marshaller();
    unMarshaller.setContextPath("package name for the classes generate by XSD A2");
}

@Bean(name="A3Unmarshaller")
public Jaxb2Marshaller A3Unmarshaller(){
    Jaxb2Marshaller unMarshaller = new Jaxb2Marshaller();
    unMarshaller.setContextPath("package name for the classes generate by XSD A3");
}

@Bean(name="A4Unmarshaller")
public Jaxb2Marshaller A4Unmarshaller(){
    Jaxb2Marshaller unMarshaller = new Jaxb2Marshaller();
    unMarshaller.setContextPath("package name for the classes generate by XSD A4");
}

@Bean(name="A5Unmarshaller")
public Jaxb2Marshaller A5Unmarshaller(){
    Jaxb2Marshaller unMarshaller = new Jaxb2Marshaller();
    unMarshaller.setContextPath("package name for the classes generate by XSD A5");
}

现在我有五个不同的类 C1、C2、C3、C4 和 C5,我正在尝试将一个 unmarshaller bean 注入一个类.这意味着 A1Unmarshaller 自动连接到 C1 等等.

Now I have five different classes C1, C2, C3, C4 and C5 and I am trying to inject one unmarshaller bean into one class. That means A1Unmarshaller is autowired to C1 and so on.

当构建 Spring 上下文时,它会抛出一个错误,说它需要一个 Jaxb2Marshaller 类型的 bean 并得到五个.

When the Spring context is built, it throws an error saying it expected one bean of type Jaxb2Marshaller and got five.

注意 使用 XML 配置完成后它工作正常,所以我不确定我是否遗漏了什么.请帮忙.

Note It worked fine when done using XML configuration, so I am not sure if I am missing something. Please help.

编辑 C1 类之一的代码如下:

EDIT The code for one of the classes C1 is below:

@Component
public class C1{

@Autowired
private Jaxb2Marshaller A1Unmarshaller;
    A1 o = null

public boolean handles(String event, int eventId) {
    if (null != event&& eventId == 5) {
                A1 =  A1Unmarshaller.unMarshal(event);
        return true;
    }
    return false;
}

}

推荐答案

你应该限定你的自动装配变量来说明应该注入哪一个

You should qualify your autowired variable to say which one should be injected

@Autowired
@Qualifier("A1Unmarshaller")
private Jaxb2Marshaller A1Unmarshaller;

默认的自动装配是按类型,而不是按名称,所以当有多个相同类型的bean时,你必须使用@Qualifier注解.

The default autowiring is by type, not by name, so when there is more than one bean of the same type, you have to use the @Qualifier annotation.

这篇关于Spring中需要多个相同类型的bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Android schema validation(Android 架构验证)
How to deal with JAXB ComplexType with MixedContent data?(如何处理带有 MixedContent 数据的 JAXB ComplexType?)
Validate an XML File Against Multiple Schema Definitions(针对多个模式定义验证 XML 文件)
JAXB - Property quot;Valuequot; is already defined. Use lt;jaxb:propertygt; to resolve this conflict(JAXB - 属性“值;已经定义了.使用 lt;jaxb:propertygt;解决这个冲突)
XML instance generation from XML schema (xsd)(从 XML 模式 (xsd) 生成 XML 实例)
Java API to parse XSD schema file(用于解析 XSD 模式文件的 Java API)