You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring MVC整合PostgreSQL启动时Web上下文初始化失败异常

解决Spring MVC配置文件中Cannot find the declaration of element 'beans'异常

从你贴出的日志和配置文件来看,这个启动异常的核心原因是Spring XML配置的命名空间与Schema地址配置错误,同时还有几处小的属性配置问题,我来一步步帮你解决:

核心问题分析

1. 默认命名空间与根元素不匹配

你的mvc-dispatcher-servlet.xml把默认命名空间设为了http://www.springframework.org/schema/mvc,但根元素却是<beans>——XML解析器会在默认命名空间下找beans元素的定义,自然找不到,这就是日志里Cannot find the declaration of element 'beans'的直接原因。

2. 事务Schema地址写错

在xsi:schemaLocation里,事务(tx)的Schema地址被错误写成了http://www.springframework.org/schema/context/spring-tx-4.0.xsd,正确的地址应该是http://www.springframework.org/schema/tx/spring-tx-4.1.xsd,这个错误会导致Spring无法加载事务相关的Schema定义,进一步干扰配置文件的解析。

3. 属性名配置错误

NumberDAO和HibernateTransactionManager里的属性名用了s,这是错误的——Spring整合Hibernate时,标准的属性名是sessionFactory,这个错误会导致后续依赖注入失败。


修正后的完整配置文件

我已经把所有问题都修正好了,直接替换你的mvc-dispatcher-servlet.xml即可:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                           http://www.springframework.org/schema/mvc 
                           http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-4.1.xsd
                           http://www.springframework.org/schema/tx 
                           http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

  <context:component-scan base-package="com.mkyong.controller" />
  
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/" />
    <property name="suffix" value=".jsp" />
  </bean>

  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://localhost:5432/ucan" />
    <property name="username" value="ucan" />
    <property name="password" value="ucan" />
  </bean>

  <bean id="hibernate4AnnotatedSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
      <list>
        <value>com.mkyong.controller.Numbers</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
        <prop key="hibernate.show_sql">false</prop>
      </props>
    </property>
  </bean>

  <bean id="nodao" class="com.mkyong.controller.NumberDAO">
    <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
  </bean>

  <tx:annotation-driven transaction-manager="transactionManager"/>
  
  <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
  </bean>

</beans>

额外说明:我把Schema版本从4.0改成了和你Spring版本一致的4.1,避免版本不兼容的潜在问题。


后续检查点

修正配置后,还需要确认以下几点:

  • 本地PostgreSQL服务已经启动,数据库ucan存在,用户名和密码正确
  • Numbers实体类的JPA注解配置正确(比如@Entity、@Id等注解不能少)
  • Maven已经正确下载所有依赖,没有缺失或冲突的Jar包

内容的提问来源于stack exchange,提问作者Disha Agrawal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:48:32