久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Spring中需要多個(gè)相同類型的bean

Required Multiple beans of same type in Spring(Spring中需要多個(gè)相同類型的bean)
本文介紹了Spring中需要多個(gè)相同類型的bean的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(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)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 一区二区三区四区在线视频 | 久久99久久98精品免观看软件 | 亚洲91 | 国产激情毛片 | 国产一区 在线视频 | 国产精品视频一区二区三区四区国 | 日本精品久久 | 国产精品国产三级国产播12软件 | 欧美日韩在线精品 | 天天夜碰日日摸日日澡 | 在线观看亚洲专区 | 午夜天堂精品久久久久 | 精品久久九 | 午夜国产 | 亚洲一区二区三区在线观看免费 | 黄色小视频入口 | 中文字幕在线一区二区三区 | 国产精品久久久久久妇女6080 | 天天草天天干天天 | 在线一区视频 | 国产香蕉视频在线播放 | 欧美电影一区 | 国产成人免费在线 | 伊人伊人伊人 | 91资源在线 | 蜜臀网站 | 日韩在线观看精品 | 欧美一级片在线观看 | 国产精品久久久久免费 | 婷婷一级片 | 免费在线观看91 | 一区二区三区免费 | 国产午夜精品视频 | 久久久国产一区二区三区四区小说 | 亚洲精品一区二区三区中文字幕 | 日韩最新网址 | 97色综合 | 精品二三区 | 一区二区三区在线播放视频 | 精品视频www | 成人乱人乱一区二区三区软件 |