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

必需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.

配置类中,我添加了五个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 A1Unmarshaller(){
    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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:28