【问题标题】:XMLBeanDefinitionStoreException: Cannot find the declaration of element 'beans'XMLBeanDefinitionStoreException:找不到元素“bean”的声明
【发布时间】:2014-02-23 19:15:45
【问题描述】:

我正在尝试以下代码:http://www.dineshonjava.com/2012/12/spring-mvc-with-hibernate-crud-example.html#.Uus0bvnoSGc 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.dineshonjava">
</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.dineshonjava.model.Employee</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>

我遇到了异常

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

我不知道出了什么问题。请帮忙

【问题讨论】:

    标签: xml spring hibernate spring-mvc


    【解决方案1】:

    把xml命名空间定义改成这个

    <beans  xmlns="http://www.springframework.org/schema/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"  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">
    

    它应该可以工作

    问题在于您对xsi:schemalocation 的定义,拼写错误是L 它应该是大写,只有这样它才能识别标签。

    【讨论】:

    • 你改变了什么?
    • 我按照建议使用了 xml 命名空间定义,但错误并没有消失。在命名空间定义的最后一行仍然是一个异常
    • 编辑了答案并解释了您的问题。 schemaLocation 有一个错字
    • 我得到了同样的错误,我也改变了 xml 文件,但没有工作。
    • 创建一个不同的问题并将其发布在 SO 上。会有人为你解答。
    【解决方案2】:

    这对我有用:

    需要更改默认命名空间,如下所示:

    <beans:beans xmlns="http://www.springframework.org/schema/security"
           xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.2.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd"
           xmlns:context="http://www.springframework.org/schema/context">
    
        <mvc:resources mapping="/scripts/**" location="/scripts/" />
        <mvc:resources mapping="/resources/**" location="/resources/" />
        <context:component-scan base-package="com.dewsoftware.webapp" />
    
    </beans:beans> 
    

    【讨论】:

      【解决方案3】:

      这有点牵强,但它可能会发生在其他人身上,就像发生在我身上一样。

      我正在用maven-assembly-plugin 打包一个胖罐子,并在尝试执行我的主类时得到了XMLBeanDefinitionStoreException。插件配置如下:

            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-assembly-plugin</artifactId>
              <version>3.1.0</version>
              <configuration>
                <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                  <manifest>
                    <mainClass>com.bigcompany.module.MyMainClass</mainClass>
                  </manifest>
                </archive>
              </configuration>
              <executions>
                <execution>
                  <id>make-assembly</id> this is used for inheritance merges
                  <phase>package</phase> bind to the packaging phase
                  <goals>
                    <goal>single</goal>
                  </goals>
                </execution>
              </executions>
            </plugin>
      

      我的解决方案是使用不同的插件:maven-shade-plugin。配置如下:

          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-shade-plugin</artifactId>
              <version>1.5</version>
              <executions>
                  <execution>
                      <phase>package</phase>
                      <goals>
                          <goal>shade</goal>
                      </goals>
                      <configuration>
                          <transformers>
                              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                  <mainClass>com.bigcompany.module.MyMainClass</mainClass>
                              </transformer>
                              <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                  <resource>META-INF/spring.handlers</resource>
                              </transformer>
                              <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                  <resource>META-INF/spring.schemas</resource>
                              </transformer>
                          </transformers>
                          <shadedArtifactAttached>true</shadedArtifactAttached>
                          <!-- configures the suffix name for the executable jar here it will be '<project.artifact>-<project.version>-executable.jar' -->
                          <shadedClassifierName>executable</shadedClassifierName>
                          <filters>
                              <filter>
                                  <artifact>*:*</artifact>
                                  <excludes>
                                      <exclude>META-INF/*.SF</exclude>
                                      <exclude>META-INF/*.DSA</exclude>
                                      <exclude>META-INF/*.RSA</exclude>
                                  </excludes>
                              </filter>
                          </filters>
                      </configuration>
                  </execution>
              </executions>
          </plugin>
      

      生成的 jar 确实使用与其他插件生成的 jar 引发异常的同一命令行无异常地执行。

      【讨论】:

      • @cm12 这就是重点。既然它有所帮助,答案就值得写了。
      • 这正是我的问题。很遗憾 maven-assembly-plugin 没有按预期工作。
      【解决方案4】:

      更改命名空间:&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"&gt;

      【讨论】:

        猜你喜欢
        • 2012-11-28
        • 1970-01-01
        • 1970-01-01
        • 2017-09-17
        • 2011-12-27
        • 1970-01-01
        • 2012-07-02
        • 2012-09-14
        相关资源
        最近更新 更多