“Bean name must not be empty” error on spring bean that has a bean name

I am puzzled at the error I am getting when trying to start up tomcat with my maven build (shown below). For some context, I am converting an old app into a maven project. The error comes when I run:

mvn tomcat:run-war

The project builds fine, but blows up when parsing the applicationContext files on server startup. applicationContext-main.xml (shown below) contains references to beans defined in both applicationContext-foo.xml and applicationContext.shared.xml.

The applicationContext files work as expected in the non-maven app, and this is a direct copy of the applicationContext files.

I’ve tried some debugging and have found that the external references to other application context files are being resolved fine, adding a name attribute results in: a bean with the name “cacheManager” has already been defined in this file, swapping the id for the name attribute results in the same “bean name must not be empty error”, and making the constructor arg an empty map results in the same error.

I’m puzzled. I did come across this post which reported the same error, and was caused by a prefix being set on the beans. I’m unaware of any prefix being set in my case.

Any ideas on how to resolve this? Thanks in advance.

Nov 05, 2012 3:10:11 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/xactsites/shared/applicationContext-shared.xml]
Nov 05, 2012 3:10:11 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/xactsites/foo/applicationContext-foo.xml]
Nov 05, 2012 3:10:11 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/xactsites/help/applicationContext-help.xml]
Nov 05, 2012 3:10:11 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/classes/applicationContext-main.xml]
Nov 05, 2012 3:10:11 PM org.springframework.web.context.ContextLoader initWebApplicationContext
SEVERE: Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unexpected failure during bean definition parsing
Offending resource: ServletContext resource [/WEB-INF/classes/applicationContext-main.xml]
Bean 'cacheManager'; nested exception is java.lang.IllegalArgumentException: Bean name must not be empty
Caused by: java.lang.IllegalArgumentException: Bean name must not be empty
    at org.springframework.util.Assert.hasText(Assert.java:161)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:305)
    at org.springframework.beans.factory.xml.UtilNamespaceHandler$MapBeanDefinitionParser.parse(UtilNamespaceHandler.java:163)
    at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:78)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1147)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseNestedCustomElement(BeanDefinitionParserDelegate.java:1185)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parsePropertySubElement(BeanDefinitionParserDelegate.java:844)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parsePropertySubElement(BeanDefinitionParserDelegate.java:832)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parsePropertyValue(BeanDefinitionParserDelegate.java:828)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseConstructorArgElement(BeanDefinitionParserDelegate.java:734)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseConstructorArgElements(BeanDefinitionParserDelegate.java:641)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseBeanDefinitionElement(BeanDefinitionParserDelegate.java:563)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseBeanDefinitionElement(BeanDefinitionParserDelegate.java:421)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseBeanDefinitionElement(BeanDefinitionParserDelegate.java:390)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:165)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:142)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:89)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:499)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:407)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:357)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:126)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:142)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:123)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:91)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:94)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:294)
    at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:156)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:246)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:184)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4135)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
    at org.apache.catalina.startup.Embedded.start(Embedded.java:825)
    at org.codehaus.mojo.tomcat.AbstractRunMojo.startContainer(AbstractRunMojo.java:558)
    at org.codehaus.mojo.tomcat.AbstractRunMojo.execute(AbstractRunMojo.java:255)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)

applicationContext-main.xml

...
<bean id="cacheManager" class="com.foo.bar.commons.caching.MainCacheManager">
    <constructor-arg>
        <util:map>
            <entry key="RIGHTS_CACHES">
                <util:list value-type="com.foo.bar.commons.caching.MainCache">
                    <ref bean="aCache" />
                    <ref bean="gRepository" />
                    <ref bean="gRCache" />
                    <ref bean="uRCache" />
                    <ref bean="userService" />
                    <ref bean="vUService" />
                    <ref bean="rIService" />
                </util:list>
            </entry>
            <entry key="RULES_CACHES">
                <util:list value-type="com.foo.bar.commons.caching.MainCache">
                    <ref bean="gBRService" />
                </util:list>
            </entry>
        </util:map>
    </constructor-arg>
</bean>
...

Answer

All the examples in the official documentation seem to use id="" attribute, check this out:

<util:map id="caches">
    <entry key="RIGHTS_CACHES">
        <util:list id="rightsCaches" value-type="com.foo.bar.commons.caching.MainCache">
            <ref bean="aCache" />
            <!-- ... -->
        </util:list>
    </entry>
    <entry key="RULES_CACHES">
        <util:list id="rulesCaches" value-type="com.foo.bar.commons.caching.MainCache">
            <ref bean="gBRService" />
        </util:list>
    </entry>
</util:map>

And consider using Java @Configuration, much easier to read and maintain than XML.

Leave a Reply

Your email address will not be published. Required fields are marked *