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

时间:2013-09-10 06:19:54

标签: java xml spring spring-mvc xsd

在您将其标记为重复之前的请求。我已经浏览了论坛,无法在任何地方找到问题的解决方案。

我正在使用Spring 3.2编写代码,一切都纯粹基于注释。代码接收从不同XSD文件派生的XML文件。

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

现在,我正试图使用​​Spring OXM解组这些。但由于涉及多个XSD,我们实际上无法使用一个Un-marshaller。所以我们需要大约五个。

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

@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,依此类推。

当构建Spring上下文时,它会抛出一个错误,说它期望一个Jaxb2Marshaller类型的bean得到五个。

注意使用XML配置完成后工作正常,所以我不确定是否遗漏了某些内容。请帮忙。

编辑其中一个类C1的代码如下:

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

}

3 个答案:

答案 0 :(得分:40)

您应该限定自动装配变量以说明应该注入哪一个

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

默认自动装配是按类型而不是按名称,因此当存在多个相同类型的bean时,必须使用@Qualifier注释。

答案 1 :(得分:4)

使用df <- read.table(text="Record Person_Ref Trade_Code 1 512 elec 1 512 plumbing 1 512 gas 2 654 gas 2 654 plumbing 3 685 elec",header=TRUE,stringsAsFactors=FALSE) df%>% group_by(Record,Person_Ref) %>% summarise(catY = toString(Trade_Code)) Record Person_Ref catY <int> <int> <chr> 1 1 512 elec, plumbing, gas 2 2 654 gas, plumbing 3 3 685 elec 注释进行注入是您正在寻找的。你可以使用

@Resource

但这不是唯一的方法。

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

这份工作也是。我建议你使用后者! See why

答案 2 :(得分:3)

Jaxb2Marshaller完全能够处理多个不同的上下文/ xsd。只需使用setContextPaths方法指定多个上下文路径。

@Bean(name="A1Unmarshaller")
public Jaxb2Marshaller A1Unmarshaller(){
    Jaxb2Marshaller unMarshaller = new Jaxb2Marshaller();
    unMarshaller.setContextPaths(
        "package name for the classes generate by XSD A1",
        "package name for the classes generate by XSD A2",
        "package name for the classes generate by XSD A3",
        "package name for the classes generate by XSD A4",
        "package name for the classes generate by XSD A5" );
    return unMarshaller;
}

这样你只需要一个marshaller / unmarshaller。

链接

  1. Jaxb2Marshaller javadoc
  2. setContextPaths javadoc