简体   繁体   中英

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.

I created the list this way:

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

Then I want to do

getBean("airMessageHandlers");

The problem is that when I bring in the XML file, I get:

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; 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 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) ...

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.

Turns out the solution was:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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