org.springframework.beans.factory.BeanDefinitionStoreException:从 ServletContext 资源解析 XML 文档时出现意外异常

Posted

技术标签:

【中文标题】org.springframework.beans.factory.BeanDefinitionStoreException:从 ServletContext 资源解析 XML 文档时出现意外异常【英文标题】:org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource 【发布时间】:2011-07-11 10:18:36 【问题描述】:

我是 spring mvc 3 和 hibernate 3 的新手。我需要澄清以下问题。我被这个问题锁定了,所以我无法继续下一步。

这是我的dispatch-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>


    <context:component-scan base-package="net.example"/>
    <tx:annotation-driven/>
    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

</beans>  

一旦我加入这一行,我得到以下错误,我试图解决,但什么都没有发生。

org.springframework.beans.factory.BeanDefinitionStoreException:从 ServletContext 资源 [/WEB-INF/dispatcher-servlet.xml] 解析 XML 文档时出现意外异常;嵌套异常是 java.lang.NoClassDefFoundError: org/springframework/transaction/interceptor/TransactionInterceptor org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:412) org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143) org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178) org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149) org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124) org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:93) org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130) org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:465) org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:395) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458) org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339) org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306) org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127) javax.servlet.GenericServlet.init(GenericServlet.java:160) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166) org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) java.lang.Thread.run(Thread.java:662)

我使用的是 spring 3.0.2 RELEASE 版本。并且还在我的库中包含了 spring-tx.3.0.2.RELEASE.jar(我使用的是 netbeans 7.0)

【问题讨论】:

【参考方案1】:

您的类路径中可能缺少spring-tx.XXX.jarorg.springframework.transaction.XXX.jar

【讨论】:

我包含了 spring-tx-3.0.2.RELEASE.jar,但发生了同样的错误。我正在使用 netbeans 7.0 IDE 作为编辑器【参考方案2】:

使用这个:

 <tx:annotation-driven/>

在你的类路径(可能还有其他)中需要这个:

org.springframework.transaction-3.1.0.M2.jar

【讨论】:

注意包/jar 名称已更改为 spring-tx.xxx.jar;见斯卡夫曼的回答【参考方案3】:

您缺少 Beans 的起始标记,这就是 xml 解析器无法解析您的文件的原因

<?xml version="1.0" encoding="UTF-8"?>

<beans>
<context:component-scan base-package="net.example"/>
<tx:annotation-driven/>
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

</beans>

【讨论】:

【参考方案4】:

将以下代码格式化为单行。

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

它可能会解决您的问题。

【讨论】:

以上是关于org.springframework.beans.factory.BeanDefinitionStoreException:从 ServletContext 资源解析 XML 文档时出现意外异常的主要内容,如果未能解决你的问题,请参考以下文章

REST API:org.springframework.beans.factory.UnsatisfiedDependencyException:

Spring security-org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.sec

org.springframework.beans.factory.UnsatisfiedDependencyException:

没有实现 [org.springframework.beans.factory.xml.NamespaceHandler] 接口

Spring Boot 错误 org.springframework.beans.factory.UnsatisfiedDependencyException

org.springframework.beans.factory.UnsatisfiedDependencyException:创建 bean 时出错