問(wèn)題描述
在將其標(biāo)記為重復(fù)之前的請(qǐng)求.我瀏覽了論壇,在任何地方都找不到問(wèn)題的解決方案.
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 編寫代碼,所有內(nèi)容都純粹基于注釋.該代碼接收從不同 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.
所以我們可以說(shuō),有五種不同的 XSD(A1、A2、A3、A4、A5),我的代碼接收任何類型的 XML,并且我有邏輯在到達(dá)時(shí)識(shí)別 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.
現(xiàn)在,我正在嘗試使用 Spring OXM 取消編組這些內(nèi)容.但是因?yàn)樯婕暗蕉鄠€(gè) XSD,我們實(shí)際上不能使用一個(gè) Un-marshaller.所以我們需要大約五個(gè).
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
類中,我添加了五個(gè) 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");
}
現(xiàn)在我有五個(gè)不同的類 C1、C2、C3、C4 和 C5,我正在嘗試將一個(gè) unmarshaller bean 注入一個(gè)類.這意味著 A1Unmarshaller
自動(dòng)連接到 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.
當(dāng)構(gòu)建 Spring 上下文時(shí),它會(huì)拋出一個(gè)錯(cuò)誤,說(shuō)它需要一個(gè) Jaxb2Marshaller
類型的 bean 并得到五個(gè).
When the Spring context is built, it throws an error saying it expected one bean of type Jaxb2Marshaller
and got five.
注意 使用 XML 配置完成后它工作正常,所以我不確定我是否遺漏了什么.請(qǐng)幫忙.
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;
}
}
推薦答案
你應(yīng)該限定你的自動(dòng)裝配變量來(lái)說(shuō)明應(yīng)該注入哪一個(gè)
You should qualify your autowired variable to say which one should be injected
@Autowired
@Qualifier("A1Unmarshaller")
private Jaxb2Marshaller A1Unmarshaller;
默認(rèn)的自動(dòng)裝配是按類型,而不是按名稱,所以當(dāng)有多個(gè)相同類型的bean時(shí),你必須使用@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.
這篇關(guān)于Spring中需要多個(gè)相同類型的bean的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!