【问题标题】:Cannot find the declaration of element 'beans'.找不到元素“bean”的声明。
【发布时间】:2026-02-13 02:15:01
【问题描述】:

下面是我的 sdnext-servlet.xml 声明。我收到此错误

“找不到元素'beans'的声明”

即使我在线。我正在使用 Spring 3.0.1。

这里是 sdnext-servlet.xml:

<beans xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="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/tx  
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:property-placeholder location="classpath:resources/database.properties">
    </context:property-placeholder>
    <context:component-scan base-package="com">
    </context:component-scan>

    <tx:annotation-driven transaction-manager="hibernateTransactionManager">
    </tx:annotation-driven>

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        id="jspViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        id="dataSource">
        <property name="driverClassName" value="${database.driver}"></property>
        <property name="url" value="${database.url}"></property>
        <property name="username" value="${database.user}"></property>
        <property name="password" value="${database.password}"></property>
    </bean>

    <bean
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
        id="sessionFactory">
        <property name="dataSource" ref="dataSource"></property>
        <property name="annotatedClasses">
            <list>
                <value>com.dto.Causer</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}  </prop>
            </props>
        </property>
    </bean>

    <bean class="org.springframework.orm.hibernate3.HibernateTransactionManager"
        id="hibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
</beans>  

【问题讨论】:

  • 尝试删除带有 schemaLocation 的版本,例如 http://www.springframework.org/schema/beans/spring-beans.xsd。你用的是哪个版本的spring?
  • 我正在使用 spring 3.0.1
  • 我删除了版本并尝试了,仍然得到同样的错误。
  • 无论如何 xml 似乎有效.. 启动 spring 上下文时是否有问题?或者你什么时候尝试验证 xml?
  • 是的,我正在从 ServletContext 资源 [/WEB-INF/config/sdnext-servlet.xml] 获取此“XML 文档中的第 10 行无效;嵌套异常是 org.xml.sax.SAXParseException: cvc -elt.1:找不到元素“beans”的声明。”

标签: java xml spring xsd


【解决方案1】:

这是schemaLocation,而不是schemalocation

修复该错误后,关闭 context:property-placeholder 和其他一些类似元素,而不是让它们的内容包含换行符。

这是您更新后的有效 XML:

<beans xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="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/tx
                           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

  <context:property-placeholder location="classpath:resources/database.properties"/>
  <context:component-scan base-package="com"/>

  <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

  <bean
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      id="jspViewResolver">
    <property name="viewClass"
              value="org.springframework.web.servlet.view.JstlView"></property>
    <property name="prefix" value="/WEB-INF/views/"></property>
    <property name="suffix" value=".jsp"></property>
  </bean>

  <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        id="dataSource">
    <property name="driverClassName" value="${database.driver}"></property>
    <property name="url" value="${database.url}"></property>
    <property name="username" value="${database.user}"></property>
    <property name="password" value="${database.password}"></property>
  </bean>

  <bean
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
      id="sessionFactory">
    <property name="dataSource" ref="dataSource"></property>
    <property name="annotatedClasses">
      <list>
        <value>com.dto.Causer</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}  </prop>
      </props>
    </property>
  </bean>

  <bean class="org.springframework.orm.hibernate3.HibernateTransactionManager"
        id="hibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>
</beans>  

【讨论】:

    最近更新 更多