Bean配置xml文件头中出错 -

时间:2012-12-02 00:45:03

标签: java spring-mvc xsd

在apache tomcat上运行我的应用程序时,我遇到了以下错误。

HTTP Status 500 - Servlet.init() for servlet spring threw exception

根本原因

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: 
Line 11 in XML document from ServletContext resource [/WEB-INF/spring-servlet.xml] is invalid;
 nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.

我的spring-context.xml就是这个

            <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans http://java.sun.com/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:cotext="http://www.springframework.org/schema/context"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:beans="http://www.springframework.org/schema/beans" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans
                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                         http://www.springframework.org/schema/context
                         http://www.springframework.org/schema/context/spring-context-3.0.xsd
                         http://www.springframework.org/schema/mvc">

     <mvc:annotation-driven/>
     <cotext:component-scan base-package="springmvc"/>   

我试过https://jira.springsource.org/browse/SPR-3372没有运气。

请帮忙。

2 个答案:

答案 0 :(得分:0)

您在XML标头中重复输入xmlns:beans="http://www.springframework.org/schema/beans"。删除它的一个实例并尝试如下:

<beans xmlns="http://www.springframework.org/schema/beans http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:cotext="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.0.xsd
               http://www.springframework.org/schema/mvc">

      <mvc:annotation-driven/>
      <cotext:component-scan base-package="springmvc"/>   

答案 1 :(得分:0)

很少有事情让我失望:

1)您必须删除xmlns属性中的“http://java.sun.com/xml/ns/javaee”。我认为这是主要问题。 “xmlns”属性不能根据规范获取多个URI: http://www.w3schools.com/xml/xml_namespaces.asp

2)有些XML库不喜欢前面的XML文件开头的空格(特别是Eclipse会抱怨)

3)xsi:schemaLocation值缺少mvc的位置URI。它应该看起来像这样:

 xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.0.xsd
               http://www.springframework.org/schema/mvc
               http://www.springframework.org/schema/context/spring-mvc-3.0.xsd">

你可以删除额外的“beans”声明,如前面的答案所示,除非你真的想因某种原因使用“beans”命名空间前缀。