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

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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 夜夜草视频| 日韩成人影院在线观看 | 久久在线免费 | 亚洲免费成人av | 美女啪啪国产 | 天堂网av在线 | 欧美一级久久久猛烈a大片 日韩av免费在线观看 | 欧美福利一区 | 亚洲欧美精品在线观看 | 国产精品欧美一区二区三区不卡 | 免费天天干| 欧美日韩三区 | 免费视频一区二区三区在线观看 | 国产精品久久久亚洲 | 在线视频亚洲 | 欧美视频免费在线观看 | 国产精品日韩在线观看一区二区 | 午夜午夜精品一区二区三区文 | 不卡的av在线 | 北条麻妃一区二区三区在线视频 | 日韩欧美在线视频播放 | 国产成人精品免费视频大全最热 | 天天干天天操天天爽 | 精品成人佐山爱一区二区 | 黄色欧美视频 | 亚洲精品二区 | 亚洲国产欧美国产综合一区 | 亚洲精品中文字幕 | 国产99久久久国产精品 | 中文字幕日韩欧美一区二区三区 | 91久久精品一区二区二区 | 自拍视频网站 | 国产激情视频在线 | 精品一级毛片 | 亚洲激精日韩激精欧美精品 | 狠狠爱免费视频 | 超碰精品在线 | 99热国产免费 | 国产成人在线视频播放 | 久久久久久久久久性 | 黄色一级特级片 |