简体   繁体   English

在XML中以bean形式创建弹簧列表

[英]creating spring list as bean in XML

I want to create a list of beans with an ID so that I can use ApplicationContext.getBean() to retrieve it into my standalone application. 我想创建一个具有ID的bean列表,以便可以使用ApplicationContext.getBean()将其检索到我的独立应用程序中。

I created the list this way: 我以这种方式创建了列表:

<list id="airMessageHandlers">
    <bean id="signInResultHandler" class="com.blah.AirMessageHandler"/>
</list>

Then I want to do 那我想做

getBean("airMessageHandlers"); 的getBean( “airMessageHandlers”);

The problem is that when I bring in the XML file, I get: 问题是,当我导入XML文件时,我得到:

Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 18 in XML document from class path resource [conf/portserver.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 18; columnNumber: 37; cvc-complex-type.2.4.a: Invalid content was found starting with element 'list'. One of '{"http://www.springframework.org/schema/beans":import, "http://www.springframework.org/schema/beans":alias, "http://www.springframework.org/schema/beans":bean, WC[##other:"http://www.springframework.org/schema/beans"], "http://www.springframework.org/schema/beans":beans}' is expected.
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:398)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:335)

... Caused by: org.xml.sax.SAXParseException; ...由...引起:org.xml.sax.SAXParseException; lineNumber: 18; lineNumber:18; columnNumber: 37; columnNumber:37; cvc-complex-type.2.4.a: Invalid content was found starting with element 'list'. cvc-complex-type.2.4.a:发现无效的内容(从元素“列表”开始)。 One of '{" http://www.springframework.org/schema/beans ":import, " http://www.springframework.org/schema/beans ":alias, " http://www.springframework.org/schema/beans ":bean, WC[##other:" http://www.springframework.org/schema/beans "], " http://www.springframework.org/schema/beans ":beans}' is expected. 其中一个“{” http://www.springframework.org/schema/beans “:进口,” http://www.springframework.org/schema/beans “:别名” http://www.springframework.org / schema / beans “:bean,WC [## other:” http://www.springframework.org/schema/beans “],” http://www.springframework.org/schema/beans“:beans }'是期待。 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325) ... 在com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)处com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)上com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)在com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)...

I couldn't find an example of a list as a stand alone bean and I'm wondering if that is a problem, but the DTD looks fine for this. 我找不到作为独立bean的列表示例,并且我想知道这是否有问题,但是DTD对此很好。

Turns out the solution was: 原来的解决方案是:

<util:list id="airMessageHandlers">
    <ref bean="signInResultHandler"/>
</util:list>

<bean id="signInResultHandler" class="com.blah.AirMessageHandler"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM