" Bean名称不能为空"具有bean名称的spring bean上的错误

时间:2012-11-05 22:40:03

标签: java spring tomcat maven

我很困惑于尝试使用我的maven构建启动tomcat时出现的错误(如下所示)。对于某些上下文,我将旧应用程序转换为maven项目。运行时出现错误:

mvn tomcat:run-war

项目构建正常,但在服务器启动时解析applicationContext文件时会爆炸。 applicationContext-main.xml(如下所示)包含对applicationContext-foo.xml和applicationContext.shared.xml中定义的bean的引用。

applicationContext文件在非maven应用程序中按预期工作,这是applicationContext文件的直接副本。

我已经尝试了一些调试,并且发现对其他应用程序上下文文件的外部引用正在被解析,添加了一个name属性导致:在此文件中已经定义了一个名为“cacheManager”的bean,交换name属性的id导致相同的“bean name不能为空错误”,并使构造函数arg为空映射会导致相同的错误。

我很困惑。我确实遇到了这个post报告了同样的错误,并且是由bean上设置的前缀引起的。我不知道在我的情况下设置了任何前缀。

有关如何解决此问题的任何想法?提前谢谢。

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

1 个答案:

答案 0 :(得分:4)

所有examples in the official documentation似乎都使用id=""属性,请查看:

<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>

考虑使用Java @Configuration,比XML更容易阅读和维护。